Thread: Repeat Code

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10: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, 05:06 PM