•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,534 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,992 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 754 | Replies: 12
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
for my c++ class we are writing a simon program (the game where you have to remember and follow the pattern given to you).
each turn the program outputs either r, b, g or y to the current pattern
(output should look like this:
turn1: y
2: yg
3:ygy
4:ygyr
etc.
but we are supposed to use the line cout<<"\010."<<flush; so that each letter dissapears after one second (hence the memory part of the game).
what package do i need to include so that this works properly because right now it is just outputing a period instead the the letter and replacing it with a period.
each turn the program outputs either r, b, g or y to the current pattern
(output should look like this:
turn1: y
2: yg
3:ygy
4:ygyr
etc.
but we are supposed to use the line cout<<"\010."<<flush; so that each letter dissapears after one second (hence the memory part of the game).
what package do i need to include so that this works properly because right now it is just outputing a period instead the the letter and replacing it with a period.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
you need to use an os-specific function to delay the program a bit inside the loop. If you are using MS-Windows you can include windows.h and call Sleep function.
>>cout<<"\010."<<flush;
Thats poorly written. flush is not normally needed. Below is normally how to move the cursor back to the beginning of the line so that succeeding output will overwrite existing characters on the screen.
Here is a complete example
>>cout<<"\010."<<flush;
Thats poorly written. flush is not normally needed. Below is normally how to move the cursor back to the beginning of the line so that succeeding output will overwrite existing characters on the screen.
cout << '\r';
Here is a complete example
#include <windows.h>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i < 10; i++)
{
cout << '\r' << setw(2) << right << i;
Sleep(1000);
}
} Last edited by Ancient Dragon : Oct 6th, 2007 at 10:54 pm.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
I don't know about the output thing you posted -- I have no idea that how to generate it. But in *nix exclude windows.h and intead of Sleep you can use the *nix version sleep or usleep
>>they are having us use PUTTY
Uggggly! That's just insane and makes me want to puke. Are you sure you are taking a c++ course ? What c++ compiler are you using that does not support cout ?
>>they are having us use PUTTY
Uggggly! That's just insane and makes me want to puke. Are you sure you are taking a c++ course ? What c++ compiler are you using that does not support cout ?
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
it supports cout. the cout<<char<<"\010."<<flush line was suposed to make the output look like that. it was supposed to output a char, then change it to a period and output the next one, but it isnt. its just outputting periods instead of changing the chars to periods after one second. the whole thing is really wierd tho. i might just use the way you told me with the rewritting the begining character.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
well, since I can't see the code on your monitor then you'll just have to post it here if you expect me to help you
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
//simon program hw7
//Matthew Gerton
//oct 6 07
//Matthew Gerton
//oct 6 07
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <time.h> using namespace std; int main() { //declares the two char arrays, turn counter, ... char seq[20], seq1[20]; int turn=1; char k; bool match=true; //seeds the random generator srand((unsigned int)(time(0))); //randomly fills the first array with r, b, g, and y for(int i=0; i<20; i++) { unsigned int random = rand() % 4 + 1; if(random==1) seq[i]='r'; if(random==2) seq[i]='b'; if(random==3) seq[i]='g'; if(random==4) seq[i]='y'; } //while loop iterates for up to 20 turns while(turn<=20&& match==true) { //this is the section that my question is about. it outputs as many chars relevant to what turn it is. //its supposed to output one, wait one second and then change it to a period and output the next one //but it instead just prints out periods for(int a=0; a<turn; a++) { cout<<seq[a]<<"\010."<<flush; } //gets the users recolection of the sequence and assigns it to the second array. for(int b=0; b<turn; b++) { cin>> k; seq1[b]=k; } //compares the arrays to make sure they are equal. if not, you loose for(int d=0; d<turn; d++) { if(seq[d]!=seq1[d]) { match=false; cout<<"you loose"<<endl; } } turn++; //if you last 20 turns, you win if(turn==20)cout<<"you win"<<endl; } return 0; }
Last edited by Ancient Dragon : Oct 6th, 2007 at 11:42 pm. Reason: add code tags -- please use then in the future
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
try this: (replace lines 30-33 with below)
cout << '\r';
for(a = 0; a < turn-1; a++)
cout << '.';
cout << seq[a] << '(';
for(a = 0; a<turn; a++)
{
cout << seq[a];
}
cout << ')';•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
//simon program hw7
//Matthew Gerton
//oct 6 07
the grader for the class lives in the dorm unit next to me and he finally got back. i talked to him and he fixed it. this is the correct code for the game. try playing it. its pretty nice
//Matthew Gerton
//oct 6 07
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <time.h> #include <unistd.h> using namespace std; int main() { //declares the two char arrays, turn counter, ... char seq[20], seq1[20]; int turn=1; char k; bool match=true; //seeds the random generator srand((unsigned int)(time(0))); //randomly fills the first array with r, b, g, and y for(int i=0; i<20; i++) { unsigned int random = rand() % 4 + 1; if(random==1) seq[i]='r'; if(random==2) seq[i]='b'; if(random==3) seq[i]='g'; if(random==4) seq[i]='y'; } //while loop iterates for up to 20 turns aslong as you haven't been wrong yet while(turn<=20&& match==true) { //outputs one character, waits one second and then change it to a period and outputs the next one cout<<"Sequence: "; for(int a =0; a<turn; a++) { cout<<seq[a]<<flush; sleep (1); cout << '\010' << '.' << flush; } cout<<endl; //gets the users recolection of the sequence and assigns it to the second array. for(int b=0; b<turn; b++) { cout<<"What was the sequence?"; cin>> k; seq1[b]=k; } //clears the screen after each turn to prevent cheating cout<<"\014"<<flush; //compares the arrays to make sure they are equal. if not, you loose and it displays the correct sequence //also displays what you entered for(int d=0; d<turn; d++) { if(seq[d]!=seq1[d]) { match=false; cout<<"you loose"<<endl; cout<<"The correct sequence was: "; for(int e=0; e<turn; e++) cout<<seq[e]; cout<<endl<<"You put: "; for(int f=0; f<turn; f++) cout<<seq1[f]; } } turn++; //if you last 20 turns, you win if(turn==20) cout<<"you win"<<endl; } cout<<endl; return 0; }
Last edited by Ancient Dragon : Oct 7th, 2007 at 6:37 am. Reason: add code tags
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Why am I getting this output (C++)
- Capturing Console Output (C)
- Reverse Output (Stack) (C++)
- Application Output Help (Java)
Other Threads in the C++ Forum
- Previous Thread: Need Help!!!!!
- Next Thread: Help with rand please.



Linear Mode