Mdscommand
New Coder
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool complete = false;
char user;
bool input = false;
while (complete == false)
{
cout << "are you new to c++? (y/n)" << endl;
cin >> user;
switch (user)
{
case 'y':
cout << " welcome new programmer!" << endl;
complete = true;
break;
case 'n':
cout << "welcome back to c++!" << endl;
complete = true;
break;
default:
cout << "invalid reaponse. please try again." << endl;
break;
}
}
return 0;
}
So the code works perfectly. If the user gives correct input it runs as expected. If the user gives incorrect input, it responds "invalid input" and runs the loop again. The code also works perfectly if the user inputs the full word yes or no. The issue arises when the user inputs some like "I am not". In that case the loop checks each individual character until it hits the 'n' in the phrase and then runs that case. I've tried a few different approaches to solve it but can't come to a resolution.
#include <string>
using namespace std;
int main()
{
bool complete = false;
char user;
bool input = false;
while (complete == false)
{
cout << "are you new to c++? (y/n)" << endl;
cin >> user;
switch (user)
{
case 'y':
cout << " welcome new programmer!" << endl;
complete = true;
break;
case 'n':
cout << "welcome back to c++!" << endl;
complete = true;
break;
default:
cout << "invalid reaponse. please try again." << endl;
break;
}
}
return 0;
}
So the code works perfectly. If the user gives correct input it runs as expected. If the user gives incorrect input, it responds "invalid input" and runs the loop again. The code also works perfectly if the user inputs the full word yes or no. The issue arises when the user inputs some like "I am not". In that case the loop checks each individual character until it hits the 'n' in the phrase and then runs that case. I've tried a few different approaches to solve it but can't come to a resolution.