Question

C++ Simulate movement of text object across the screen using sleep(1); and  cout << "\033[2J\033[1;1H" //to clear...

C++

Simulate movement of text object across the screen using sleep(1); and  cout << "\033[2J\033[1;1H" //to clear screen These are non-negotiable. Have to use sleep(1) and make it work, per instructor instruction.

The code I have written (below) clears the screen but does not show the movement of the object. Please help! It is supposed to show the object moving horizontally or vertically.

#include
#include
#include
#include
using namespace std;


int main()
{
   enum Direction {Horizontal = 'H', Vertical = 'V'};
  
   char usr_direction;
   char response;  
  
   string triangle;
   string down;
   string right;
  
   const int move = 12; //will indicate how many to move
   int height; //number of lines of object
   int b = 1;
  
   do
   {
      
       cout << "Enter the height of the structure: ";
       cin >> height;
          
       cout << "Which direction? H for Horizontal or V for vertical: ";
       cin >> usr_direction;
       usr_direction = toupper(usr_direction);
  
      
      
           for (int i = 1; i <= height; i++) //triangle
           {
      
               for(int j = 0; j<= height-i; j++)
               {
                   triangle += " ";
               }
               for(int k = 0; k != 2*i-1; k++)
               {
                   triangle +='*';
               }
          
           triangle += '\n';
           }
           cout << triangle << endl;
      
           while( b <= move)
           {
               sleep(1); // wait for one second before clearing
               cout << "\033[2J\033[1;1H"; //clear screen
              
               switch (usr_direction)
               {
                   case 'H':                      
                       right = "/t"; // Horizontal
                       cout << right << triangle;
                      
                       b++;
              
                       break;
          
                   case 'V':
                       down = "\n"; // Vertical
                       cout << down << triangle;

                      
                       b++;
              
                       break;
          
                   default:
                       cout << "Inappropriate response.";
               }

           }
      
      
       cout << "Do you want to replay? Y/N: ";
       cin >> response;
      
   }while(response == 'Y' || response == 'y');
  
  
  
  
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

Successfully executed on ubuntu 18.10

#include<iostream>
#include <unistd.h>//for sleep() in linux
#include <sstream>//for getline()
using namespace std;


int main()
{
   enum Direction {Horizontal = 'H', Vertical = 'V'};

   char usr_direction;
   char response;

   string triangle;
   string down;
   string right;


   const int move = 12; //will indicate how many to move
   int height; //number of lines of object
   int b = 1;

   do
   {
    
       cout << "Enter the height of the structure: ";
       cin >> height;
        
       cout << "Which direction? H for Horizontal or V for vertical: ";
       cin >> usr_direction;
       usr_direction = toupper(usr_direction);

    
    
           for (int i = 1; i <= height; i++) //triangle
           {
    
               for(int j = 0; j<= height-i; j++)
               {
                   triangle += " ";
               }
               for(int k = 0; k != 2*i-1; k++)
               {
                   triangle +='*';
               }
        
           triangle += '\n';
           }
           cout << triangle << endl;
    
           while( b <= move)
           {
               sleep(1); // wait for one second before clearing
               cout << "\033[2J\033[1;1H"; //clear screen
            
               switch (usr_direction)
               {
                   case 'H':   
{
// \t works for only first line of string. so we will do few changes below
       istringstream f(triangle);
       string line;
       triangle="";//empty triangle
       //WE will get each line of triangle and shift it by \t and again add each line to triangle and we will get new triangle wiht \t.
                                while (getline(f, line)) {
                              
/* initialize triangle lines everytime with \t concatinated */
                         line="\t"+line;
                     triangle+=line+"\n";
                               }
                         right=triangle;//initialize right by triangle (you can also print triangle directly)
                                   cout << right ; // Horizontal

                             b++;
                       break;
          }
                   case 'V':
                 
/* initialize triangle everytime with \n concatinated */
                       triangle = "\n"+triangle; // Vertical
                       down=triangle;
                       cout << down ;

                    
                       b++;
            
                       break;
        
                   default:
                       cout << "Inappropriate response.";
               }

           }
    
    
       cout << "Do you want to replay? Y/N: ";
       cin >> response;
    
   }while(response == 'Y' || response == 'y');




   return 0;
}

Output

I have skipped complete output as it was too long , but was similar.You can see yourself by running program.

Add a comment
Know the answer?
Add Answer to:
C++ Simulate movement of text object across the screen using sleep(1); and  cout << "\033[2J\033[1;1H" //to clear...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • You've been hired by Rugged Robots to complete a C++ console application that controls a robotic...

    You've been hired by Rugged Robots to complete a C++ console application that controls a robotic floor vacuum. Wayne State software engineers have already developed the following three files: File Description Lab20-01-Key.cpp This is the user application. RuggedRobotLibrary.h This is the library header file. It contains constant declarations and function prototypes. It is included by the other two files. RuggedRobotLibrary.cpp This is the library file. It contains functions that may be used in any user application. This is considered an...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output:...

    Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output: A 1000Pairs.txt looks like this: {100, A } {200, A } {300, B } {400, C } {500, D } {600, E } {700, F } {800, G } {900, H } {1000, I } {1100, J } {1200, K } {1300, L } {1400, M } {1500, N } {1600, O } {1700, P } {1800, Q } {1900, R } {2000, S...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT