Thread: Can someone please help me with my errors?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    60

    Can someone please help me with my errors?

    Here is my code:
    Code:
    //name
    //CSCI 1010 Section 3
    //Assignment 4
    //9.28.06
    //The purpose of this program is to price an entered number of shirts.  More shirts means less cost per shirt.
    
    #include<iostream>
    using namespace std;
    
    int main ()
    {
            int shirts, price;
            cout << "Welcome to Expensive Shirts Dot Com!" << endl;
            cout << "Enter the number of shirts you wish to purchase:" << endl;
            cin >> shirts;
            if (shirts < 0)
            {
                    cout << "ERROR: You must enter a positive number. We only sell shirts. We do not buy them." << endl;
            else if (shirts < 3)
                    {
                    price = shirts * 12;
                    cout << "Total cost of " << shirts << " shirts: $" << price << endl;
                    }
            else if (shirts < 10)
                    {
                    price = shirts * 10;
                    cout << "Total cost of " << shirts << " shirts: $" << price << endl;
                    }
            else if (shirts < 24)
                    {
                    price = shirts * 8;
                    cout << "Total cost of " << shirts << " shirts: $" << price << endl;
                    }
            else if (shirts > 24)
                    cout << "Sorry, you cannot order more than 24 shirts." << endl;
            }
    return 0;
    }

    Here is the error message when i tried to compile:
    Code:
                                                                                                                                  
    a4xxxxxxxl.cpp: In function `int main()':
    a4xxxxxxx.cpp:19: syntax error before `else'
    a4xxxxxxx.cpp: At global scope:
    a4xxxxxxx.cpp:37: syntax error before `return'

  2. #2
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    you don't have the opening curly bracket after the last else if (shirts > 24)

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    didnt work. i didnt think i needed one since it has only one line in it. the ; closes it

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    last else doenst need if, ill try that

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    fixed one error by taking if out of the last else.

    new error message:
    Code:
    a4xxxxxxx.cpp: In function `int main()':
    a4xxxxxxx.cpp:19: syntax error before `else'
    a4xxxxxxx.cpp:35: syntax error before `<<' token

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you have a series of if, else if statements, you don't enclose all the else ifs inside the if braces. Instead of this:
    Code:
    if ()
    {
        else if
        {
        }
        else if
        {
        }
    }
    you should be doing this:
    Code:
    if ()
    {
    }
    else if
    {
    }
    else if
    {
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    k, did that, it is still giving errors:

    Code:
    a4xxxxxxx.cpp: In function `int main()':
    a4xxxxxxx.cpp:35: syntax error before `<<' token
    Last edited by WinterInChicago; 09-22-2006 at 01:43 PM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Did you change it to:
    Code:
    else(shirts > 24)
         cout << "Sorry, you cannot order more than 24 shirts." << endl;
    This would cause an error, because else cannot have any "parameters".

    If you have else put correctly, then you should try adding braces to that else, because C++ is a free-form language and this would look suspicious:
    Code:
    else cout << "Sorry, you cannot order more than 24 shirts." << endl;
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    thanks much.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Which one was the error?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM