If, Else if, Else Help!

This is a discussion on If, Else if, Else Help! within the C++ Programming forums, part of the General Programming Boards category; im using dev-c++ compiler, and i keep getting an error on this bolded line of code Code: #include <iostream> using ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    10

    If, Else if, Else Help!

    im using dev-c++ compiler, and i keep getting an error on this bolded line of code
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int age;
        cout<< "Please input your age...";
        cin>> age;
        cin.ignore();
        if ( age <= 40) {
                cout<<"You are pretty young!";
                }
        else if( age >=41, age <= 60 ) {
             cout<<"You are kind of old!";
             }
        else ( age >= 61) {
             cout<<"You are very old!";
             }
             cin.get();
             return 0;
    }

    the error I'm getting says this: expected ";" before "{" token.

    any help guys?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    the keyword "else" in the bolded text needs to be followed by "if".
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    This line does not do what you think it does:
    Quote Originally Posted by yosimba2000 View Post
    Code:
        else if( age >=41, age <= 60 ) {
    In particular, a comma does not represent 'and'.
    The 'age >= 41,' part is redundant and should be removed. In fact the '(age >= 61)' part is also redunadant.
    Last edited by iMalc; 09-05-2010 at 12:35 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    else does not take any conditions.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    Quote Originally Posted by iMalc View Post
    This line does not do what you think it does:In particular, a comma does not represent 'and'.
    The 'age >= 41,' part is redundant and should be removed. In fact the '(age >= 61)' part is also redunadant.
    well i was trying to make it so that 41 <= age <= 60. i didn't know how to do it since the compiler wouldnt accept the " 41 <= age <= 60 " form, so i split it using a comma. what is the proper way to do this? the comma form works fine for me though... maybe you should try to compile it but remove the condition for else and see if it works.

    and to the guy right above me, thanks for telling me that ELSE doesnt take any conditions. totally worked
    Last edited by yosimba2000; 09-05-2010 at 02:17 PM.

  6. #6
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18
    ok so i just started with programming like yesterday but i got it

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int age;
        cout<< "Please input your age...";
        cin>> age;
        cin.ignore();
        if ( age <= 40) {
                cout<<"You are pretty young!";
                }
        else if( age >=41, age <= 60 ) {
             cout<<"You are kind of old!";
             }
        else ( age >= 61); {
             cout<<"You are very old!";
             }
             cin.get();
             return 0;
    }
    lol i just tried random stuff dont feel bad a 2nd day programmer fixed your problem

  7. #7
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18
    Quote Originally Posted by arc_angel14 View Post
    ok so i just started with programming like yesterday but i got it

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int age;
        cout<< "Please input your age...";
        cin>> age;
        cin.ignore();
        if ( age <= 40) {
                cout<<"You are pretty young!";
                }
        else if( age >=41, age <= 60 ) {
             cout<<"You are kind of old!";
             }
        else ( age >= 61); {
             cout<<"You are very old!";
             }
             cin.get();
             return 0;
    }
    lol i just tried random stuff dont feel bad a 2nd day programmer fixed your problem
    wait lol srry abt that i didnt fix it ignore the noob

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The proper way is
    Code:
    age >=41 && age <= 60
    where && is the logical AND

    Removing the wrong else condition, you don't need the age >=41 as iMalc said. Because you already know that it is not less or equal than 40 (first if). So of course it is greater or equal than 41.

    Should you use it for clarity? Generally, it is not a bad idea to add things just to make your code more clear and not going insane about an extra operation. But you should avoid typing the same thing twice. Because lets say you decide to make the first condition less than 50. Then you have to change the second condition. Maybe you forget. Then you will have something confusing. Having it once (on the first if) you can manipulate more easily your code. So it is a good advice to remove it.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    Quote Originally Posted by arc_angel14 View Post
    wait lol srry abt that i didnt fix it ignore the noob
    lol i just started yetserday too! im on pointers now but if anyone knows the answer to my new question like about 3 posts up, feel free to tell me..

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    Quote Originally Posted by C_ntua View Post
    The proper way is
    Code:
    age >=41 && age <= 60
    where && is the logical AND

    Removing the wrong else condition, you don't need the age >=41 as iMalc said. Because you already know that it is not less or equal than 40 (first if). So of course it is greater or equal than 41.

    Should you use it for clarity? Generally, it is not a bad idea to add things just to make your code more clear and not going insane about an extra operation. But you should avoid typing the same thing twice. Because lets say you decide to make the first condition less than 50. Then you have to change the second condition. Maybe you forget. Then you will have something confusing. Having it once (on the first if) you can manipulate more easily your code. So it is a good advice to remove it.
    hey thanks for that && tip, totally forgot about it. but can you clarify where i typed te same thing twice? i can't find it.

  11. #11
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18
    Quote Originally Posted by yosimba2000 View Post
    lol i just started yetserday too! im on pointers now but if anyone knows the answer to my new question like about 3 posts up, feel free to tell me..
    dude what the heck im still making calculators im only at constants. this is as much as i can do

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
    	system("TITLE Calculator");
    	system("COLOR 2");
    	char cChar;
    	double dfirstnumber;
    	double dsecondnumber;
    	char cDoagain;
    	
    do
    {
    	system("CLS");
    	cout << "please enter the first number that you would like to use" << endl;
    	cin >> dfirstnumber;
    	cout << "please enter the operation that you would like to complete" << " (+,-,* or /)" << endl;
    	cin >> cChar;
    	cout << "please enter the second number you would like to use" << endl;
    	cin >> dsecondnumber;
    	
    		switch (cChar)
    		{
    		case '+':
    			cout << "the answer is; " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
    				break;
    		case '-':
    			cout << "the answer is; " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
    				break;
    		case '*':
    			cout << "the answer is; " << dfirstnumber << " * " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
    				break;
    		case '/':
    			if(dsecondnumber == 0) {
    			cout << "that is an invalid operation" << endl;
    			}else{
    			cout << "The answer is; " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
    			}
    		}
    		cout << "would you like to start again? (y or n)" << endl;
    		cin >> cDoagain;
    	}while (cDoagain == 'Y' || cDoagain == 'y');
    	system("pause");
    	return 0;
    	
    }

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    to the guy above me, i think you learn much faster than me. i don't even understand how the switch/case works AND i did not yet learn this Doagain command. im using this tutorial: C++ Made Easy: Learning to Program in
    C++


    heres the site: http://www.cprogramming.com/tutorial.html

  13. #13
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18
    its easy it switches the computer to cChar and in the case + do this in case - do this ......

  14. #14
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    The syntax for the control statement "if, else if, else" goes like this:

    Code:
    if (condition) {
       // stuff that happens when condition is true
    }
    else if (other_condition) {
       // stuff that happens when condition is false but other_condition is true
    }
    else {
       // stuff that happens when condition and other_condition are both false
    }
    Now look at your code again...
    Code:
        if ( age <= 40) {
                cout<<"You are pretty young!";
                }
        else if( age >=41, age <= 60 ) {
             cout<<"You are kind of old!";
             }
        else ( age >= 61) {
             cout<<"You are very old!";
             }
    There is no condition for the final "else" clause, it doesn't make sense. The compiler thinks you meant to say:
    Code:
    else {
       ( age >= 61 );
    }
    ...which isn't what you intended.

    I think this is what you actually intended...
    Code:
    if (age <= 40) {
       cout << "You are pretty young!";
    }
    else if (age<= 60) {
       cout << "You are kind of old!";
    }
    else {
       cout << "You are very old!";
    }
    Last edited by BMJ; 09-06-2010 at 10:00 PM.

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    ok thx BMJ, so ELSE does not have any arguments right? it just stays as else.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21