### Need some help!

Hello,

I'm writing a little mini-game and I'm stuck at a point. What I want to do is add a loop that only allows the user to guess the number 6 times, and if they reach > 6 guesses output a message saying,
 `Sorry - you have run out of turns, the number was: //the number`
If they do not however go above, then print out a message stating,
 `Congratulations, you guessed correct after: //number of attempts`

I'm assuming the easiest way to do so is a simple for loop? If not, tell/show me your idea, please!

Thanks for the help!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include #include #include int RN (int); int CA (int, int, int); void main(){ int userguess, number, correct, n; srand(time(NULL)); number=(RN)(n); correct=0; while (correct==0){ cout<<"\nGuess a number between 1 and 100: "; cin>>userguess; correct=CA(userguess, number, correct); } } int RN (int n){ n=rand()%100+1; return n; } int CA (int userguess, int number, int correct){ if (userguessnumber){ cout<<"\nToo High!\n"; correct=0; return correct; } else{ cout<<"\nCongratulations, you have guessed the correct number!\n"; correct=1; return correct; } }``````
Last edited on
closed account (j3Rz8vqX)
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include using namespace std; int main() { srand(time(0)); string guess; int value, myValue=rand()%10; cout<<"You've got 6 tries to guess my number! (between 0 and 9)"<
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include bool CA ( int userguess, int number ); void main() { srand ( time ( NULL ) ); int guesses, userguess, number = rand ( ) % 100 + 1; bool correct = false; for ( guesses = 0; guesses < 6 && !correct; ++guesses ) { std::cout << "\nGuess a number between 1 and 100: "; std::cin >> userguess; correct = CA ( userguess, number ); } if ( correct ) std::cout << "\nCongratulations, you guessed correct after " << guesses + 1 << " guesses.\n\n"; else std::cout << "\nSorry - you have run out of turns, the number was: " << number << "\n\n"; } bool CA ( int userguess, int number ) { if ( userguess < number ) { std::cout << "\nToo Low!\n"; return false; } else if ( userguess > number ) { std::cout << "\nToo High!\n"; return false; } else { std::cout << "\nCongratulations, you have guessed the correct number!\n"; return true; } }``````
As much as I appreciate both of your responses, I have very strict guidelines to follow on this assignment. I need to keep the code I have to meet these requirements, and therefore cannot use the code you both supplied. Is there anyway to keep my code and write what I need?
You could add a static counter to CA and increment it each time the guess is wrong.
Then add an extra check that checks for an invalid amount of guesses
Last edited on
So you're saying it doesn't work right now, but you can't change your code to make it work? Well, what CAN you change about your code then?
 You could add a static counter to CA and increment it each time the guess is wrong or just a regular counter to main. Then add an extra check that checks for an invalid amount of guesses

Do you think you could show how I would do this? I would appreciate it.

 So you're saying it doesn't work right now, but you can't change your code to make it work? Well, what CAN you change about your code then?

Let me try to explain what I meant - I never said my code didn't work, rather I'm looking to find a way to add something into my code that I already have in place. All I want to do is add a segment that would allow for what I need, not a revamp of everything I've already written.
Last edited on
Yea i was wondering that myself what exactly are you not allowed to change
 Do you think you could show how I would do this? I would appreciate it.

You would have to pass in an int pointer. Is that allowed?
Last edited on
 You would have to pass in an int pointer. Is that allowed?

Yes.
closed account (j3Rz8vqX)
Not sure what you're interpreting, but we aren't telling you to use the functions/procedures we've supplied.

They were examples...

The key point was to let you see the for loop, delimited by a cycle of 6:

Example, with your case:
 ``12345678`` `````` for(int i=0;i<6 && correct==0;i++) { cout<<"\nGuess a number between 1 and 100: "; cin>>userguess; correct=CA(userguess, number, correct); } //if(correct==0){cout<<"Sorry - you have run out of turns, the number was: "<
 You would have to pass in an int pointer. Is that allowed?

Actually you don't even need to do that
Last edited on
 ``123456789101112131415161718192021222324252627282930`` ``````void main() { int userguess, number, correct, n; srand(time(NULL)); number = (RN) (n); correct = 0; int counter = 0; while (correct == 0) { std::cout << "\nGuess a number between 1 and 100: "; std::cin >> userguess; correct = CA(userguess, number, correct); counter += 1; if (counter > 5) { std::cout << "you failed."; break; } } }``````

Will this work?
Last edited on
By the way i realized you haven't declared the std namespace on any of your calls to cout or cin.

Dputs method would also work
Last edited on
 By the way i realized you haven't declared the std namespace on any of your calls to cout or cin. Dputs method would also work

I tried Dputs method along with yours, and had success with both of them. For this project however, I'm going to use the code you provided me.

Thank you everybody!

**Sorry Dputs and Yay295 for any confusion, I appreciate the help!
My method actually had an error I edited to fix
Last edited on
Topic archived. No new replies allowed.