Hello! Beginner C++ student with an issue with methods
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Hello! Beginner C++ student with an issue with methods

Hybrid View

  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Hello! Beginner C++ student with an issue with methods

    Ok, my program right now works I just need it to do some more things.

    First- I need for when the user gets to the bool function and enters 'N' it should go back to the checkout function.

    Secondly, when the user logs out it should display the totals for total weight of each product that was sold, gross total sales amount, total of actual checkout amounts (discounted), and total discount amount grossed for everytime the checkout was ran. I really have no idea how to do this.

    any help is appreciated.

    #include <iostream>
    #include <string>

    using namespace std;

    /*
    *
    */

    int checkout ()
    {

    float produce = 1, pounds = 0, total = 0, finTotal = 0, finPounds = 0, count = 0;


    cout << "Please enter the following: " << endl << "1 for Bananas" << endl;
    cout << " 2 for Apples " << endl << "3 for Cucumbers" << endl;
    cout << " 4 for Carrots" << endl << "5 for Oranges" << endl;
    cout << endl << "0 for Final Total" << endl;

    while (produce != 0){
    cout << "Enter type of Produce:";
    cin >> produce;

    if (produce == 1){
    cout << "Enter amount in pounds" << endl;
    cin >> pounds;
    total = pounds * .44;
    finTotal += total;
    finPounds += pounds;
    }

    else if (produce == 2){
    cout << "Enter amount in pounds"<< endl;
    cin >> pounds;
    total = pounds * .99;
    finTotal += total;
    finPounds += pounds;
    }

    else if (produce == 3){
    cout << "Enter amount in pounds"<< endl;
    cin >> pounds;
    total = pounds * 1.19;
    finTotal += total;
    finPounds += pounds;
    }

    else if (produce == 4){
    cout << "Enter amount in pounds"<< endl;
    cin >> pounds;
    total = pounds * .89;
    finTotal += total;
    finPounds += pounds;
    }
    else if (produce == 5){
    cout << "Enter amount in pounds"<< endl;
    cin >> pounds;
    total = pounds * .79;
    finTotal += total;
    finPounds += pounds;
    }
    else if (produce == 0){

    if (finTotal >= 50){
    cout << "Your total weight is "<< finPounds << endl;
    cout << "Your total is $" << finTotal << endl;
    finTotal = finTotal * .5;
    cout << "Your discounted total is $"<< finTotal << endl;
    count += 1;
    }
    else{
    cout << "Your total weight is "<< finPounds << "lbs."<< endl;
    cout << "You total is $" << finTotal << "." << endl;
    }

    }

    else{
    cout << "Incorrect input"<< endl;
    produce = 1;
    }


    }

    }
    bool logout ()

    {
    bool quit = true;
    cout << "Are you ready to quit?" << endl;
    cin >> quit;
    if (quit == 'N'){
    checkout();
    }
    }



    int main(int argc, char** argv) {
    checkout();

    return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Hello! Beginner C++ student with an issue with methods

    Quote Originally Posted by UrbanSidhe View Post
    Ok, my program right now works I just need it to do some more things.
    1) Use code tags when posting code. The code you posted is almost unreadable.

    2) Really, learn to use arrays and structs. What if you had 300 products? Would you write 300 if() statements? What if there was a price, product code, and many other items associated with the product? The main reason why it is difficult to complete your assignment is that you're using very basic C++ constructs (simple variables for example), so it becomes overwhelming using these basic constructs.

    3) Look at your if() statements -- the only difference is the price -- everything else is exactly the same. Doesn't this look like it's time to organize the code a little better?

    4) Your "produce" variable should be an int, not float.

    I can see that you know very basic C++ in terms of writing a program "linearly". You need to move on and learn even higher constructs such as arrays and put them to use. Your program is evidence that arrays should be used, just by looking at the repeated nature of your logic (again, what if there were 300 products?).

    Here is an example:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct ProduceInfo
    {
       std::string produceName;
       double producePrice;
       double produceTotal;
       double producePounds;
    };
    
    // Create an array of the above, and initialize all the members with appropriate values.
    ProduceInfo allProduce[] = {{"Bananas", 0.44, 0, 0}, 
                                 {"Apples",  0.99, 0, 0}, 
                                 {"Cucumbers", 1.19, 0, 0}, 
                                 {"Carrots", 0.89, 0, 0}, 
                                 {"Oranges", 0.79, 0, 0}};
    
    int checkout ()
    {
        int produce;
        double pounds;
        cout << "Please enter the following: " << endl << "1 for Bananas" << endl;
        cout << " 2 for Apples " << endl << "3 for Cucumbers" << endl;
        cout << " 4 for Carrots" << endl << "5 for Oranges" << endl;
        cout << endl << "0 for Final Total" << endl;    
    
        do 
        {
            cout << "Enter type of Produce:";
            cin >> produce; 
            if ( produce >= 1 && produce <= 5 )
            {
                int whichItem = produce - 1; 
                cout << "Enter amount in pounds of this item: " << allProduce[whichItem].produceName << endl;
                cin >> pounds;
                allProduce[whichItem].produceTotal += pounds * allProduce[whichItem].producePrice;
                allProduce[whichItem].producePounds += pounds;
            }
        } while (produce != 0 );
        // ... final totals
       return 0;
     }
    The code above is in a nutshell, what you originally wrote (a few prompts are missing, but that isn't important). There are no repeated if() statements, as the produce name, price, and totals are members of the struct ProduceInfo -- my code just gets the right ProduceInfo, given the input from the user.

    Also note that I created an array of ProduceInfo and initialized the array with all the information your original code had. Given the above, it should be straightforward as to how to complete your assignment. Also, the loop was changed from a while() loop to a do-while loop. The reason is that you're going to loop at least once, and the best construct for that is to use do-while().

    If you don't know what the code does, then I suggest you read the chapter in your book(s) on arrays and structs, and go through the simple program a step at a time using your debugger.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 22nd, 2012 at 05:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center