Hello rokij6698,
Your use of
cin >> decision
works fine for variable defined as a single character. But when you try to use it for a string with spaces it will only extract from the input buffer up to the first white space leaving the rest of the string in the input buffer.
For things like "first (space) last" name and an address it is better to use
std:getline(std::cin, address);
. This will extract everything from the input buffer including the new line character.
Mixing "std::cin" and "std::getline(...)" in a program is fine as long as you use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
after the "std::cin" to clear the input buffer otherwise the "std::getline(...)" will extract the newline character and continue on with the program.
Two other observations:
char firstName[99] = "\0";
can be shortened to
char firstName[99]{};
where the empty {}s will do the same thing.
A "std::string" would be a better choice for your string variables instead of the character arrays and they are much more flexible. Unless you are stuck with using character arrays.
Suggestion: include the header file "<cctype>" and you could follow "cin >> decision"
decision = std::toupper(decision);
. This way you will always be using an upper case letter for the while loop and the switch.
A few blank lines in the program would help also. An example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
obj.printMenu();
cin >> decision;
decision = std::toupper(decision);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
while (decision != 'E' && decision != 'e' && decision != 'D' && decision != 'd' && decision != 'Q' && decision != 'q')
{
cout << "invalid character, please enter again: ";
cin >> decision;
}
switch (decision)
{
case 'E':
|
This makes the code much easier to read when all the lines are not run together.
Hope that helps,
Andy