#include <cstdlib>
#include <iostream>
#include <cctype>
usingnamespace std;
int main(int argc, char *argv[])
{
string question;
string repeat;
int answer;
do{
cout << "Please ask a question for the Magic 8 Ball\n" << endl;
getline(cin, question);
answer = rand() % 20 + 1;
switch (answer)
{
case 1:
cout << "it is certain" << endl;
cout << "affirmative"<<endl;
break;
case 2:
cout << "it is decidedly so" << endl;
cout << "affirmative"<<endl;
break;
case 3:
cout << "most likely" << endl;
cout << "affirmative"<<endl;
break;
case 4:
cout << "signs point to yes" << endl;
cout << "affirmative"<<endl;
break;
case 5:
cout << "reply hazy, please ask again" << endl;
cout << "neutral"<<endl;
break;
case 6:
cout << "ask again later"<<endl;
cout << "neutral"<<endl;
break;
case 7:
cout << "dont count on it"<<endl;
cout << "negative"<<endl;
break;
case 8:
cout << "my sources say no"<<endl;
cout << "negative"<<endl;
break;
case 9:
cout << "my reply is no"<<endl;
cout << "negative"<<endl;
case 10:
cout << "very doubtful"<<endl;
cout << "negative"<<endl;
case 11:
cout << "outlook not soo good"<<endl;
cout << "negative"<<endl;
case 12:
cout << "without a doubt" << endl;
cout << "affirmative"<<endl;
break;
case 13:
cout << "yes - definitely" << endl;
cout << "affirmative"<<endl;
break;
case 14:
cout << "you may rely on it" << endl;
cout << "affirmative"<<endl;
break;
case 15:
cout << "yes" << endl;
cout << "affirmative"<<endl;
break;
case 16:
cout << "outlook good" << endl;
cout << "affirmative"<<endl;
break;
case 17:
cout << "as i see it, yes" << endl;
cout << "affirmative"<<endl;
break;
case 18:
cout << "better not tell you now" << endl;
cout << "neutral"<<endl;
break;
case 19:
cout << "cannot predict now" << endl;
cout << "neutral"<<endl;
break;
case 20:
cout << "concentrate and ask again" << endl;
cout << "neutral"<<endl;
break;
}
cout << "Would you like to ask another question?" << endl;
getline(cin, repeat);
repeat = toupper (repeat);
} while (repeat == 'Y');
system("PAUSE");
return EXIT_SUCCESS;
}
getline(cin, repeat);
repeat = toupper (repeat);
the compiler says "no matching function for call to 'toupper(std::string&)' "
as well as "canidates are: int toupper(int)" and "no match for 'operator==' in 'repeat==Y'
ive searched online and im just not sure how to fix this. im very new to c++, sorry for using using namespace std in advance.
toupper can only transform a single char and not a string.
Also you can't compare a string with a single char.
The easiest solution should be to use a char for repeat.
1 2 3 4
cout << "Would you like to ask another question?" << endl;
cin >> repeat; // don't forget to change repeat to a char
cin.ignore(255, '\n'); // remove trailing '\n'
repeat = toupper (repeat);
The toupper function only works for single characters. If you want it convert the whole string to upper case you need to loop through the string and use toupper on each character.
thank you very much thomas, i am having a difficult time learning the language (first programming class) and did not realize that i could not toupper a string. also, the line that says "cin.ignore(255, '\n'); // remove trailing '\n'" what does this mean? what are you ignoring?
For example if you have a char var and type 'a' and return in the console only the 'a' gets stored in the variable. The '\n' will stay in the input buffer and might cause problems with the following inputs.
Not sure if you have a good textbook already, but if you are serious grab a copy of this
The C++ Programming Language 4th ed by Bjarne Stroustrup
The author is the creator of C++ so he knows what he is writing about.