Do While Loop Not Looping/Working

Hello. As you are probably aware, I am a student and this is for an assignment, but I am stuck. I have spent hours trying to change the code so that my program loops again if the user would selects "Y" at the end, however, the way I have it coded, it either skips to the end OR it continues to infinity with just the last statement instead of the entire program from the beginning.

FYI, I did try Switch and Case instead of the IF for the Days of the week, but I understand IF better, so I left it.

int main ()
{
int DayofWeek = 0, Y = 0, N = 0;

int RepeatOrTerminate = 0;

cout << "Enter a day of week value: " << endl;
cin >> DayofWeek;

while (DayofWeek < 1 || DayofWeek > 7)

{
cout << DayofWeek << " is invalid" << endl;
cout << "Enter a day of week value: " << endl;
cin >> DayofWeek;
break;
}

do
{
if (DayofWeek == 1){cout << "Monday" << endl;}
if (DayofWeek == 2){cout << "Tuesday" << endl;}
if (DayofWeek == 3){cout << "Wednesday" << endl;}
if (DayofWeek == 4){cout << "Thursday" << endl;}
if (DayofWeek == 5){cout << "Friday" << endl;}
if (DayofWeek == 6){cout << "Saturday" << endl;}
if (DayofWeek == 7){cout << "Sunday" << endl;}
cout << "Do you wish to continue?" << endl;
cin >> RepeatOrTerminate;
break;
} while (RepeatOrTerminate == Y);

cout << "Goodbye! Thank you for using our program!" << endl;


return 0;
}
Remove both break statements. They currently exit loops that you want to test.

I presume you are aware that the value of variable Y means that you would have to input 0 to continue looping?

Now would be a good time to learn about arrays.

Please use code tags.
Last edited on
One of a myriad of ways, easier with an array instead of repetitive switch cases:
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
#include <string>
#include <iostream>

using namespace std;

int main ()
{
    string DayName[] =
    {
        "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday"
    };

    int DayNumber{0};
    char repeat{'Y'};

    while( toupper(repeat) == 'Y' )
    {
        cout << "Enter a day of week value: ";
        cin >> DayNumber;

        if(DayNumber > 0 && DayNumber < 7)
            cout << "Day of week is: " << DayName[DayNumber] << '\n';
        else
            cout << DayNumber << " is invalid\n";

        while (
               cout << "Do you wish to continue Y/N?" &&
               cin >> repeat && toupper(repeat) != 'Y' &&
               toupper(repeat) != 'N'
               )
        {
            cout << "Your answer must be Y or y or N or n\n";
        }
    }

    cout << "Goodbye! Thank you for using our program!\n";

    return 0;
}


Enter a day of week value: 9
9 is invalid
Do you wish to continue Y/N?
y
Enter a day of week value: 4
Day of week is: Friday
Do you wish to continue Y/N?
77
Your answer must be Y or y or N or n
Do you wish to continue Y/N?
Your answer must be Y or y or N or n
Do you wish to continue Y/N?
y
Enter a day of week value: 3
Day of week is: Thursday
Do you wish to continue Y/N?
n
Goodbye! Thank you for using our program!
Program ended with exit code: 0

Note the response for 77, check for valid input is very basic only
Hello mrsfizz9,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This is your code in a better format:
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
iint main()
{
    int DayofWeek = 0, Y = 0, N = 0;

    int RepeatOrTerminate = 0;  // <--- Should be a "char" not an "int".

    cout << "Enter a day of week value: ";
    cin >> DayofWeek;

    while (DayofWeek < 1 || DayofWeek > 7)
    {
        cout << DayofWeek << " is invalid\n";

        cout << "Enter a day of week value: ";
        cin >> DayofWeek;

        break;
    }

    do
    {
        if (DayofWeek == 1)
            cout << "Mondan\n";

        if (DayofWeek == 2)
        {
            cout << "Tuesday\n"; 
        }

        if (DayofWeek == 3) { cout << "Wednesday" << endl; }
        if (DayofWeek == 4) { cout << "Thursday" << endl; }
        if (DayofWeek == 5) { cout << "Friday" << endl; }
        if (DayofWeek == 6) { cout << "Saturday" << endl; }
        if (DayofWeek == 7) { cout << "Sunday" << endl; }

        cout << "Do you wish to continue?" << endl;
        cin >> RepeatOrTerminate;

        break;
    } while (RepeatOrTerminate == Y);

    cout << "Goodbye! Thank you for using our program!" << endl;

    return 0;  // <--- Not required, but makes a good break point.
}

It is usually best to provide complete code that can be compiled and tested. Having to guess at header files that you used may result in someone using a header file that you have not learned yet.

Line 7: is a start, but "value" can have more than 1 meaning. If I choose to enter "Monday" instead of "1" "cin" would fail and be unusable the rest of the program until the program is stopped or "cin" is fixed.

What you could do is print a menu of choices first:
1
2
3
4
5
std::cout <<
    " 1. Monday\n"
    " 2. Tuesday\n"
    // Continue with the rest of the days.
    ;  // <--- At the end of the last line. 

Each line may be a quoted string, but is is considered 1 big string in the end. Doing it this way makes it easier to change or adjust and it looks more like what is printed on the screen. The the prompt can be changed to ask for a number.

The while loop works until you enter a valid number.

The do/while works except that "DayofWeek" never changes.

On line 36 the prompt does not tell the user what to enter. You are expecting a number, but most people would likely type "y" or "n" or the upper case letters. Again this would cause "cin" to fail and become unusable. Also "RepeatOrTerminate", good name BTW and do not be afraid of long names, would work better as a "char" type. That is what more people tend to use.

That is something to start with. When I get the program into my IDE and can test I will know more.

Andy
Hello mrsfizz9,

As I managed to get the code to compile an run I discovered that the while and do/while both contained a "break" as the last line.

The while loop never repeats because you leave the loop even if you enter an incorrect value the 2nd time.

In the do while loop it does not matter what you enter from the prompt "\nDo you wish to continue (Y/N)? " with the "cin" the next line is "break" so you leave the do/while loop, but there is no way back to the top to repeat the menu and start over.

The following code follows what you started with and some changes that you may find useful. I also added the switch to give you an idea of how that can work.

Unless you have been coding for a couple of years the first 2 if statements are better options until you get use to the compiler's error messages. With or without the {}s the compiler is more likely to point to the line number where the problem is. Sometimes the line listed as an error is not always where it started. It might be a line or 2 above. I think this would be better in the beginning because with a single line you may have a hard time trying to figure out what part is wrong. IMHO.

Your variable names are good, but I will suggest that a regular variable like "repeatOrTerminate" and "dayOfWeek" start with a lower case letter. Although you are free to choose how you write a variable name this is the more often used method that I see. Whether you use camelCAse or the (_) is your choice.

The names that start with a capital tend to be "classes" and "structs" and my choice is to include function names, although some may frown on this.

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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <cctype>

int main()
{
    char repeatOrTerminate{};
    int dayOfWeek{}/*, n{}*/;
    const std::string DAY_NAMES[]{ "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    std::string dayName;

    while (1)
    {
        std::cout <<
            "\n"
            " 1. Monday\n"
            " 2. Tuesday\n"
            " 3. Wednesday\n"
            " 4. Thursday\n"
            " 5. Friday\n"
            " 6. Saturday\n"
            " 7. Sunday\n";

        while (std::cout << " Enter a day of week number: " && !(std::cin >> dayOfWeek) || (dayOfWeek < 1 || dayOfWeek > 7))
        {
            if (!std::cin)
            {
                std::cerr << "\n     Invalid Input!. Must be a number.\n\n";

                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
            }
            else if (dayOfWeek < 1 || dayOfWeek > 7)
            {
                std::cout << "\n     " << dayOfWeek << " is invalid\n\n";
            }
        }

        std::cout << "\n You have chosen ";

        //switch (dayOfWeek)
        //{
        //    case 1:
        //        dayName = DAY_NAMES[dayOfWeek];
        //        break;
        //    case 2:
        //        dayName = DAY_NAMES[dayOfWeek];
        //        break;

        //    default:
        //        break;
        //}

        //std::cout << dayName << '\n';

        if (dayOfWeek == 1)
            std::cout << "Monday\n";
            //dayName = DAY_NAMES[dayOfWeek];  // <--- Could also use this. And copy line 56 thrn paste it in after the if statements.

        if (dayOfWeek == 2)
        {
            std::cout << "Tuesday\n";
        }

        if (dayOfWeek == 3) { std::cout << "Wednesday\n"; }
        if (dayOfWeek == 4) { std::cout << "Thursday\n"; }
        if (dayOfWeek == 5) { std::cout << "Friday\n"; }
        if (dayOfWeek == 6) { std::cout << "Saturday\n"; }
        if (dayOfWeek == 7) { std::cout << "Sunday\n"; }

        std::cout << "\nDo you wish to continue (Y/N)? ";  // <--- Changed. Added space at the end.
        std::cin >> repeatOrTerminate;
        
        if (std::toupper(repeatOrTerminate) != 'Y')
            break;
    }

    std::cout << "\n   Goodbye! Thank you for using our program!\n";

	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

I also found that the do/while was causing a problem because there was no way back to the top.

The outer most while loop you can keep or change to a do/while loop.

Andy
Topic archived. No new replies allowed.