Jump to content

Problem with Guess the number in C++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Azrian

Azrian

    Newbie

  • Members
  • Pip
  • 9 posts
Alright, I just switched to C++ (started today!) and tried making a guess the number game, but I'm encoutering one little problem (oh and I'm aware that for some reason that I don't know using main() to restart the program is baaaaad but that's the only way I know to do it), when you finish one game and it asks you if you want to play again, no matter what you type it will always restart :thumbdown:

I'm faily sure this is easy to fix and would really appreciate some pointers! Thanks! :D

// Fait par Sam le Roux =D
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int main() {
	cout << "Programme fait en C++ par Sam le roux :D\n";
	int tries,guess,secret;
	string play;
	srand ( time(NULL) );
	secret = rand() % 51;
	tries = 0;
	cout << "Guess the number!\n" << "It's between 1 and 50, you have 5 tries.\n";
	do {
			cout << "What's your guess?: ";
			cin >> guess;
				if(guess>secret) {
					cout << "Your guess is too high!\n";
					tries++;
				}
				else if(guess<secret) {
					cout << "Your guess is too low!\n";
					tries++;
				}
				else if(guess==secret) {
					guess = 0;
					cout << "You won! Congratulations!\n";
					cout << "Play again? (Y/N): ";
					cin >> play;
					if(play == "Y");
						main();
				}
	}while(tries<5);
	if(tries>=5) {
		cout << "You lost! The number was: " << secret << "\n";
		cout << "Play again? (Y/N): ";
		cin >> play;
		if(play == "Y");
			main();
	}
	return 0;
}


#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
I would put the if (play == "Y"); in the loop, like so;

	cout << "Do you want to move again? Y or N only please.\n";
	cin >> again;
} while (again == 'Y');

This iswhat I did on somehting i've made, and it works^^ (also, that gets rid of the main(); being there)
Posted Image

#3
Azrian

Azrian

    Newbie

  • Members
  • Pip
  • 9 posts
I'm still having some problems, I can get the whole program to work but I can't get it to work with a Yes/No answer for the play again prompt :( I can with integers though (1 and 0 in this case).

Edited by Azrian, 18 February 2010 - 07:27 PM.


#4
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Here is the modified program....

#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int main() {
    cout << "Programme fait en C++ par Sam le roux :D\n";
    int tries,guess,secret;
    string play;
    srand ( time(NULL) );
    secret = rand() % 51;
    tries = 0;
    cout << "Guess the number!\n" << "It's between 1 and 50, you have 5 tries.\n";
    do {
            cout << "What's your guess?: ";
            cin >> guess;
            if(guess>secret) {
                cout << "Your guess is too high!\n";
                tries++;
            }
            else if(guess<secret) {
                cout << "Your guess is too low!\n";
                tries++;
            }
            else if(guess==secret) {
                guess = 0;
                cout << "You won! Congratulations!\n";
                cout << "Play again? (Y/N): ";
                cin >> play;
                if(play == "Y" || play == "y")
                {
                    tries = 0;
                    cout << "Guess the number!\n" << "It's between 1 and 50, you have 5 tries.\n";
                    srand ( time(NULL) );
                    secret = rand() % 51;
                }
                else if(play == "N" || play == "n")
                {
                    tries = 6;
                }
            }
            if(tries == 5)
            {
                cout << "You lost! The number was: " << secret << "\n";
                cout << "Play again? (Y/N): ";
                cin >> play;
                if(play == "Y" || play == "y")
                {
                    tries = 0;
                    cout << "Guess the number!\n" << "It's between 1 and 50, you have 5 tries.\n";
                    srand ( time(NULL) );
                    secret = rand() % 51;
                }
                else if(play == "N" || play == "n")
                {
                    tries = 6;
                }
            }
    }while(tries<5);

    return 0;
}