Public Group

# getline/else if

This topic is 2175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites

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...

##### Share on other sites
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 }
}


Edited by Kaptein

##### Share on other sites

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.

##### Share on other sites

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.

• 12
• 10
• 10
• 12
• 21