Exercise attempt bugs, please help!!!

So I have learned a few things about c++ in the past week and have decided to try an exercise with what I've learned and make a number guessing game.

At times the game runs fine and gives me my Win message but at other times it'll just stop letting me input numbers even if i havent guessed correctly or ran out of tries. Also sometimes if i do guess correctly it won't count it as correct. Please help debug this!! I want to learn from my mistakes!!!

Please be specific on how to fix ^_^

// Note: I use y as a way to break the loop.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int x;
int y;
int t = 1;

int main(){
srand(time(0));

    cout << "Ten tries to guess what number I'm thinking of...\n But it changes each guess!!! \n It's 1 - 10" << endl;
    cout << "_________________________________________________" << endl;

    cin >> x;
        if(x == 1+(rand()%10)){
    cout << "Lucky guess >_> You win" << endl;
    cout << "_________________________________________________" << endl;
            y = 11;};

            while(t<=10 && y < 10 && x!= 1+(rand()%10)){
        cout << "Try again! It was " << 1+(rand()%10) << "!" << endl;
        cin >> x;
        t++;
                if(x == 1+(rand()%10)){

            cout << "Lucky guess >_> You win" << endl;
            cout << "_________________________________________________" << endl;

            y = 11;
            }       else if(t==11){
                cout << "Out of tries punk ass Dragon boy" << endl;}
        }

}

Last edited on
each time you do this:
rand() % 10
you'll get a different number back. it looks like you'd want the same number on both lines 18 and 23.

when i see duplicate code, i.e.
1
2
  cout << "Lucky guess >_> You win" << endl;
    cout << "_________________________________________________" << endl;

then that indicates you could probably refactor this to be in one loop.

I also think that this:
while(t<=10 && y < 10 && x!= 1+(rand()%10)){
is over complicated. Surely you just want to loop ten times or until the user gets the right number?

Please be specific on how to fix

No, because you also say this:
I want to learn from my mistakes!


:)
Oh cmon help me out here! Lmao

okay so I need to find a way to contain it all into one loop that'll allow the first guess to be the same as any other guess and randomly created number. I'm not sure how I'm honestly not that advanced at all yet. I've been learning for a few days now. I need to know how to fix it so I can learn from that!
here's a simplified example. I've taken the random stuff out and assumed rand will return 4, but you'll get the idea i hope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
	const int maxNumberOfTries(10);
	bool guessed(false);

	for (int i = 0; i < maxNumberOfTries && !guessed; i++)
	{
		int number(0);
		std::cout << "input: ";
		std::cin >> number;

		if (number == 4)
		{
			std::cout << "well done";
			guessed = true;
		}
	}

	return 0;
}
Simplify. Use break

LOOP 10 times
  set answer
  get guess
  IF guess == answer
  THEN you win, break;
  ELSE try again

IF did not win
THEN mock

IF did not win
THEN mock

lol :) I need that printed on a t-shirt.
Ookay i seriously cannot figure this out. Can someone please just edit my current code >_<
i dont know what to do
jesus, i've given a example of the loop you need.

edit: one thing about rand: you should call it once per loop and then store this value in a variable. So if you need to test the number again you know you're using the same number.
Last edited on
I'll just mention how brand spanking new I am to this. I've been teaching my self for the past 6 days or so hahaha. Go ahead and MOCK! So I understand what is needed but I can't figure out how to go about it. Like I don't know what "bool" is yet or "const". I'm like level 1 noob. Which is why I kinda just want my code straight up fixed in the simplicity that it's in so I can learn what and how to go about certain things before moving on.
Edit: I want to master the little bit of knowledge I have in practice like this mini game before using stuff I haven't seen yet ^_^
Last edited on
i'm not mocking. i've given you an example and basically you need to plug in your random stuff.

if you don't know data types like bool yet i suggest you look here:
http://www.cplusplus.com/doc/tutorial/variables/
in fact, you might want to skim through sections "Basics of C++" and "Program structure".

(const is just a way of telling the compiler you never intend to change the value of a variable, which stops a programmer accidentally changing it in the code as you'll get a compiler error).

This is almost the same as mutexe's version:
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
#include <iostream>

int main()
{
  const int maxNumberOfTries {10};
  const int answer {4}; // this the user should guess
  bool guessed {false}; // by default, the user has guessed wrong

  // simpler condition on the loop
  for ( int i = 0; i < maxNumberOfTries; ++i ) {
    int number {0};
    std::cout << "input: ";
    std::cin >> number;

    if ( answer == number ) {
      // no output here, just store that guess was correct
      guessed = true;
      // terminate loop prematurely
      break;
    }
    else if ( 1 + i < maxNumberOfTries ) {
      // on all but the last loop iteration
      std::cout << "No, " << answer << " != " << number << ", try again\n";
    }
  }

  // Now we know whether a guess was right or all times failed.
  // Print appropriate message
  if ( guessed ) {
    // won, congratulate
  }
  else {
    // lost, tell that game is over
  }
  return 0;
}

This also hints that one can do things in many ways. Some of the syntax requires C++11 support from the compiler. Depends on your compiler whether it has support and has it enabled by default.
Topic archived. No new replies allowed.