Most of my questions are answered by quick Google searches, barring this one. The compiler gives an error whenever I try to compile this code (ignore the indentation). It says "expected expression" at line 15. I'm guessing it's a known issue as I've tried it in a different compiler which also gives an error (albeit a different one).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
string gm;
int stars = 0, c;
cout << "Anyway, you want to play a game?" << endl;
cin >> gm;
if (gm == "y" || "Y") {
cout << "How many stars do you want?" << endl;
cin >> stars; }
for (c=0;c<=stars;++c) {
cout << "*";
}
elseif (gm == "n") {
cout << endl << "Ok, thanks for talking with me. Have a nice day!";
}
In C++ you need to declare variables before you use them.
You never declare "gm" as a variable, a declaration would look like this string gm; //type is string, name is gm
Same applies to your c variable, and your stars variable, except they are ints instead of strings.
You'll also need to add #include <string> to the top of your code.
Also, every statement in an if statement needs to be its own thought. if (gm == "y" || "Y") {
should be if (gm == "y" || gm == "Y") {
Lastly, you can't have a for loop between and if and and else if statement, the else if block needs to directly follow the if block. Your placement of } braces makes this hard to see.
Yeah, I've edited it. This code is actually a part of a bigger one, I snipped out the rest and forgot to leave in the declarations, should look fine now.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string nm, gm;
int age, stars, c=0;
cout << "What's your name? ";
getline (cin,nm);
cout << "Hello, " << nm << "!" << endl;
cout << "And what is your age? " ;
cin >> age;
if (age <= 13)
cout << "It's time for your nappy time, little baby!";
elseif (age >= 30)
cout << "You're too old for this!\n";
else cout << "You're just about right!\n";
cout << "Anyway, you want to play a game?" << endl;
cin >> gm;
if (gm == "y" || gm == "Y") {
cout << "How many stars do you want?" << endl;
cin >> stars; }
for (c=0;c<=stars;++c) {
cout << "*";
}
elseif (gm == "n" || gm == "N") {
cout << endl << "Ok, thanks for talking with me. Have a nice day!";
}
}
I have no idea what I have been doing for the past hour but it's not working. Can you clarify what exactly is the problem? The compiler still has trouble recognising which else if belongs with which if.
my post above shows exactly what you need to do. basically you only want to print the stars out if the user presses y or Y? So it should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
if (gm == "y" || gm == "Y")
{
cout << "How many stars do you want?" << endl;
cin >> stars;
for (c = 0; c <= stars; ++c)
{
cout << "*";
}
}
elseif (gm == "n" || gm == "N") {
cout << endl << "Ok, thanks for talking with me. Have a nice day!";
}
I have. However, another problem has come up. When I compile this code and enter a digit, it jumps to the dumbass: line, doing the opposite of what I want. When I enter an alphabet or a symbol, it becomes all messy. No compile time errors.
Anyway, your problem is you're misunderstanding what isdigit does.
isdigit is supposed to take in a character or number that represents the ASCII value of a character. http://www.asciitable.com/
For example, if you input 48 as your age, isdigit will return true.
Ignoring the goto, which is most likely bad practice in this situation,
I would change your code to this:
Having an age variable be an int is much more intuitive than it being a char.
Also, if he changes age to be a char, it will only save the first digit.
55 will be recorded as 5.
Checking the cin stream is probably the best way to check for invalid input.
I couldn't find any goto equivalent, no loops could work here.
So if I'm understanding this correctly, when you 'check' an input, it verifies whether or not the input matches the data type variable? This is better than the isX() functions.