Thread: How would you go back to a section of code?

  1. #16
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by Sean
    I was going to draw Hello World with the geometry functions, but that would just be overkill, now wouldn't it?
    I guess it does
    What is C++?

  2. #17
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Smile hmm

    Hey,

    I'm still very new to C/C++ but I took a shot at it.. if any experts notice its extremely inefficient, which it probably is then just tell the maker to disregard the code. So.. this is what I came up with,

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    int NUMBER = 89; //the number to be guessed.
    int i;
    int a[4]; //number of tries +1
    
    cout<<"You've got three trys.\n";
    
    for(i = 1; i <= 4; i++)
    {
        if ( i < 4) {
    	cin>>a[i];
    	}
    	
    	if(a[i]==NUMBER) {
    		cout<<"Wow, you've gotten it right!\n";
    		break;
    	} else {
    
    		if(i == 1) {
    			cout<<"First try\n";
    		} else if (i == 2) {
    			cout<<"Second try\n";
    		} else if (i == 3) {
    			cout<<"Third try\n";
    		} else {
    			cout<<"Game over\n\n";
    		}
    
    	}
    
    }
    
    cout<<"Thats all";
    cin.get();
    
    }
    Thats sorta what some were talking about with the while statement, but I used for, and some ifs.. its probably way longer than it needs to be.. I just started typing it so Id have to think about the possible concepts, if it were my script.

  3. #18
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi Dae,

    I worked through this one last night. Like you, I used a for loop and a whole mess of ifs (though my for loop is a little different than yours).

    I won't post my code unless XenoForce says it's ok though. I really like figuring out an assignment (even if it is only from a book or off the net) on my own and if he's the same he may not want to see too much.

    Thanks for posting this XenoForce. There's a lot of great stuff going on in this forum but a lot of it is over my head still. It's nice to be able to read stuff that beginners can get in on a little.

  4. #19
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    As has been mentioned, use a loop instead of goto. It's better

    Code:
    bool correct = false;  //Did they get it right?
    int tries = 0;		 //How many times have they tried?
    int guess;		  //What did they guess?
     
    while(tries < 3 && !correct)  //Will loop until tries is 3 or correct is true
    {
       cout << "Enter number: ";
       cin >> guess;
       if(guess == 12345)  //or whatever number you want
       {
    	  cout << "You got it right!" << endl;
    	  correct = true;  //end the loop
       }
       else
       {
    	  cout << "Wrong, try again." << endl;
    	  tries = tries + 1;  //increase the counter
       }
    }
    Last edited by Hunter2; 10-12-2004 at 10:08 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #20
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Hunter2
    As has been mentioned, use a loop instead of goto. It's better

    Code:
    bool correct = false;  //Did they get it right?
    int tries = 0;		 //How many times have they tried?
    int guess;		  //What did they guess?
     
    while(tries < 3 && !correct)  //Will loop until tries is 3 or correct is true
    {
       cout << "Enter number: ";
       cin >> guess;
       if(guess == 12345)  //or whatever number you want
       {
    	  cout << "You got it right!" << endl;
    	  correct = true;  //end the loop
       }
       else
       {
    	  cout << "Wrong, try again." << endl;
    	  tries = tries + 1;  //increase the counter
       }
    }
    Pretty nice, .. now if you replaced the "Wrong, try Again" line with some if statement, you could have it say the whole "First try, Second try, Third try", thing.

    Hi Elhaz,

    Yeah I figured as much.. I try not to give full source code, but considering mine was competely inefficient I figured it needed enough touchups as it is but got the idea through on how to do it (ie, an example). If people want to do it themselves, they can look away, or just get the concept and try their own ways without copying it.. so if you truelly want to do it yourself, you can, eh
    Last edited by Dae; 10-12-2004 at 05:00 PM.

  6. #21
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hunter2,

    That while is loads better than my for loop. I never woulda thought of using the bool. Cool.

    Dae,

    If people want to do it themselves, they can look away...
    Ya, I suppose you're right. But it's sooo hard to look away!

  7. #22
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Thanks a bunch!!! By the way, how do you make pauses at the end so the user can see the end product?

  8. #23
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Either use
    system("PAUSE");
    or
    cin.get();

    to use the first include stdlib.h
    to use second im not exactly sure if you have to include anything because my teacher prefers the first.
    Last edited by Siggy; 10-20-2004 at 10:47 PM.

  9. #24
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    >> to use second im not exactly sure if you have to include anything because my teacher prefers the first.

    Hm, why does your teacher prefer this?

    I would stronly recomend the second method. It is in the iostream header under the STD namespace

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    
        cout << "Hit enter to quit!";
    
        cin.get ();
        return 0;
    
    }
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with section of code.
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2005, 06:38 PM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  3. how to loop back for this code ?
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 12:40 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM