Need help with my loop program...

I think the problem is my use of the WHILE loop. I am trying to set it up where the loop repeats until the condition has been fulfilled. I thought I could use an increment to repeat the loop, but the result is that the last number simply repeats itself, without repeating the initial request or exiting the loop (when either the user says "no" or achieves a "BUST". Where am I going 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
26
27
28
29
30
31
32
33
34
35
36
int main()
{
    srand(time(0));
    int firstNum, secondNum, sumNum, newNum, newSumNum, newNewSumNum;
    char responseUser;
    for (int i = 1; i < 2; i++){
        firstNum = 1 + rand()%11;
        secondNum = 1 + rand()%11;
        sumNum = firstNum + secondNum;
        cout << "First cards: " << firstNum << ", " << secondNum << endl;
        cout << "Total: " << sumNum << endl;
        cout << '\n';
        cout << "Do you want a card (y/n)?" << endl;
        cin >> responseUser;
        cout << '\n';
        if (responseUser != 'n' && responseUser != 'N'){
            newNum = rand()%11;
            newSumNum = newNum + sumNum;
            cout << newNum << endl;
            while (newSumNum <= 21){
                cout << "Total: " << newSumNum << endl;
                cout << "Do you want another card (y/n)?" << endl;
                cin >> responseUser;
                cout << endl;
                newNum = rand()%11;
                newNewSumNum = newSumNum + newNum;
                cout << newNewSumNum << endl;
                newNewSumNum++;
            }
        }
        else {
            cout << "Good-Bye!" << endl;
            return 0;
        }
    }
	return 0;
1
2
3
4
5
6
7
8
9
10
while (newSumNum <= 21){
                cout << "Total: " << newSumNum << endl;
                cout << "Do you want another card (y/n)?" << endl;
                cin >> responseUser;
                cout << endl;
                newNum = rand()%11;
                newNewSumNum = newSumNum + newNum;
                cout << newNewSumNum << endl;
                newNewSumNum++;
            }

In this piece of code, you essentially ignore the user's input. This loop will always run until you bust, because weather or not you hit n or y the computer will add to the total. Try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
while (newSumNum <= 21){
                cout << "Total: " << newSumNum << endl;
                cout << "Do you want another card (y/n)?" << endl;
                cin >> responseUser;
                if(tolower(responseUser) == 'n'){
                        break;
                };
                cout << endl;
                newNum = rand()%11;
                newNewSumNum = newSumNum + newNum;
                cout << newNewSumNum << endl;
                newNewSumNum++;
            }


I would also recommend separating your code into functions, so that you don't have to re-write newNum = rand()%11 repeatedly.

Also, please try to use more unique variable names, as it gets very difficult to read your code with too similar variables, like newSumNumandnewNewSumNum, especially when they're the same type. Hope this helps!

EDIT: I was working with your code a bit, and I think this is a far more efficient way to write it:
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
#include<iostream>
#include<string>
#include<time.h>
using namespace std;

int hit(){
	return(rand()%11);
}

int main()
{
    srand(time(0));
    int firstNum, secondNum, sumNum;
    char responseUser;
    firstNum = hit();
    secondNum = hit();
    sumNum = firstNum + secondNum;
    cout << "First cards: " << firstNum << ", " << secondNum << endl;
        while ((tolower(responseUser) != 'n')&&(sumNum <= 21)){
            cout << "Total: " << sumNum << endl;
            cout << "Do you want another card (y/n)?" << endl;
            cin >> responseUser;
            if(tolower(responseUser) == 'n'){break;}
            else{
               	cout << endl;
               	sumNum += hit();
            }
    }
    return sumNum;
}
Last edited on
You should REALLY name your variables better, then again I have the same habit. Anways the problem is that your never changing newSumNum in the loop so it's always going to be the same number. If you want to exit the loop when the user says no have an if statement and use the break command to break out of it when needed to. Also your question was kind of hard to understand so forgive me if that's not the right answer.
Last edited on
Topic archived. No new replies allowed.