Thread: Else if problem (noob)

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    11

    Else if problem (noob)

    Hi!

    So i started out two days ago with c++ and programming in general and while trying to make a simple program I stumbled upon an error.

    I've been staring at it for about 30 minutes now and it is probably an easy fix but I can't seem to find an answer.

    Any help on this matter would be greatly appreciated and any further comments on the effectiveness on my written code or any other criticism would also be welcomed. But mainly I just want my program to run so the error is the main point of focus.

    Code:
    #include <iostream>
    #include <string>
    
    
    
    
    using namespace std;
    
    
    string user_age_1;
    string user_age_2;
    
    
    int main ()
    
    
    {
        cout << "user 1 enter your age" << "\n";
        cin >> user_age_1;
    
    
        cout << "user 2 enter your age" << "\n";
        cin >> user_age_2;
    
    
        if (user_age_1 < user_age_2)
        {
            cout << "user number 2 is older than user number 1" << "\n";
        }
    
    
        else
        {
            cout << "user number 1 is older than user number 2" << "\n";
        }
        
        else if (user_age_1 > 100 && user_age_2 > 100)
        {
            cout << "both users are over a 100 years old" << "\n";
        }
    
    
    
    
        return 0;
    
    
    }
    The error i'm getting is at the else-if-line.

    "No match for operator> in 'user_age_2 > 100'"

    I've also google'd this matter but haven't found a solution that would relate to my problem (I think... )

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You declared user_age_2 to be a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    12
    Hey! Noob here as well.

    Your problem seems to lie at line 32. You put the "else" statement before your "else if" statement, this is a common newbie mistake so don't fret. Before continuing on, I would highly recommend you change your variable type from "string" to "int". So no that we've taken care of those two major road blocks, my next advice and really the only option here, is to throw out the "else" statement" and just add in another "else if" statement. I've rewritten your code below,

    Code:
    #include <iostream>
    using namespace std;
    
    
    int User_Age_1, User_Age_2;
    
    
    int main()
    {
       cout << "User 1- Enter your age: ";
       cin >> User_Age_1;
       cout << "User 2- Enter your age: ";
       cin >> User_Age_2;
    
    
       if (User_Age_1 < User_Age_2)
       {
           cout << "User 2 is older than User 1.";
       }
       else if (User_Age_1 > User_Age_2)
       {
           cout << "User 1 is older than User 2.";
       }
       else if (User_Age_1 > 100 && User_Age_2 > 100)
       {
           cout << "Both users are older than 100.";
       }
       else if (User_Age_1 == User_Age_2)
       {
           cout << "Both users are the same age.";
       }
       return 0;
    }
    Hope this helps!
    Last edited by C_Plus_Plus_96; 11-22-2013 at 10:53 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by C_Plus_Plus_96 View Post
    Hope this helps!
    not as much as you'd think. you gave the OP a full solution. this is against the rules. how is anyone supposed to learn if you just give them the answers?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    12
    Quote Originally Posted by Elkvis View Post
    not as much as you'd think. you gave the OP a full solution. this is against the rules. how is anyone supposed to learn if you just give them the answers?
    How is what I've done against forum policy? I've read the guidelines various times. It clearly specifies that those posting cannot ask for other to complete their program, nothing about us answering someone's problem on our own accord. Obviously, I know we shouldn't just give someone a answer, this forum wasn't made for that, but for those looking to get advice so they may solve the problems themselves. So I apologize for not giving a more indepth explanation with less code, but it is not against the rules.

    I do respect you Elkvis, and I hope you don't see my reply as rampaging. If I am in the wrong, please notify a moderator to tell me otherwise.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I think you could have explained the necessary course of action without providing the full code of a solution. in most cases, this is preferred over simply handing out code.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    12
    Quote Originally Posted by Elkvis View Post
    I think you could have explained the necessary course of action without providing the full code of a solution. in most cases, this is preferred over simply handing out code.
    I agree with you completely, sorry for any frustration. I'll keep that in mind for future answerings to posts on here.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I still think the last else if should be an else.
    Global variables should be avoided, too. There is no reason for the variables to be local in this example, either, so I would make them local.
    Also remember that while you can order strings (i.e. some_string < some_other_string), it is not the same as comparing two integers. Case in point: You may find that "100" < "2" hold for strings, but it definitely does not hold for integers!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    I still think the last else if should be an else.
    Mmm, well, I like that the test is there; otherwise you have to reason about whether that else works. I don't think the actual test is a problem for the compiler to omit.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    There is no reason for the variables to be local in this example, either, so I would make them local.
    just to help avoid confusion, I'm going to go out on a limb here, and assume that you meant:

    There is no reason for the variables to be global in this example, either, so I would make them local.
    italics to show the change.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Except quotes are all italics, so it is kind of hard to know where the emphasis is, so I am going to guess that you meant...
    There is no reason for the variables to be global in this example, either, so I would make them local.
    ...and if so, you are right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Except quotes are all italics, so it is kind of hard to know where the emphasis is, so I am going to guess that you meant....
    indeed. silly forum, doing what I said, instead of what I meant.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Quote Originally Posted by C_Plus_Plus_96 View Post
    Hey! Noob here as well.

    Your problem seems to lie at line 32. You put the "else" statement before your "else if" statement, this is a common newbie mistake so don't fret. Before continuing on, I would highly recommend you change your variable type from "string" to "int". So no that we've taken care of those two major road blocks, my next advice and really the only option here, is to throw out the "else" statement" and just add in another "else if" statement. I've rewritten your code below,



    Hope this helps!
    I actually did try switching places between the else if and the else-command, didn't seem to work, maybe it was because i used string instead of int? Anyway, thank you for your answer!

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Quote Originally Posted by russiancircles View Post
    I actually did try switching places between the else if and the else-command, didn't seem to work, maybe it was because i used string instead of int? Anyway, thank you for your answer!
    Your final statement of an if else-if block of code should always be just else.

    Such as:
    Code:
    if(something)
    {
    	do something
    }
    else if(something different)
    {
    	do something else
    }
    else
    {
    	do something else
    }
    Last edited by Ewiv; 11-25-2013 at 05:04 PM.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by russiancircles
    I actually did try switching places between the else if and the else-command, didn't seem to work, maybe it was because i used string instead of int?
    It didn't work because it is syntactically incorrect. Every else must have a matching if, so this:
    Code:
    if (user_age_1 < user_age_2)
    {
        cout << "user number 2 is older than user number 1" << "\n";
    }
    else
    {
        cout << "user number 1 is older than user number 2" << "\n";
    }
    else if (user_age_1 > 100 && user_age_2 > 100)
    {
        cout << "both users are over a 100 years old" << "\n";
    }
    is equivalent to:
    Code:
    if (user_age_1 < user_age_2)
    {
        cout << "user number 2 is older than user number 1" << "\n";
    }
    else
    {
        cout << "user number 1 is older than user number 2" << "\n";
    }
    else
    {
        if (user_age_1 > 100 && user_age_2 > 100)
        {
            cout << "both users are over a 100 years old" << "\n";
        }
    }
    Obviously, the second else does not have a matching if. On the other hand this:
    Code:
    if (User_Age_1 < User_Age_2)
    {
        cout << "User 2 is older than User 1.";
    }
    else if (User_Age_1 > User_Age_2)
    {
        cout << "User 1 is older than User 2.";
    }
    else if (User_Age_1 > 100 && User_Age_2 > 100)
    {
        cout << "Both users are older than 100.";
    }
    else if (User_Age_1 == User_Age_2)
    {
        cout << "Both users are the same age.";
    }
    is equivalent to:
    Code:
    if (User_Age_1 < User_Age_2)
    {
       cout << "User 2 is older than User 1.";
    }
    else
    {
        if (User_Age_1 > User_Age_2)
        {
            cout << "User 1 is older than User 2.";
        }
        else
        {
            if (User_Age_1 > 100 && User_Age_2 > 100)
            {
                cout << "Both users are older than 100.";
            }
            else
            {
                if (User_Age_1 == User_Age_2)
                {
                    cout << "Both users are the same age.";
                }
            }
        }
    }
    Clearly, every else has a matching if. There is an if with no else, but that is not a problem.

    Quote Originally Posted by Ewiv
    Your final statement of an if else-if block of code should always be just else.
    That's not accurate, as C_Plus_Plus_96 demonstrated in post #3 and as I have elaborated on in this post.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help (noob problem)
    By Cheyne in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2013, 11:43 PM
  2. Another Noob Problem
    By TheDeathryder in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2012, 11:30 AM
  3. noob problem in code
    By Dadude in forum C Programming
    Replies: 12
    Last Post: 02-07-2009, 10:28 AM
  4. C++ Problem, I'm such a noob :d
    By Shamino in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2005, 09:52 AM
  5. [noob] i can't even name the problem
    By knoxville in forum C Programming
    Replies: 19
    Last Post: 03-15-2005, 06:32 AM