I guess it doesOriginally Posted by Sean
I guess it doesOriginally Posted by Sean
What is C++?
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,
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.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(); }
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.
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.
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.Originally Posted by Hunter2
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.
Hunter2,
That while is loads better than my for loop. I never woulda thought of using the bool. Cool.
Dae,
Ya, I suppose you're right. But it's sooo hard to look away!If people want to do it themselves, they can look away...
Thanks a bunch!!! By the way, how do you make pauses at the end so the user can see the end product?
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.
>> 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++?