I am having a problem with a while loop in c++. The while loop is always being executed the first time around but when the program reaches the cin of the while loop the while loop works perfectly I was wondering what I was doing wrong. Thanks in advance. Also I am sorry if the problem is noobish. I am still a beginner.
cout<<"Would you like some ketchup? y/n"<<endl<<endl<<endl; //Ketchup
selection screen
cin>>optketchup;
while (optketchup != "yes" && optketchup != "no" && optketchup != "YES" && optketchup != "Yes" && optketchup != "YEs" && optketchup != "yEs" && optketchup != "YeS"
&& optketchup != "yeS" && optketchup != "YeS" && optketchup != "yES" && optketchup != "y" && optketchup != "Y" && optketchup != "No" && optketchup != "nO"
&& optketchup != "NO" && optketchup != "n" && optketchup != "No");
{
cout<<"You have entered an entered "<<optketchup<<" which is an invalid
option. Please try again."<<endl;
cin>>optketchup;
}
if (optketchup == "yes" || optketchup == "YES" || optketchup == "Yes" || optketchup == "YEs" || optketchup == "yEs" || optketchup == "YeS"
|| optketchup == "yeS" || optketchup == "YeS" || optketchup == "yES" || optketchup == "y" || optketchup == "Y")
{
slcketchup == "with";
}
else
{
slcketchup == "without";
}
cout<<"Your sandwich shall be "<<slcketchup<<" ketchup."<<endl;
system ("pause");
Again thanks in advance.
You have a semicolon(‘;’) in the
while. That’s causing the problem.Don’t write
Write
Notice the lack of the
;in the 2nd one.Other than that, what if you had to check for the word
Pneumonoultramicroscopicsilicovolcanoconiosis. How many combinations of upper & lower cases would you end up checking?Instead, convert the input into upper case & compare against upper case
YESorNOorPNEUMONOULTRAMICROSCOPICSILICOVOLCANOCONIOSIS.