Hey, i made a code, and i would like to know how to repeat it without exiting, if i say M stating that i am a male, the script should answer properly, and then repeat, and ask me again.
#include "stdafx.h"
#include "iostream"
usingnamespace std;
int main()
{
char x;
cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
cin >> x;
switch (x)
{
case'F':
cout << "I know your gender now: Male" << endl;
break;
case'M':
cout << "I know your gender now: Female" << endl;
break;
default:
cout << "Reply with M for male and F for female. Your answer is case sensitive" << endl;
}
system("PAUSE");
}
Yes, i did have my answers reversed, and i made a loop and now works, but i have another problem: when i add anything instead of M or F, the code submits on my behalf the number times as the number of characters my answer has:
View photo
#include "stdafx.h"
#include "iostream"
usingnamespace std;
int main()
{
int rep = 0;
while (rep == 0)
{
char x = 0;
cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
cin >> x;
switch (x)
{
case'F':
cout << "I know your gender now: Female" << endl;
break;
case'M':
cout << "I know your gender now: Male" << endl;
break;
case'Q':
rep = 1;
break;
default:
cout << "Reply with M for male and F for female. Your answer is case sensitive." << endl;
}
}
}
int main()
{
char x;
do
{
cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
cin >> x;
switch (x)
{
case'F':
cout << "I know your gender now: Female" << endl;
break;
case'M':
cout << "I know your gender now: Male" << endl;
break;
case'Q':
break;
default:
cout << "Reply with M for male and F for female. Your answer is case sensitive." << endl;
}
}while (x!='F' && x!='M' && x!='Q');
}
#include "stdafx.h"
#include "iostream"
usingnamespace std;
int main()
{
char x;
do
{
cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
cin >> x;
switch (x)
{
case'F':
cout << "I know your gender now: Female" << endl;
break;
case'M':
cout << "I know your gender now: Male" << endl;
break;
case'Q':
break;
default:
cout << "Reply with M for male and F for female. Your answer is case sensitive." << endl;
}
} while (x != 'Q');
}
and the same happens if i input something that is not F, M or Q, the loop repeats the same number of times as the number of characters in my reply. If i reply "test", it repeats 4 times, because there are 4 letters
x is one character so it extracts only one character from the buffer in the case of "test" it extracts the t and places it in x. Now "est" is left in the buffer. There are error flags letter you know that something went wrong you can check with if(!cin) if there are any errors you then may wish to clear the flag with cin.clear(); and then ignore anything that may be left in the buffer (read but don't extract) with cin.ignore(1024, '\n'); where 1024 is an arbitrary number if you wish to ignore everything that could possibly be in the buffer then you would use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); (You would put this stuff in the default case or after getting the input)