Repeat Code

This is a discussion on Repeat Code within the C++ Programming forums, part of the General Programming Boards category; Don't make fun of me because my question seems kind of juvenile to you...I started learning C++ maybe three days ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    Repeat Code

    Don't make fun of me because my question seems kind of juvenile to you...I started learning C++ maybe three days ago, so....

    With that said, I'm having a problem with a "game" I'm trying to create where the object is to guess a number between 1 and 999. However, if one guesses wrong, the game is *supposed* to go back to the begenning and ask you to guess again. Here's the code.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    	int a = rand()%999;
    
    int main()
    {
    	int b;
    	int a;
    	int x = 1;
    
    	cout<<"See if you can crack the code!"<<"\n";
    	cout<<"Guess #"<< x <<": ";
    	cin>> b;
    	cin.ignore();
    	x++;
    	if (b>a) 
    		{
    		cout<<"Too high, try again...";
    		int main();                                                                  //here
    		}
    	if (b<a) 
    		{
    		cout<<"Too low, try again...";
    		int main();                                                                  //here
    		}
    	if (b == a) 
    		{
    		int win();                                                                   //here
    		}
    }
    
    int win(void)
    
    {
    	char * y;
    
    	cout<<"Correct!";
    	cout<<"Try to break another code?" "Y or N";
    	cin>> y;
    	if (y == "Y")
    		{
    		int main();                                                                   //here
    		}
    }
    As you can see, all the highlited places are where I want it to go. I thought about using the goto command, but from what I read (here, of course) I decided that the goto command was not what I wanted. If you could give me a hand, it would be much appreciated.
    Last edited by Iceboy152; 02-24-2006 at 07:00 PM.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You're calling a function in the wrong way, you don't write it's return type when calling them. And don't call main().
    What you wan't to do is wrap your code inside a loop that doesn't end until a == b.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You are not supposed to recursively call int main. Instead make a while loop like this
    Code:
    while (b != a)
    {
         check too high/low
    }
    win();

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    The highlighted parts weren't what I actually put in my code (i had no idea how to do this). They were only meant to show where I wanted the code to go from there. As for your other statement, I looked at loops, but I don't know how to write that entire function into the loop. Heck, I don't even know which loop to use ( for, do until, etc)

    [EDIT] Oops, ignore that last sentence. Thanks

    [EDIT2] Okay, but what do you mean "check high/low"...the loop already states that b != a, or else it would not be in effect
    Last edited by Iceboy152; 02-24-2006 at 07:11 PM.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    He means the (a < b) and (a > b) if-statements that prints out whether the number is too high or too low.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    I *think* I get what you're saying, but now the prompt is throwing error #E2120--Cannot call "main" from within the program in function main().

    Also, it's saying function win() is undefined.....

  7. #7
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You're not supposed to call main(). You will have to find another way around it e.g. a loop. This means you will have to put your first loop inside another loop that doesn't exist until the user says that he doesn't want to play anymore.
    win() is not declared, you're calling the function before the compiler knows how it looks like so you have to put a prototype of it at the top.
    Code:
    int win (void);

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    I realize you've probably said this two or three times already, and I apologize, but I just don't get it. I understand that i need to write a while loop. The part i don't understand is how to get back to function main without calling it. I appreciate your patience in this.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    GAAH! I changed a few things, and it worked, except now i'm getting a new error...the prompt tells me there is a syntax error on line 9:

    int main()

    is there??

  10. #10
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Well, written explanations are usually harder to grasp than a visual demonstration so I fixed your program to a working state and put out some comments and I hope it will help.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <ctype>
    
    using namespace std;
    
    bool win (void); //Function prototype.
    
    int main () //Remember to use good variable names if applicable!
    {
    	int number; //Originally "a". Also a local variable this time, don't use globals.
    	int guess; //Originally "b".
    	int guesscount; //Originally "x".
    
    	srand(time(0)); //Give rand() a seed so we actually get a random number.
    	do { //The main loop.
    		guesscount = 1;
    		number = rand()%999 + 1; //rand()%999 gives a number between 0 and 998 instead of 1 and 999.
    		cout << "See if you can crack the code!" << endl;
    
    		do { //The other loop.
    			cout << "Guess #" << guesscount++ << ": " << flush; //Notice the ++ after guesscount.
    			cin >> guess;
    			cin.ignore();
    
    			if (guess > number)
    				cout << "Too high, try again..." << endl;
    			else if (guess < number)
    				cout << "Too low, try again..." << endl;
    		} while (guess != number);
    	} while (win()); //If the user wants to continue win() returns true and the loop continues.
    }
    
    bool win (void)
    {
    	char answer; //Originally "y".
    
    	cout << "Correct!" << endl //I am only making one call to cout here.
    	     << "Try to break another code? Y or N: " << flush; //If I'm not ending a cout with endl then I end it with flush.
    	cin >> answer;
    
    	if (tolower(answer) == 'y')
    		return true;
    	//"else"
    		return false;
    }

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    Much obliged!

    [EDIT] Idle curiosity....what do the inclusions "ctime" and "ctype" do?
    Last edited by Iceboy152; 02-24-2006 at 08:05 PM.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Iceboy152
    [EDIT] Idle curiosity....what do the inclusions "ctime" and "ctype" do?
    Since the code uses functions such as time and tolower it's best to let the compiler know how to use them properly, so it can tell you when you're not.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21