getline/else if

Started by
3 comments, last by Scot Garcia 10 years, 10 months ago

Just starting out in this programming adventure, and I've run into a problem already with my getline function and else if.

Here is my code:


#include <iostream>

using namespace std;

int main ()
{
    int billy;
    int jill;

    cout << "How old is Billy? " << "\n";
    getline( cin, billy, '\n');

    cout << "Okay!  Now, how old is Jill? " << "\n";
    getline ( cin, jill);

    if ( ( billy > 100 ) && ( jill > 100 ) );
    {
        cout << "Wow!  Those guys are ******* old!";
    }
    else if ( billy > jill );
    {
        cout << "Okay, so Billy is older than Jill.  Expected.";
    }
    else ( billy < jill );
    {
        cout << "Wait, so Jill was older than Bill?  Interesting.";
    }
}

Now it my compiler (I'm using code::blocks) says that there's no matching function for all to getline. It works just fine if I switch it to:


cin >> billy

So, I'm not sure what's wrong with the code. I've reread the instruction book I have explaining getline, but it doesn't exactly explain the full use of it.

And secondly, when I switch the getline to cin >>, when my compiler get's to the else if statement, it says: error: 'else' without a previous 'if'

So, thanks ahead of time for any help, I'd definitely like to get past this point, and I don't want to move on before fully understanding what I've learned.

Advertisement

Dunno about your getline problem. (EDIT: Oh yeah, now I do, it probably expects a string instead of an int, so use cin >> instead).

Your other problem is you have semicolons after your if, else if and else statements...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
An example:


if (something == something_else);   // this is an empty if (expression) test, that does nothing

{
    // this codeblock is effectively a 'scope,'
    // however, it is not related to the above if statement in any way
}

if (something == something_else)
     one_line(of, &code);   // this if statement has one line of code, because there are no {}

while (something == something_else)
     one_line(of, &code);   // this while loop has one line of code, because there are no {}

if (something == something_else)
{
   // this if (expression) has a code block, that
   // allows for multiple lines of code to be executed if the test was true
   
   int a = 0; // this variable is dead as soon as we hit a }
}

Now it my compiler (I'm using code::blocks) says that there's no matching function for all to getline.

You need to #include <string>. The <string> header is the header that defines std::getline(std::istream&, std::string&). Also, '\n' is the default delimiter, so you don't need to (and probably shouldn't) put it in the function call.

And secondly, when I switch the getline to cin >>, when my compiler get's to the else if statement, it says: error: 'else' without a previous 'if'

You've got semicolons after your if and elses. That doesn't do what you think it does. Remove the semicolons so it reads like this:

if ( ( billy > 100 ) && ( jill > 100 ) ) // no semicolon
{
cout << "Wow! Those guys are ******* old!";
}
else if ( billy > jill ) // no semicolon
{
cout << "Okay, so Billy is older than Jill. Expected.";
}
else ( billy < jill ) // no semicolon
{
cout << "Wait, so Jill was older than Bill? Interesting.";
}

When you put the semicolons there, it immediately terminates the if/else (which means that the following {} block is not part of the if/else body).

Sorry I'm not using syntax highlighting in this post. GD's post editor is messed up and I'm sick of fighting it.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Thank you so much guys! I wasn't aware getline was part of <string> and I got so used to a semi-colon going at the end of everything, that I must've blocked out the part where it says NOT to put them in the if/else statements.

This topic is closed to new replies.

Advertisement