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