Gentle guidance While, Switch, forever looping

Greetings all,

I'm very new to programming and I'm trying to self teach.

Currently I am trying to solve the following:

I am giving the user the ability to add points to some attributes. I want to give them a total of 5 points and let them have the freedom to select how many and which attributes to add the points.

The problem: After the user confirms the correct attribute the loop repeats infinitely, never making it to the if statement.

FYI, This is a portion of the code. I didn't want to post all of it because I don't want to waste your time trying to read all of the other crap.



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
int dex = 10;               //Starting dexterity
int str = 10;               //Starting strength
int intel = 10;             //Starting Intelligence
int cons = 10;              //Starting Constitution
int attriChoice;            //User input variable they select the attribute that they want to increase
char attriConfirm;          //User verification of the attribute that they want to modify
int attriBonus;             //Variable for the amount of points to be added to the attribute
int pointsAvail = 5;        //The number of points available to add to their attributes

while(pointsAvail > 0){
        cout << "You have " << pointsAvail << " attribute point(s) to distribute. \n \n";         //User chooses which attribute they want to modify
        cout << "Type the number corresponding to the attribute you wish to enhance. \n";
        cout << "1. Strength \n";
        cout << "2. Intelligence \n";
        cout << "3. Dexterity \n";
        cout << "4. Constitution \n";

        cin >> attriChoice;                                                                       //User inputs the attribute they want to modify

            switch (attriChoice)
            {
                case 1:
                    cout << "You have chosen Strength, is that correct? \n";
                    cout << "Yes \n";
                    cout << "No \n";
                    cin >> attriConfirm;                                                           //Verify that they chose the one they want

                    if((attriConfirm == 'Yes') || (attriConfirm == 'yes'))                           //If YES then ask how many points they want to add
                        {
                            cout << "Please enter the number of attribute points you would "
                                << "like to add to Strength. \n";

                            cin >> attriBonus;

                            str = str + attriBonus;                     //The original strength plus the amount the user wants to add will replace the int str.
                            pointsAvail = pointsAvail - attriBonus;

                            cout << "Your Strength is now " << str << endl;
                        }

                break;

                 case 2:
                    cout << "You have chosen Intelligence, is that correct? \n";
                    cout << "Yes \n";
                    cout << "No \n";
                    cin >> attriConfirm;

                    if((attriConfirm == 'Yes') || (attriConfirm == 'yes'))
                        {
                            cout << "Please enter the number of attribute points you would "
                                << "like to add to Intelligence. \n";

                            cin >> attriBonus;

                            intel = intel + attriBonus;
                            pointsAvail = pointsAvail - attriBonus;

                            cout << "Your Intelligence is now " << intel << endl;
                        }

                break;

                case 3:
                    cout << "You have chosen Dexterity, is that correct? \n";
                    cout << "Yes \n";
                    cout << "No \n";
                    cin >> attriConfirm;

                    if((attriConfirm == 'Yes') || (attriConfirm == 'yes'))
                        {
                            cout << "Please enter the number of attribute points you would "
                                << "like to add to Dexterity. \n";

                            cin >> attriBonus;

                            dex = dex + attriBonus;
                            pointsAvail = pointsAvail - attriBonus;

                            cout << "Your Dexterity is now " << dex << endl;
                        }
                break;

                case 4:
                    cout << "You have chosen Constitution, is that correct? \n";
                    cout << "Yes \n";
                    cout << "No \n";
                    cin >> attriConfirm;

                    if((attriConfirm == 'Yes') || (attriConfirm == 'yes'))
                        {
                            cout << "Please enter the number of attribute points you would "
                                << "like to add to Constitution. \n";

                            cin >> attriBonus;

                            cons = cons + attriBonus;
                            pointsAvail = pointsAvail - attriBonus;

                            cout << "Your Constitution is now " << cons << endl;
                        }
                break;
            }
Last edited on
Because of this:
char attriConfirm;

and then this:
if((attriConfirm == 'Yes') || (attriConfirm == 'yes'))

Your if should be:
if((attriConfirm == 'Y') || (attriConfirm == 'y'))
Here is somewhere to start:

char attriConfirm; //User verification of the attribute that they want to modify

later on you have:
1
2
3
4
5
6
                    
cout << "You have chosen Intelligence, is that correct? \n";
cout << "Yes \n";
cout << "No \n";
cin >> attriConfirm;
if((attriConfirm == 'Yes') || (attriConfirm == 'yes'))     


This is an error - a single char value like attriConfirm cannot
hold 'yes' or 'no'

if the user entered yes, then only the first char y will be put into the attriConfirm variable - leaving stray characters lying around in the
input buffer to trip you up and cause problems later
Thanks alot!!!

I made the changes and it works fine now.

Curiosity question:
Assuming no other changes to the code, why do I get an error at the if statement when I change the:
char attriConfirm;
to a string?

I'm sure that question just made me look like a thoroughbred newbie.







I'm not 100% on this or my terminology, but I'll try my best to explain it.

Char data types accept a char, const char, char literals. A char literal would be something like 'a'.

String data types accept const char arrays, strings, const strings and string literals. A string literal would be something like "a".

Notice the difference in quotes. This is just general, as there is always exceptions, but it's a good rule to follow. You can read up more on this by reading through the tutorial here under the documentation section and then selecting strings.

Someone else may also be able to enlighten both of us if I was wrong about something.
Topic archived. No new replies allowed.