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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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;
      }
      
      else if (userguess>number){
      
         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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdlib>
#include <ctime>
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)"<<endl<<endl;
    for(int i=0;i<6;i++)
    {
        cout<<"Guess #"<<i+1<<": ";
        getline(cin,guess);
        value = atoi(guess.c_str());
        if(value == myValue)
        {
            cout<<"You guess right! are you a psychic?"<<endl;
            break;
        }
        else
            cout<<"Nope, try again!"<<endl;
        cout<<endl;
    }
    if(value!=myValue)
        cout<<"Game over, my number was: "<<myValue<<endl;
    cout<<endl;
    cout<<"Custom Exit: Press enter: ";
    cin.get();
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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";
		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:
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; 
You would have to pass in an int pointer. Is that allowed?


Actually you don't even need to do that
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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.