Thread: Working on homework, hit a snag

  1. #1
    Registered User devisezni's Avatar
    Join Date
    Sep 2012
    Location
    North Carolina
    Posts
    5

    Working on homework, hit a snag

    Hey guys,

    I'm in my first year of community college, trying to learn C++ for the first time. I'm running into a snag on my homework though. I've got 99% of the code, but continually get the same error when i try to compile it.

    The code is supposed to Ask whether someone is an Instate student. If yes, tuition =

    here's the code:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        int numCredHour = 0;
        double totTuition = 0;
        char letter = ' ';
        
        
        cout<<"Are You An In-State Student?"<<endl;
        cin>>letter;
        cout<<"How Many Credit Hours Do You Have?"<<endl;
        cin>>numCredHour;
        
        if(toupper(letter) == 'Y'&& numCredHour < 10)
        {
               totTuition=numCredHour*50;
               cout<<"Your Total Tuition is: $"<<totTuition<<endl;
               }
                else(numCredHour>10);
                 totTuition=500;
                  cout<<"Your Total Tuition is: $500"<<endl;
        if (toupper(letter) == 'N' && numCredHour<15);
        {
               totTuition=numCredHour*400;
               cout<<"Your Total Tuition is: $"<<totTuition<<endl;
               }
        else (numCredHour>15);
               totTuition=6000;
               cout<<"Your Total Tuition is: $6000"<<endl;
    system("pause");
    return 0;
    }
    The error code i get is:
    30 C:\Users\******\Prog1.cpp expected primary-expression before "else"
    and
    30 C:\Users\******\Prog1.cpp expected `;' before "else"
    Any ideas as to what might fix my issue would be greatly appreciated. I'll be checking back here every couple of minutes while I work on the next few programs. Thanks for any and all who help!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
                else(numCredHour>10);
                 totTuition=500;
                  cout<<"Your Total Tuition is: $500"<<endl;
    in line 1 the semicolon must be removed and actually i want to remind you to use braces when you are a beginner.When not using braces the compiler assumes that body of the if statement is only the very next line.No error is going to be produced ,but a possible logical error is on the way

  3. #3
    Registered User devisezni's Avatar
    Join Date
    Sep 2012
    Location
    North Carolina
    Posts
    5
    Okay, I think I get what you are saying... So it should look more like this:

    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {
        int numCredHour = 0;
        double totTuition = 0;
        char letter = ' ';
        
        
        cout<<"Are You An In-State Student?"<<endl;
        cin>>letter;
        cout<<"How Many Credit Hours Do You Have?"<<endl;
        cin>>numCredHour;
        
        if(toupper(letter) == 'Y' && numCredHour < 10)
        {
               totTuition=numCredHour*50;
               cout<<"Your Total Tuition is: $"<<totTuition<<endl;
               }
                else(numCredHour>10)
                {
                 totTuition=500;
                  cout<<"Your Total Tuition is: $500"<<endl;
                  }
        if (toupper(letter) == 'N' && numCredHour<15)
        {
               totTuition=numCredHour*400;
               cout<<"Your Total Tuition is: $"<<totTuition<<endl;
               }
        else (numCredHour>15)
        {
               totTuition=6000;
               cout<<"Your Total Tuition is: $6000"<<endl;
               }
    system("pause");
    return 0;
    }
    EDIT: After re-writing and compiling I get error codes stating i need a ; before each {
    Last edited by devisezni; 09-18-2012 at 01:37 PM. Reason: Failed To Compile program

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Can you please post what the compiler actually says?

  5. #5
    Registered User devisezni's Avatar
    Join Date
    Sep 2012
    Location
    North Carolina
    Posts
    5
    Not a problem Dev -C++ compiler is what i am using.

    24 C:\Users\*****\Prog1.cpp expected `;' before '{' token
    34 C:\Users\*****\Prog1.cpp expected `;' before '{' token

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > else (numCredHour>15)
    else doesn't have an expression.

    You might have meant
    else if (numCredHour>15)

    But the final else is just that - everything else not matching any previous if / else if
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if (toupper(letter) == 'N' && numCredHour<15)
    {
        totTuition=numCredHour*400;
        cout<<"Your Total Tuition is: $"<<totTuition<<endl;
    }
    else (numCredHour>15)
    {
        totTuition=6000;
        cout<<"Your Total Tuition is: $6000"<<endl;
    }
    A quick review shows this mistake - the proper syntax is as follows:

    Code:
    if (condition)
    {
        // executes if condition is TRUE
    }
    else
    {
        // executes if condition is FALSE
    }
    Whereas you have:

    Code:
    else (numCredHour>15)
    That extra line after the "else" statement does not belong there.

    If you want the "else" to test another condition, then the proper syntax would be:

    Code:
    if (condition1)
    {
        // executes if condition1 is TRUE
    }
    else if (condition2)
    {
        // executes if condition1 is FALSE and condition2 is TRUE
    }
    [EDIT] Too slow...

  8. #8
    Registered User devisezni's Avatar
    Join Date
    Sep 2012
    Location
    North Carolina
    Posts
    5
    I love you guys! Everyone was a lot of help. here's the new and updated program code that works flawlessly so far through 5 examples Again, thank you!

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        int numCredHour = 0;
        double totTuition = 0;
        char letter = ' ';
        
        
        cout<<"Are You An In-State Student?"<<endl;
        cin>>letter;
        cout<<"How Many Credit Hours Do You Have?"<<endl;
        cin>>numCredHour;
        
        if(toupper(letter) == 'Y' && numCredHour < 10)
        {
               totTuition=numCredHour*50;
               cout<<"Your Total Tuition is: $"<<totTuition<<endl;
               }
                if(numCredHour>10 && toupper(letter) == 'Y')
                {
                 totTuition=500;
                  cout<<"Your Total Tuition is: $500"<<endl;
                  }
        if (toupper(letter) == 'N' && numCredHour<15)
        {
               totTuition=numCredHour*400;
               cout<<"Your Total Tuition is: $"<<totTuition<<endl;
               }
        else if(numCredHour>15)
        {
               totTuition=6000;
               cout<<"Your Total Tuition is: $6000"<<endl;
               }
    system("pause");
    return 0;
    }
    If you know of a better way for me to write this up, any and all advice is welcome. Knowledge is my drug

    EDIT: Found a little snag in it, but fixed it with the advice you guys gave me. Changed the code i had here, and put up the new one. THanks again for all the help
    Last edited by devisezni; 09-18-2012 at 01:54 PM. Reason: found and fixed last error

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    It also help to format your code consitently so you can actually see the blocks clearly. See this
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Salem View Post
    > else (numCredHour>15)
    else doesn't have an expression.

    You might have meant
    else if (numCredHour>15)

    But the final else is just that - everything else not matching any previous if / else if
    My only excuse is that i just arrived in swiss for a scholarship and i am 'out of my environment'...

  11. #11
    Registered User devisezni's Avatar
    Join Date
    Sep 2012
    Location
    North Carolina
    Posts
    5
    Quote Originally Posted by WaltP View Post
    It also help to format your code consitently so you can actually see the blocks clearly. See this
    Thanks! That helps a lot! my main problem was i was using tabs. lol Now I know why it wasn't acting right! and 'white space' does definitely clean it up.

    EDIT: If I run into any more snags, I'll definitely come back to this forum. Everyone was a big help! Wish me luck on the last 3 programs.
    Last edited by devisezni; 09-18-2012 at 02:04 PM.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    else(numCredHour>10);
     totTuition=500;
      cout<<"Your Total Tuition is: $500"<<endl;
    Quote Originally Posted by Salem View Post
    > else (numCredHour>15)
    else doesn't have an expression.

    You might have meant
    else if (numCredHour>15)

    But the final else is just that - everything else not matching any previous if / else if
    I recall a very very old thread:

    N00b with n00b question (probably a quick one)

    I honestly don't know why I remember it through...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  2. Homework Help
    By shadowctr in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2005, 07:29 PM
  3. Homework =)
    By Raeliean in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 10:27 PM
  4. Just a little snag
    By StarOrbs in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 01:39 PM
  5. Windows upgrade snag
    By Abiotic in forum Tech Board
    Replies: 4
    Last Post: 10-26-2003, 04:40 PM

Tags for this Thread