User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 6
Reputation: imonriddilin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imonriddilin imonriddilin is offline Offline
Newbie Poster

gradually hidding an output

  #1  
Oct 6th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: gradually hidding an output

  #2  
Oct 6th, 2007
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);
   }

}
Last edited by Ancient Dragon : Oct 6th, 2007 at 10:54 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: imonriddilin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imonriddilin imonriddilin is offline Offline
Newbie Poster

Re: gradually hidding an output

  #3  
Oct 6th, 2007
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
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: gradually hidding an output

  #4  
Oct 6th, 2007
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 ?
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: imonriddilin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imonriddilin imonriddilin is offline Offline
Newbie Poster

Re: gradually hidding an output

  #5  
Oct 6th, 2007
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: gradually hidding an output

  #6  
Oct 6th, 2007
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
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: imonriddilin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imonriddilin imonriddilin is offline Offline
Newbie Poster

Re: gradually hidding an output

  #7  
Oct 6th, 2007
//simon program hw7
//Matthew Gerton
//oct 6 07
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main()
  5. {
  6. //declares the two char arrays, turn counter, ...
  7. char seq[20], seq1[20];
  8. int turn=1;
  9. char k;
  10. bool match=true;
  11.  
  12. //seeds the random generator
  13. srand((unsigned int)(time(0)));
  14.  
  15. //randomly fills the first array with r, b, g, and y
  16. for(int i=0; i<20; i++)
  17. {
  18. unsigned int random = rand() % 4 + 1;
  19. if(random==1) seq[i]='r';
  20. if(random==2) seq[i]='b';
  21. if(random==3) seq[i]='g';
  22. if(random==4) seq[i]='y';
  23. }
  24. //while loop iterates for up to 20 turns
  25. while(turn<=20&& match==true)
  26. {
  27. //this is the section that my question is about. it outputs as many chars relevant to what turn it is.
  28. //its supposed to output one, wait one second and then change it to a period and output the next one
  29. //but it instead just prints out periods
  30. for(int a=0; a<turn; a++)
  31. {
  32. cout<<seq[a]<<"\010."<<flush;
  33. }
  34. //gets the users recolection of the sequence and assigns it to the second array.
  35. for(int b=0; b<turn; b++)
  36. {
  37. cin>> k;
  38. seq1[b]=k;
  39. }
  40. //compares the arrays to make sure they are equal. if not, you loose
  41. for(int d=0; d<turn; d++)
  42. {
  43. if(seq[d]!=seq1[d])
  44. {
  45. match=false;
  46. cout<<"you loose"<<endl;
  47. }
  48. }
  49. turn++;
  50. //if you last 20 turns, you win
  51. if(turn==20)cout<<"you win"<<endl;
  52.  
  53. }
  54. return 0;
  55. }
Last edited by Ancient Dragon : Oct 6th, 2007 at 11:42 pm. Reason: add code tags -- please use then in the future
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: gradually hidding an output

  #8  
Oct 7th, 2007
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 << ')';
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: imonriddilin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imonriddilin imonriddilin is offline Offline
Newbie Poster

Re: gradually hidding an output

  #9  
Oct 7th, 2007
it gave me:
line 37: error: name lookup of `a' changed for new ISO `for' scoping
line 35: error: using obsolete binding at `a'
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: imonriddilin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imonriddilin imonriddilin is offline Offline
Newbie Poster

Re: gradually hidding an output

  #10  
Oct 7th, 2007
//simon program hw7
//Matthew Gerton
//oct 6 07
  1. #include <iostream>
  2. #include <time.h>
  3. #include <unistd.h>
  4. using namespace std;
  5. int main()
  6. {
  7. //declares the two char arrays, turn counter, ...
  8. char seq[20], seq1[20];
  9. int turn=1;
  10. char k;
  11. bool match=true;
  12.  
  13. //seeds the random generator
  14. srand((unsigned int)(time(0)));
  15.  
  16. //randomly fills the first array with r, b, g, and y
  17. for(int i=0; i<20; i++)
  18. {
  19. unsigned int random = rand() % 4 + 1;
  20. if(random==1) seq[i]='r';
  21. if(random==2) seq[i]='b';
  22. if(random==3) seq[i]='g';
  23. if(random==4) seq[i]='y';
  24. }
  25. //while loop iterates for up to 20 turns aslong as you haven't been wrong yet
  26. while(turn<=20&& match==true)
  27. {
  28. //outputs one character, waits one second and then change it to a period and outputs the next one
  29. cout<<"Sequence: ";
  30. for(int a =0; a<turn; a++)
  31. {
  32. cout<<seq[a]<<flush;
  33. sleep (1);
  34. cout << '\010' << '.' << flush;
  35. }
  36. cout<<endl;
  37. //gets the users recolection of the sequence and assigns it to the second array.
  38. for(int b=0; b<turn; b++)
  39. {
  40. cout<<"What was the sequence?";
  41. cin>> k;
  42. seq1[b]=k;
  43. }
  44.  
  45. //clears the screen after each turn to prevent cheating
  46. cout<<"\014"<<flush;
  47.  
  48. //compares the arrays to make sure they are equal. if not, you loose and it displays the correct sequence
  49. //also displays what you entered
  50. for(int d=0; d<turn; d++)
  51. {
  52. if(seq[d]!=seq1[d])
  53. {
  54. match=false;
  55. cout<<"you loose"<<endl;
  56. cout<<"The correct sequence was: ";
  57. for(int e=0; e<turn; e++)
  58. cout<<seq[e];
  59. cout<<endl<<"You put: ";
  60. for(int f=0; f<turn; f++)
  61. cout<<seq1[f];
  62. }
  63. }
  64. turn++;
  65. //if you last 20 turns, you win
  66. if(turn==20)
  67. cout<<"you win"<<endl;
  68. }
  69. cout<<endl;
  70. return 0;
  71. }
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
Last edited by Ancient Dragon : Oct 7th, 2007 at 6:37 am. Reason: add code tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC