Loop in a selection problem.

Pages: 12
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>
  using namespace 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 << "*";
    }
   else if (gm == "n") {
       cout << endl << "Ok, thanks for talking with me. Have a nice day!"; 
}
Last edited on
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.
Last edited on
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.
ignore the indentation
Your issue arises because of your indentation.

You cannot have a code struture like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (gm == "y" || "Y")
{
		
}
for (c = 0; c <= stars; ++c)
{

}

else if (gm == "n")   // <-- else with no if.
{
		
}


which is exactly what you'd see if you sorted out your formatting and indentation.

You also do no close off your main() function i.e. you need to another closing brace after line 17.

edit 2: use getline to read in your string:
http://www.cplusplus.com/reference/string/string/getline/
(and #include<string>
Last edited on
The compiler I use will compile the code regardless of indentation or lack thereof.
The compiler gives an error


The compiler I use will compile the code regardless of indentation or lack thereof.

Make your mind up dude :)

Your indentation is making it look like you have the right amount of closing braces, but you do not.
The error isn't the indentation itself, but it leads to errors like the one described by mutexe. (Sorry my internet is being slow right now)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace 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!";
    else if (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 << "*";
    }
    else if (gm == "n" || gm == "N") {
        cout << endl << "Ok, thanks for talking with me. Have a nice day!"; 
}
}


Here's the whole code. Still any errors? :(
Still any errors?

Yes. exactly the same one. I'll paste my reply again.



You cannot have a code struture like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (gm == "y" || "Y")
{
		
}
for (c = 0; c <= stars; ++c)
{

}

else if (gm == "n")   // <-- else with no if.
{
		
}



Ok, I'll try to improve it.
if you move that for loop (lines 23 and 24) inside your if (i.e. insert at line then it should compile. i.e. here:

cin >> stars; /* <insert your for loop here, before the closing brace> */ }
Last edited on
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.
Last edited on
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 << "*";
		}
	}
	else if (gm == "n" || gm == "N") {
		cout << endl << "Ok, thanks for talking with me. Have a nice day!";
	}

Thank you! Your code works. Apparently the problem was that the for loop was outside the selection, which later turned into a missing curly bracket.
Last edited on
You're welcome. If you get used to structuring if/else's and for loops like i've done above it's a lot easier to spot missing brackets.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
    string nm, gm, q;
    int age, stars, c=0;
    cout << "What's your name? ";
    getline (cin,nm);
    cout << "Hello, " << nm << "!" << endl;
    dumbass:
    cout << endl << "What is your age? ";
    cin >> age;
    if (isdigit(age))
    {
    }
    else
    {
        cout << "Enter a number, dumbass.";
        goto dumbass;
    }
}
Why did you start using goto?

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main()
{
    string nm, gm, q;
    int age, stars, c=0;
    cout << "What's your name? ";
    getline (cin,nm);
    cout << "Hello, " << nm << "!" << endl;
    dumbass:
    cout << endl << "What is your age? ";

    if ((cin >> age)) // Checks if input is valid
    {
        cout << "Age confirmed" << endl;
    }
    else
    {
        // Clears invalid input buffer:
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        cout << "Enter a number, dumbass.";
        goto dumbass;
    }
}


Edit: Fixed typo/logic
Edit2: Fixed infinite loop, woops.
Last edited on
You have the int variable age. Change it to a char variable instead.
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.
Pages: 12