want to play again?

I made a text based game where the player tries to guess a pre set number. That works fine. At the end there is a 'want to play again'? type thing. The main code is in a while loop so that should wrok. It did. Then I added some other stuff. An error message, which sould be if again doesn't equal upper or lower case y or n, it makes you try again. Instead, it always takes me to the error message and even when I enter upper or lower case n or y, it repeats the message. Before that, even the'y's would end the program, not restart it. Any help would be nice.

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
66
67
68
69
70
71
72
73
74
75
  //Guess my number
//An originalish game by Calvin

#include<iostream>
#include<windows.h>
#include<cstdlib>
#include<ctime>
#include <string>

using namespace std;

string again;

int guess = 0; //this is the guess

const int numb = 8; //this is the number that try to guess

int main()
//start of main int
{
//its in a wile so they can play again
    while (again == "y" || "Y")
    //start of while loop
    {


    cout << "Hello, and wellcome to 'Guess my Number'! I am your host, CPU, and you have \nthree trys to guess my munber. It is between 1 and 10." << endl;

    cout << "\nGuess number 1: ";
    cin >> guess;
    if (guess == numb) //first guess if
        cout << "\nCorrect! Congratulations!" << endl;
    else
    {   //start of else 1
        cout << "I am sorry, that is incorrect. Please try again." << endl;
        cout << "\nGuess number 2: ";
        cin >> guess;
        if (guess == numb)//third guess if
            cout << "\nCorrect! Congratulations!" << endl;
        else
        { //start of else 2
            cout << "I am sorry. Please try again." << endl;
            cout << "\nFinal guess: ";
            cin >> guess;
            if (guess == numb) //third guess if
                cout << "\nCorrect! Congratulations!" << endl;
            else
                cout << "I am sorry. That was incorrect." << endl;
        } //end of else 2

    } //end of else 1
        cout << "\nDo you want to play again? (y/n): ";
        cin >> again;

        if (again == "n")
                    break;

        else
        {

            if (again != "n" || "y")
               {
                while (again != "n" || "y")
                {cout << "Error. Please enter a valid answer.\n";
                cin >> again;}}}


    }  //end of while loop
    cout << "Thank you for playing. Please come again!\n" << endl;

    cin.get();
    return 0;

} //end of main int
The first statement of main is already invalid

while (again == "y" || "Y")

First of all this expression is alway true because "Y" is not equal to zero. The correct condition should look as

while (again == "y" || again == "Y")

But if you will write the correct condition the loop will not be executed because the initial value of again is an empty string.

Also it is not clear and there is no any need to define again as a global variable.

(again != "n" || "y")
should be
(again != "n" && again != "y")
Also single quotes (char).
Topic archived. No new replies allowed.