Dice Game

What did i do wrong?

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

using namespace std;






int main()
{
    string answer;

    cout << "Are You Ready For A Dice Game?" << endl;
    cin >> answer;
    if (answer == yes)
    {
        cout << "Let's Begin!" << endl;
    }
        else
        {
            cout << "Goodbye!" << endl;
        }
}
answer is a string variable. yes is what? If you want to check if answer is equal to "yes" you should put double quotes around the word.

 
if (answer == "yes")
ok so next question. if the user says no how do i stop the "Roll The Dice" from coming up

for example the user says no...i want the program to end after it says goodbye.

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

using namespace std;

int main()
{
    string answer;
    string roll;

    cout << "Are You Ready For A Dice Game?" << endl;
    cin >> answer;
    if (answer == "yes")
    {
        cout << "Let's Begin!" << endl;
    }

        else
        {
            cout << "Goodbye!" << endl;
        }

cout << "Roll The Dice" << endl;
cout << "Type ROLL To Roll The Dice" << endl;
cin >> roll;
    if (roll == "ROLL")
    {
        cout << "You've rolled the Dice" << endl;
    }

        else
        {
            cout << "Try Again!" << endl;
        }



}
A few ways to do this.

1) move lines 22-33 to after line 14.
2) Add return 0; after line 19.
3) Move lines 22-33 to a function and call that function after line 14.
Last edited on
well considering what i kno about c++ i think i did an amazing job. what do you think?

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

using namespace std;

int main()
{
    string answer;
    string roll;

    cout << "Are You Ready For A Dice Game?" << endl;
    cin >> answer;
    if (answer == "yes")
    {
        cout << "Let's Begin!" << endl;
    }

        else
        {
            cout << "Goodbye!" << endl;
            exit(0);
        }

cout << "Roll The Dice" << endl;
cout << "Type (ROLL) To Roll The Dice" << endl;
cin >> roll;
    if (roll == "ROLL")
    {
        cout << "You've Rolled A " << endl;
    }

        else
        {
            cout << "Try Again!" << endl;
            exit(0);
        }

    srand(time(0));
        for (int x = 1; x<=1; x++)
        {
            cout << 1+(rand()%12) << endl;
        }


}
I'm not fond of the way you have structured your if statements:
1
2
3
4
5
6
7
8
  if (some_condition) 
  {  // do something
      //  fall through
  }
  else
  {  // output message
     exit(0);
  }


IMO, it's much cleaner to put the error/exit branch under the then portion of the if.
1
2
3
4
5
6
  if (! some_condition)  // Note the use of the NOT
  {  // output error message
      return 0;  //  exit the program
  }
  // some_condition is true.  no else required
  //  continue with program 


Topic archived. No new replies allowed.