C++ error messages

Discussion in 'Mac Programming' started by thinspired98, Jul 22, 2008.

  1. thinspired98 macrumors newbie

    Joined:
    Jul 22, 2008
    #1
    wont compile,
    keep getting these error messages (prog. below)
    In function `void calculate_Bill(double&, double&, double&, double&)':
    expected `}' at end of input
    expected `while' at end of input
    `(' at end of input
    expected primary-expression at end of input



    #include <iostream>
    using namespace std;

    void calculate_Bill (double&, double&, double&, double&); //prototype
    void printBill (double, double, double, double); //prototype

    int main () //main function
    {

    double num_items;
    double total_cost, least_exp, most_exp;

    calculate_Bill (num_items, total_cost, least_exp, most_exp);
    //calls calculate_Bill
    system ("PAUSE");
    return 0; //end main function
    }

    void calculate_Bill (double& num_items, double& total_cost, double& least_exp, double& most_exp)
    {

    int cost_item;
    char answer = 'n';

    num_items = 0;
    total_cost = 0;
    least_exp = cost_item;
    most_exp = 0;

    do
    {
    const int COST = 0;
    int i, array[COST];

    for(i = 0; i < COST; i++) //populate the array with user input
    {

    cout<<"Please enter the cost of item#" <<array <<" (zero or negative # to exit): $";
    cin>>array;

    }

    while (cost_item > 0)
    {
    total_cost = total_cost + cost_item;

    if (cost_item < least_exp)
    least_exp = cost_item;

    if (cost_item > most_exp)
    most_exp = cost_item;


    const int COST = 0;
    int i, array[COST];

    for(i = 0; i < COST; i++) //populate the array with user input
    {

    cout<<"Please enter the cost of item#" <<array <<" (zero or negative # to exit): $";
    cin>>array;


    }

    num_items--;
    printBill (num_items, total_cost, least_exp, most_exp); //calls printBill
    num_items = 0;

    cout<<"Would you like to continue with the next customer?";
    cin>>answer;

    }
    while (answer == 'y' || answer =='Y');
    cout<<endl;
    //end calculate Bill
    {
    void printBill (double num_items, double total_cost, double least_exp, double most_exp);
    {
    if (total_cost >= 500)

    cout<<"STOP...Please show the sales associate your ID before you check out!"<<endl;
    cout<<endl;
    }
    cout<<array<<endl;
    cout<<"-------------------------------------------"<<endl;
    cout<<"Number of items: "<<num_items<<endl;
    cout<<"Total cost of all items: $"<<total_cost<<endl;
    cout<<"Least expensive item: $"<<least_exp<<endl;
    cout<<"Most expensive item: $"<<most_exp<<endl;
    cout<<"-------------------------------------------"<<endl;
    cout<<endl;
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Please put code in code tags.

    I started trying to fix this, but at some point the logic totally broke down and I could not tell where certain blocks were supposed to end. You have 8 opening curly braces and 5 closing curly braces in the source file. You need to close some control blocks, but as I said I couldn't figure out where to do so.

    There is a while loop whose condition is that a variable cost_item is greater than 0. This variable is never initialized or assigned to at all. This will yield completely indeterminate behavior.

    You have if statements that do not have curly braces, and the expression executed if true is on the next line. This is terrible for readability.

    I'm reasonably confident that the line after "//end calculate bill" should be a closing curly brace, not an opening curly brace. If that is the case we'd be down to 7 opening braces and 6 closing braces. This means we're only off by one, and it seems to be in calculate_Bill (normally convention is camel case or _s, not both). There is a for nested in a while nested in a do-while, it looks like, but where each is supposed to end is very difficult to say.

    The comments that are included definitely don't clarify things. Anyone who ought to be reading your code can probably decipher when a function call is being made. Some in-code documentation about the intent of control structures would be more helpful (beyond "loop until x is greater than 10", but actually why), instead.

    Apologies as this is probably more brusk than I'd normally be, I'm not in a great mood, but hopefully some of this was helpful.

    -Lee
     

Share This Page