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.

Recommended Answers

All 12 Replies

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.

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);
   }

}

they are having us use PUTTY, which runs on linux.
and they want the output to be something like this
(this is in one turn)
y
.r(yr)
..g(yrg)
...g(yrgg)
not just rewritting the begining character in the output

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 ?

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.

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 :)

//simon program hw7
//Matthew Gerton
//oct 6 07

#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;
}

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 << ')';

it gave me:
line 37: error: name lookup of `a' changed for new ISO `for' scoping
line 35: error: using obsolete binding at `a'

//simon program hw7
//Matthew Gerton
//oct 6 07

#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;
}

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

Member Avatar for iamthwee

There's still something not right with your loops.

it gave me:
line 37: error: name lookup of `a' changed for new ISO `for' scoping
line 35: error: using obsolete binding at `a'

oops. you have to declare a, see line 2 below

cout << '\r';
int a;
for(a = 0; a < turn-1; a++)
    cout << '.';
cout << seq[a] << '(';
for(a = 0; a<turn; a++)
{
   cout << seq[a];
}
cout << ')';

they are having us use PUTTY, which runs on linux.
and they want the output to be something like this
(this is in one turn)
y
.r(yr)
..g(yrg)
...g(yrgg)
not just rewritting the begining character in the output

PuTTY is not a compiler, it is a free software SSH, Telnet, rlogin, and raw TCP client, you can read more here... You can donwload a windows version here.... If you don't like it {like me...} you can do the following things::

You are on windows:: use an alternative program like winscp {its free}....

You are on linux:: {i am not sure} but i think there is native support for remote ssh connections, so you can create the code on your machine and upload it in the university server....{all this apply to ubuntu, thats why i am not so sure about other distros..}

Usually universities do this thing with putty, so that everyone can work in a common tool{eg compiler}. So everyone writes his code but in the end it must compile with the compiler the university picked... Where i study we had this system on C, C++ courses and also in "compiler construction" courses using bison and flex...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.