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!
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
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 (userguess<number){
cout<<"\nToo Low!\n";
correct=0;
return correct;
}
elseif (userguess>number){
cout<<"\nToo High!\n";
correct=0;
return correct;
}
else{
cout<<"\nCongratulations, you have guessed the correct number!\n";
correct=1;
return correct;
}
}
#include <iostream>
#include <time.h>
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";
returnfalse;
}
elseif ( userguess > number )
{
std::cout << "\nToo High!\n";
returnfalse;
}
else
{
std::cout << "\nCongratulations, you have guessed the correct number!\n";
returntrue;
}
}
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
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.
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:
1 2 3 4 5 6 7 8
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: "<<number<<endl;