13 Replies - 1492 Views - Last Post: 14 July 2012 - 01:51 AM Rate Topic: -----

#1 Xinzin  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 13-July 12

How to include a for loop to keep a running total

Posted 13 July 2012 - 07:18 PM

Hello All. My name is Xinzin, and like many, I would like some assistance. However before we go much further I will describe my very brief history with C++ programming to better inform you how you can help. I am currently taking a summer course for c++ for the past 3 weeks, that said, I only know the extreme basics. cout, cin, basic operators, if statements, if else statements, if else if statements, and just recently the while, do while and for loops. That said, any advanced answers will not help me in the least.

Anyway, my prof had an assignment to write our own piece of code, which should include the three loops we have learned in class. I decided to create grocery store application in which users can select between regular price, clearance, and special discount items to purchase. It is nearly complete; however I would like to add in a running total using the for loop at the very end, where the menu asks to quit (if you wish to skip ahead, the last default option).

As for loops are my weakest suit currently, I have tried reading tutorials on how to apply the concepts to a running total, but I don't understand. Any help? Thanks in advance.

Also, the code runs fine.

#include <iostream>
#include <iomanip>
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) { 
    // Constants for menu choices
 const int Regular = 1,
 Clearance = 2,
 Discount = 3,
 QUIT_CHOICE = 4;
 unsigned short choice; 
  do {
    //Display the menu
    cout<<"Type 1 if you would like to purchase Regular priced items"<<endl;
    cout<<"Type 2 if you would like to purchase Clearance items"<<endl;
    cout<<"Type 3 if you would like to purchase Special Discount items"<<endl;
    cout<<"Type 4 if you would like to quit" <<endl;
    //Declare and input the choice
     cin>>choice;
    // Validate the menu selection.
        while (choice < Regular || choice > QUIT_CHOICE) 
        {
                cout << "Please enter a valid menu choice: ";
                cin >> choice;
        }
     //Based upon the choice, proceed to next menu
    switch(choice){
      case 1:{
        // Display the Regular priced menu
        cout<<"Type A if you would like to purchase brand name Cookies "<<endl;
        cout<<"Type B if you would like to purchase brand name Juice "<<endl;
        cout<<"Type C if you would like to purchase brand name Cereal "<<endl;
        cout<<"Type D if you would like to purchase Brand name Crackers "<<endl;
        cout<<"Type E if you would like to purchase Brand name Oatmeal "<<endl;
        //Declare and input choice      
        char choice1;
        cin>>choice1;
       //Based upon the choice, solve the particular problem
     switch(choice1){
      case 'A':{
        // Ask the user how many cookies they want
        cout<<"How many boxes of cookies would you like to buy?"<<endl;
        int Cookies;
        const float Cprice = 5.6, Tax = .0775;
        float Price;
        cin>>Cookies;
        Price = (Cookies * Cprice) + (Cookies * Cprice * Tax);
        cout<<"The price of the cookies are : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
        break;
                }
         
      case 'B':{
        // Ask the user how many Juices they want
        cout<<"How many cans of juice would you like to buy?"<<endl;
        int Juice;
        const float Jprice = 5.2, Tax = .0775;
        float Price;
        cin>>Juice;
        Price = (Juice * Jprice) + (Juice * Jprice * Tax);
        cout<<"The price of the Juice is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
        break;
                }
        
       case 'C':{
         // Ask the user how many boxes of Cereal they want
         cout<<"How many boxes of cereal would you like to buy?"<<endl;
         int Cereal;
         const float Clprice = 5.2, Tax = .0775;
         float Price;
         cin>>Cereal;
         Price = (Cereal * Clprice) + (Cereal * Clprice * Tax);
         cout<<"The price of the cookies are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
         break;
                }
                
       case 'D':{
         // Ask the user how many boxes of crackers they want
         cout<<"How many boxes of crackers would you like to buy?"<<endl;
         int Crack;
         const float Crackprice = 5.2, Tax = .0775;
         float Price;
         cin>>Crack;
         Price = (Crack * Crackprice) + (Crack * Crackprice * Tax);
         cout<<"The price of the crackers are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
         break;
                 }
                
       case 'E':{
         // Ask the user how boxes oatmeal they want
         cout<<"How many boxes of oatmeal would you like to buy?"<<endl;
         int Oats;
         const float Oprice = 5.2, Tax = .0775;
         float Price;
         cin>>Oats;
         Price = (Oats * Oprice) + (Oats * Oprice * Tax);
         cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
         break;
                }
       default:{
         cout<<"You do not know how to follow instructions, do you?"<<endl;
               }
       break;
                   }
       break; 
          }
       
      case 2:{
        // Display the Clearance priced menu
        cout<<"Type a if you would like to purchase clearance Cookies "<<endl;
        cout<<"Type b if you would like to purchase clearance Juice "<<endl;
        cout<<"Type c if you would like to purchase clearance Cereal "<<endl;
        cout<<"Type d if you would like to purchase clearance Crackers "<<endl;
        cout<<"Type e if you would like to purchase clearance Oatmeal "<<endl;
        //Declare and input choice      
        char choice2;
        cin>>choice2;
       //Based upon the choice, solve the particular problem
     switch(choice2){
        case 'a':{
            // Ask the user how many cookies they want
            cout<<"How many boxes of cookies would you like to buy?"<<endl;
            int ClearCookies;
            const float OCprice =5.6, ClCprice = 4.0, Tax = .0775;
            float Oprice, Price, Diff, Savings; // Diff = difference in price, Savings = % you saved
            cin>>ClearCookies;
            Price = (ClearCookies * ClCprice) + (ClearCookies * ClCprice * Tax);
            Oprice =(ClearCookies * OCprice) + (ClearCookies * OCprice * Tax);
            Diff = Oprice - Price;
            Savings = 100 - (Price / Oprice) * 100 ;
            cout<<"The price of the cookies are : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Savings<<" %"<<endl;
            break;
                   }
                
         case 'b':{
            // Ask the user how many Juices they want
            cout<<"How many cans of juice would you like to buy?"<<endl;
            int ClJuice;
            const float OJJprice = 5.2, ClJprice = 3.6, Tax = .0775;
            float Price, OJYPrice, Diff, Sav;
            cin>>ClJuice;
            Price = (ClJuice * ClJprice) + (ClJuice * ClJprice * Tax);
            OJYPrice = (ClJuice * OJJprice) + (ClJuice * OJJprice * Tax);
            Diff = OJYPrice - Price;
            Sav = 100 - (Price / OJYPrice) * 100;
            cout<<"The price of the Juice is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                   }
                
         case 'c':{
            // Ask the user how many boxes of Cereal they want
            cout<<"How many boxes of cereal would you like to buy?"<<endl;
            int ClCereal ;
            const float OClprice = 5.2, ClCrprice = 2, Tax = .0775;
            float Price, Ocer, Diff, Sav; // Ocer=OriginalCeral Price
            cin>>ClCereal;
            Price = (ClCereal * ClCrprice) + (ClCereal * ClCrprice * Tax);
            Ocer = (ClCereal * Ocer) + (ClCereal * Ocer * Tax);
            Diff = Ocer - Price;
            Sav = 100 - (Price/ Ocer) * 100;
            cout<<"The price of the cookies are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
             cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                   }
                
         case 'd':{
            // Ask the user how many boxes of crackers they want
            cout<<"How many boxes of crackers would you like to buy?"<<endl;
            int ClCrack;
            const float OCrackprice = 5.2, ClCrackprice = 1.9, Tax = .0775;
            float Ocrack, Price, Diff, Sav;
            cin>>ClCrack;
            Price = (ClCrack * ClCrackprice) + (ClCrack * ClCrackprice * Tax);
            Ocrack = (ClCrack * OCrackprice) + (ClCrack * OCrackprice * Tax);
            Diff = Ocrack - Price;
            Sav = 100 - (Price/Ocrack) * 100;
            cout<<"The price of the crackers are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                    }
                
         case 'e':{
            // Ask the user how boxes oatmeal they want
            cout<<"How many boxes of oatmeal would you like to buy?"<<endl;
            int ClOats;
            const float Oprice = 5.2, ClOprice = 2.3, Tax = .0775;
            float Price, Ooats, Diff, Sav;
            cin>>ClOats;
            Price = (ClOats * ClOprice) + (ClOats * ClOprice * Tax);
            Ooats= (ClOats * Oprice) + (ClOats * Oprice * Tax);
            Diff = Ooats - Price;
            Sav = 100 - (Price / Ooats) * 100;
            cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                    }
        default:{
            cout<<"You do not know how to follow instructions, do you?"<<endl;
                }
        break;
              }
            break;
        }
      case 3:{
            // Display another menu
            cout<<"Press Q if you would like to buy Cheese"<<endl;
            cout<<"Press W if you would like to buy Milk"<<endl;
            cout<<"Press E if you would like to buy Candy"<<endl;
            cout<<"Press R if you would like to buy Tea"<<endl;
            cout<<"Press T if you would like to buy Water"<<endl;
            //Declare and input the choice
            char choice3;
            cin>>choice3;
            //Based upon the choice, proceed to next menu
        switch(choice3){
          case 'Q':{
            // Ask the user how much cheese
            cout<<"How much cheese would you like to buy?"<<endl;
            int Cheese;
            const float Cheeseprice = 3.99, Tax = .0775;
            float Price;
            cin>>Cheese;
                if (Cheese ==1) {
                        Price = (Cheese * Cheeseprice) + (Cheese * Cheeseprice * Tax) -1;
                        cout<<"The price of the cheese after discount is $"<<fixed<<setprecision(2)<<Price<<" "<<endl;
                                }
                else {
                        Price = (Cheese * Cheeseprice) + (Cheese * Cheeseprice * Tax);
                        cout<<"The price of the cheese is : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                        }
            break;
                    }
        
            case 'W':{
                // Ask the user how much Milk they want
                cout<<"How many gallons of Milk would you like to buy?"<<endl;
                int Milk;
                const float Mprice = 3.39, Tax = .0775;
                float Price;
                cin>>Milk;
                if (Milk >= 5 || Milk <= 10) {
                        Price = (Milk * Mprice);
                        cout<<"The price of the milk after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                                              }
                else {
                        Price = (Milk * Mprice) + (Milk * Mprice * Tax);
                        cout<<"The price of the milk is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                     }
                break;
                     }
        
            case 'E':{
                 // Ask the user how many boxes of Candy they want
                cout<<"How many boxes of Candy would you like to buy?"<<endl;
                int Candy;
                const float CanPrice = 1.2, Tax = .0775;
                float Price;
                cin>>Candy;
                if (Candy > 9) {
                        Price = ((Candy * CanPrice) + (Candy * CanPrice * Tax)) - ((CanPrice * Tax ) + CanPrice) ;
                        cout<<"The price of the candy after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                                }
                else {
                        Price = (Candy * CanPrice) + (Candy * CanPrice * Tax)  ;
                        cout<<"The price of the candy is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                     }
                break;
                        }
        
            case 'R':{
                // Ask the user how many tea packets they want
                cout<<"How many packets of tea would you like to buy?"<<endl;
                int Tea;
                const float Tprice = 2.89, Tax = .0775;
                float Price;
                cin>>Tea;
                if (Tea <4) {
                        Price = (Tea * Tprice) + (Tea * Tprice * Tax) * .50;
                        cout<<"The price of the Tea after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                            }
                else{
                        Price = (Tea * Tprice) + (Tea * Tprice * Tax);
                        cout<<"The price of the Tea is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                    }
            break;
                      }
        
            case 'T':{
                // Ask the user how gallons of water  they want
                cout<<"How many gallons would you like to buy?"<<endl;
                int Wat;
                const float Wprice = 1.0, Tax = .0775;
                float Price;
                cin>>Wat;
                if (Wat < 3, Wat > 1) 
                        {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .05;
                        cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                        }
                else if (Wat < 4 , Wat > 2) 
                        {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .06;
                        cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                        } 
                else if (Wat >3) 
                {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .07;
                        cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                }
                else {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax);
                        cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                     }
            break;
                   }
        default:{
            cout<<"You do not know how to follow instructions, do you?"<<endl;
                }
        break;    
                  }    
        break;
        }
        default:{
            cout<<"Why don't you want to shop anymore?"<<endl;
                }
           }
     } while (choice!= QUIT_CHOICE); 
     return 0;
 }



Is This A Good Question/Topic? 0
  • +

Replies To: How to include a for loop to keep a running total

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 495
  • View blog
  • Posts: 1,546
  • Joined: 20-March 10

Re: How to include a for loop to keep a running total

Posted 13 July 2012 - 07:48 PM

Hi,


You say

Quote

Also, the code runs fine.


No it doesn't sorry....

line 304 if (Wat < 3, Wat > 1)
line 309 else if (Wat < 4 , Wat > 2)

The OR operator is ||
The AND operator is &&
which is it ?

line 161 float Price, Ocer, Diff, Sav; // Ocer=OriginalCeral Price

You tell us in a comment that Ocer should be set to something then don't do it ...
What's up with that ?

line 11 + 12 you set up variables Clearance and Discount then don't use them
is this for future expansion ?

Right to your question....

you have a float Price you could have a second float variable at the start of main called

TotalPrice

Then when you buy something you could just do TotalPrice = TotalPrice + Price;
this would give a running total.

Best Wishes

Snoopy.

This post has been edited by snoopy11: 13 July 2012 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 3110
  • View blog
  • Posts: 9,482
  • Joined: 25-December 09

Re: How to include a for loop to keep a running total

Posted 13 July 2012 - 08:06 PM

Also posted here.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Xinzin  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 13-July 12

Re: How to include a for loop to keep a running total

Posted 13 July 2012 - 08:12 PM

Quote

No it doesn't sorry....

What errors are you recieving? My IDE is Netbeans and I am not seeing any problems.

Quote

line 304 if (Wat < 3, Wat > 1)
line 309 else if (Wat < 4 , Wat > 2)

The OR operator is ||
The AND operator is &&
which is it ?

The conditions would be 2 and 3 respectively. I agree it is an ugly code and I should user the "==" operator, but for now I'm looking for functionality. Optimization can come later.

Quote

line 161 float Price, Ocer, Diff, Sav; // Ocer=OriginalCeral Price


You tell us in a comment that Ocer should be set to something then don't do it ...
What's up with that ?


You are absolutely correct. That was a misnamed variable.

Quote

line 11 + 12 you set up variables Clearance and Discount then don't use them
is this for future expansion ?

its for the menu for the do while loop. Its required, unless I am misreading your statement.

Quote

Right to your question....

you have a float Price you could have a second float variable at the start of main called

TotalPrice

Then when you buy something you could just do TotalPrice = TotalPrice + Price;
this would give a running total.

Best Wishes

Snoopy.


How is that utilizing the for loop? I understand it is possible to keep a running total using different methods, but I was hoping I could use the for loop. Are you saying this is not possible?


@ Jim. Yes, I posted it in another C++ forum. Is it a problem I look for help in multiple sources?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 3110
  • View blog
  • Posts: 9,482
  • Joined: 25-December 09

Re: How to include a for loop to keep a running total

Posted 13 July 2012 - 08:23 PM

Quote

Is it a problem I look for help in multiple sources?

Yes, in my opinion it is very rude and selfish. Pick one forum and stick with that forum, if you don't get an answer in a couple of days then try another forum.

Jim

This post has been edited by jimblumberg: 13 July 2012 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#6 Xinzin  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 13-July 12

Re: How to include a for loop to keep a running total

Posted 13 July 2012 - 08:26 PM

Quote

Is it a problem I look for help in multiple sources?

Yes, in my opinion it is very rude and selfish. Pick one forum and stick with that forum, if you don't get an answer in a couple of days then try another forum.

Jim
[/quote]

I apologize, but this assignment was given yesterday, and I only have until Sunday morning to complete it. A few days is something I do not have.
Was This Post Helpful? -1
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 495
  • View blog
  • Posts: 1,546
  • Joined: 20-March 10

Re: How to include a for loop to keep a running total

Posted 13 July 2012 - 11:46 PM

Right,

This is rapidly going into weirdness.

Comma operator ( , )
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.

so in your if statement

line 304 if (Wat < 3, Wat > 1)


only the right hand side is considered, is that what you wanted , if so why do it that way ?

The warning I get is this 'left operand of comma operator has no effect'

I tend to treat warnings as errors

You go on to say

Quote

You are absolutely correct. That was a misnamed variable.


Could you please give us a hint and post your corrected code then with the variable being named
correctly ?

Quote

How is that utilizing the for loop? I understand it is possible to keep a running total using different methods, but I was hoping I could use the for loop. Are you saying this is not possible?


Which for loop ?

There is only a do while loop in your code ...??? :/

Snoopy.

This post has been edited by snoopy11: 13 July 2012 - 11:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 Xinzin  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 13-July 12

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 12:13 AM

tweaked the code a bit. Optimization is still needed as well as more comments, but its slightly more stream lined to show what I wanted to do.

#include <iostream>
#include <iomanip>
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) { 
    // Constants for menu choices
 const int Regular = 1,
 Clearance = 2,
 Discount = 3,
 QUIT_CHOICE = 4;
 float total_so_far = 0; // accumulator
 unsigned short choice; 
  do {
    //Display the menu
    cout<<"Type 1 if you would like to purchase Regular priced items"<<endl;
    cout<<"Type 2 if you would like to purchase Clearance items"<<endl;
    cout<<"Type 3 if you would like to purchase Special Discount items"<<endl;
    cout<<"Type 4 if you would like to quit" <<endl;
    //Declare and input the choice
     cin>>choice;
    // Validate the menu selection.
        while (choice < Regular || choice > QUIT_CHOICE) 
        {
                cout << "Please enter a valid menu choice: ";
                cin >> choice;
        }
     //Based upon the choice, proceed to next menu
    switch(choice){
      case 1:{
        // Display the Regular priced menu
        cout<<"Type A if you would like to purchase brand name Cookies "<<endl;
        cout<<"Type B if you would like to purchase brand name Juice "<<endl;
        cout<<"Type C if you would like to purchase brand name Cereal "<<endl;
        cout<<"Type D if you would like to purchase Brand name Crackers "<<endl;
        cout<<"Type E if you would like to purchase Brand name Oatmeal "<<endl;
        //Declare and input choice      
        char choice1;
        cin>>choice1;
       //Based upon the choice, solve the particular problem
     switch(choice1){
      case 'A':{
        // Ask the user how many cookies they want
        cout<<"How many boxes of cookies would you like to buy?"<<endl;
        int Cookies;
        const float Cprice = 5.6, Tax = .0775;
        float Price;
        cin>>Cookies;
        Price = (Cookies * Cprice) + (Cookies * Cprice * Tax);
        total_so_far += Price;
        cout<<"The price of the cookies are : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
        break;
                }
         
      case 'B':{
        // Ask the user how many Juices they want
        cout<<"How many cans of juice would you like to buy?"<<endl;
        int Juice;
        const float Jprice = 5.2, Tax = .0775;
        float Price;
        cin>>Juice;
        Price = (Juice * Jprice) + (Juice * Jprice * Tax);
        total_so_far += Price;
        cout<<"The price of the Juice is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
        break;
                }
        
       case 'C':{
         // Ask the user how many boxes of Cereal they want
         cout<<"How many boxes of cereal would you like to buy?"<<endl;
         int Cereal;
         const float Clprice = 5.2, Tax = .0775;
         float Price;
         cin>>Cereal;
         Price = (Cereal * Clprice) + (Cereal * Clprice * Tax);
         total_so_far += Price;
         cout<<"The price of the cookies are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
         break;
                }
                
       case 'D':{
         // Ask the user how many boxes of crackers they want
         cout<<"How many boxes of crackers would you like to buy?"<<endl;
         int Crack;
         const float Crackprice = 5.2, Tax = .0775;
         float Price;
         cin>>Crack;
         Price = (Crack * Crackprice) + (Crack * Crackprice * Tax);
         total_so_far += Price;
         cout<<"The price of the crackers are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
         break;
                 }
                
       case 'E':{
         // Ask the user how boxes oatmeal they want
         cout<<"How many boxes of oatmeal would you like to buy?"<<endl;
         int Oats;
         const float Oprice = 5.2, Tax = .0775;
         float Price;
         cin>>Oats;
         Price = (Oats * Oprice) + (Oats * Oprice * Tax);
         total_so_far += Price;
         cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
         break;
                }
       default:{
         cout<<"You do not know how to follow instructions, do you?"<<endl;
               }
       break;
                   }
       break; 
          }
       
      case 2:{
        // Display the Clearance priced menu
        cout<<"Type a if you would like to purchase clearance Cookies "<<endl;
        cout<<"Type b if you would like to purchase clearance Juice "<<endl;
        cout<<"Type c if you would like to purchase clearance Cereal "<<endl;
        cout<<"Type d if you would like to purchase clearance Crackers "<<endl;
        cout<<"Type e if you would like to purchase clearance Oatmeal "<<endl;
        //Declare and input choice      
        char choice2;
        cin>>choice2;
       //Based upon the choice, solve the particular problem
     switch(choice2){
        case 'a':{
            // Ask the user how many cookies they want
            cout<<"How many boxes of cookies would you like to buy?"<<endl;
            int ClearCookies;
            const float OCprice =5.6, ClCprice = 4.0, Tax = .0775;
            float Oprice, Price, Diff, Savings; // Diff = difference in price, Savings = % you saved
            cin>>ClearCookies;
            Price = (ClearCookies * ClCprice) + (ClearCookies * ClCprice * Tax);
            total_so_far += Price;
            Oprice =(ClearCookies * OCprice) + (ClearCookies * OCprice * Tax);
            Diff = Oprice - Price;
            Savings = 100 - (Price / Oprice) * 100 ;
            cout<<"The price of the cookies are : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Savings<<" %"<<endl;
            break;
                   }
                
         case 'b':{
            // Ask the user how many Juices they want
            cout<<"How many cans of juice would you like to buy?"<<endl;
            int ClJuice;
            const float OJJprice = 5.2, ClJprice = 3.6, Tax = .0775;
            float Price, OJYPrice, Diff, Sav;
            cin>>ClJuice;
            Price = (ClJuice * ClJprice) + (ClJuice * ClJprice * Tax);
            total_so_far += Price;
            OJYPrice = (ClJuice * OJJprice) + (ClJuice * OJJprice * Tax);
            Diff = OJYPrice - Price;
            Sav = 100 - (Price / OJYPrice) * 100;
            cout<<"The price of the Juice is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                   }
                
         case 'c':{
            // Ask the user how many boxes of Cereal they want
            cout<<"How many boxes of cereal would you like to buy?"<<endl;
            int ClCereal ;
            const float OClprice = 5.2, ClCrprice = 2, Tax = .0775;
            float Price, Ocer, Diff, Sav; // Ocer=OriginalCeral Price
            cin>>ClCereal;
            Price = (ClCereal * ClCrprice) + (ClCereal * ClCrprice * Tax);
            total_so_far += Price;
            Ocer = (ClCereal * OClprice) + (ClCereal * OClprice * Tax); //fixed variable
            Diff = Ocer - Price;
            Sav = 100 - (Price/ Ocer) * 100;
            cout<<"The price of the cookies are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
             cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                   }
                
         case 'd':{
            // Ask the user how many boxes of crackers they want
            cout<<"How many boxes of crackers would you like to buy?"<<endl;
            int ClCrack;
            const float OCrackprice = 5.2, ClCrackprice = 1.9, Tax = .0775;
            float Ocrack, Price, Diff, Sav;
            cin>>ClCrack;
            Price = (ClCrack * ClCrackprice) + (ClCrack * ClCrackprice * Tax);
            total_so_far += Price;
            Ocrack = (ClCrack * OCrackprice) + (ClCrack * OCrackprice * Tax);
            Diff = Ocrack - Price;
            Sav = 100 - (Price/Ocrack) * 100;
            cout<<"The price of the crackers are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                    }
                
         case 'e':{
            // Ask the user how boxes oatmeal they want
            cout<<"How many boxes of oatmeal would you like to buy?"<<endl;
            int ClOats;
            const float Oprice = 5.2, ClOprice = 2.3, Tax = .0775;
            float Price, Ooats, Diff, Sav;
            cin>>ClOats;
            Price = (ClOats * ClOprice) + (ClOats * ClOprice * Tax);
            total_so_far += Price;
            Ooats= (ClOats * Oprice) + (ClOats * Oprice * Tax);
            Diff = Ooats - Price;
            Sav = 100 - (Price / Ooats) * 100;
            cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
            cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
            cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
            break;
                    }
        default:{
            cout<<"You do not know how to follow instructions, do you?"<<endl;
                }
        break;
              }
            break;
        }
      case 3:{
            // Display another menu
            cout<<"Press Q if you would like to buy Cheese"<<endl;
            cout<<"Press W if you would like to buy Milk"<<endl;
            cout<<"Press E if you would like to buy Candy"<<endl;
            cout<<"Press R if you would like to buy Tea"<<endl;
            cout<<"Press T if you would like to buy Water"<<endl;
            //Declare and input the choice
            char choice3;
            cin>>choice3;
            //Based upon the choice, proceed to next menu
        switch(choice3){
          case 'Q':{
            // Ask the user how much cheese
            cout<<"How much cheese would you like to buy?"<<endl;
            int Cheese;
            const float Cheeseprice = 3.99, Tax = .0775;
            float Price;
            cin>>Cheese;
                if (Cheese ==1) {
                        Price = (Cheese * Cheeseprice) + (Cheese * Cheeseprice * Tax) -1;
                        total_so_far += Price;
                        cout<<"The price of the cheese after discount is $"<<fixed<<setprecision(2)<<Price<<" "<<endl;
                                }
                else {
                        Price = (Cheese * Cheeseprice) + (Cheese * Cheeseprice * Tax);
                        total_so_far += Price;
                        cout<<"The price of the cheese is : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                        }
            break;
                    }
        
            case 'W':{
                // Ask the user how much Milk they want
                cout<<"How many gallons of Milk would you like to buy?"<<endl;
                int Milk;
                const float Mprice = 3.39, Tax = .0775;
                float Price;
                cin>>Milk;
                if (Milk >= 5 || Milk <= 10) {
                        Price = (Milk * Mprice);
                        total_so_far += Price;
                        cout<<"The price of the milk after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                                              }
                else {
                        Price = (Milk * Mprice) + (Milk * Mprice * Tax);
                        total_so_far += Price;
                        cout<<"The price of the milk is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                     }
                break;
                     }
        
            case 'E':{
                 // Ask the user how many boxes of Candy they want
                cout<<"How many boxes of Candy would you like to buy?"<<endl;
                int Candy;
                const float CanPrice = 1.2, Tax = .0775;
                float Price;
                cin>>Candy;
                if (Candy > 9) {
                        Price = ((Candy * CanPrice) + (Candy * CanPrice * Tax)) - ((CanPrice * Tax ) + CanPrice) ;
                        total_so_far += Price;
                        cout<<"The price of the candy after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                                }
                else {
                        Price = (Candy * CanPrice) + (Candy * CanPrice * Tax)  ;
                        total_so_far += Price;
                        cout<<"The price of the candy is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                     }
                break;
                        }
        
            case 'R':{
                // Ask the user how many tea packets they want
                cout<<"How many packets of tea would you like to buy?"<<endl;
                int Tea;
                const float Tprice = 2.89, Tax = .0775;
                float Price;
                cin>>Tea;
                if (Tea <4) {
                        Price = (Tea * Tprice) + (Tea * Tprice * Tax) * .50;
                        total_so_far += Price;
                        cout<<"The price of the Tea after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                            }
                else{
                        Price = (Tea * Tprice) + (Tea * Tprice * Tax);
                        total_so_far += Price;
                        cout<<"The price of the Tea is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                    }
            break;
                      }
        
            case 'T':{
                // Ask the user how gallons of water  they want
                cout<<"How many gallons would you like to buy?"<<endl;
                int Wat;
                const float Wprice = 1.0, Tax = .0775;
                float Price;
                cin>>Wat;
                if (Wat ==2) 
                        {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .05;
                        total_so_far += Price;
                        cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                        }
                else if (Wat == 3) 
                        {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .06;
                        total_so_far += Price;
                        cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                        } 
                else if (Wat >3) 
                {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .07;
                        total_so_far += Price;
                        cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                }
                else {
                        Price = (Wat * Wprice) + (Wat * Wprice * Tax);
                        total_so_far += Price;
                        cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
                     }
            break;
                   }
        default:{
            cout<<"You do not know how to follow instructions, do you?"<<endl;
                }
        break;    
                  }    
        break;
        }
        default:{
            cout<<"Your total is: $"<<fixed<<setprecision(2)<<total_so_far<<endl;
                }
           }
     } while (choice!= QUIT_CHOICE); 
     return 0;
 }



The total, how it is now works fine, and probably the most straight forward method; however, I read in a book that the for loop could be used to keep a running total. I thought I could to the same with my application. Here's the code.

 // This program takes daily sales figures over a period of time
 // and calculates their total.
 #include <iostream>
 #include <iomanip>
 using namespace std;

 int main()
 {
 int days; // Number of days
 double total = 0.0; // Accumulator, initialized with 0

 // Get the number of days.
 cout << "For how many days do you have sales figures? ";

 cin >> days;

 // Get the sales for each day and accumulate a total.
 for (int count = 1; count <= days; count++)
 {
 double sales;
 cout << "Enter the sales for day " << count << ": ";
 cin >> sales;
 total += sales; // Accumulate the running total.
 }

 // Display the total sales.
 cout << fixed << showpoint << setprecision(2);
 cout << "The total sales are $" << total << endl;
 return 0;
 }



Was the reason you did not use a for loop in your initial suggestion because it is overly complicated?
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 495
  • View blog
  • Posts: 1,546
  • Joined: 20-March 10

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 12:30 AM

No,

The reason I just suggested you do TotalPrice = TotalPrice + Price;

Is because it fitted better with your coding style....

I would never actually write a program this way...

But at the end of the day You are here to learn...

So me totally re-writing your code in my style..

In this case wouldn't help...

You still don't use the variables Clearance or Discount

anywhere and this is your final program ??

Strange....

Snoopy.

This post has been edited by snoopy11: 14 July 2012 - 12:31 AM

Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2035
  • View blog
  • Posts: 6,058
  • Joined: 05-May 12

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 12:38 AM

Good code communicates the programmers intent. That for loop you posted shows that the intent was to loop over the number of days. It shows a definite beginning, middle, and end.

In the case of your program, the do-while structure make more sense because you don't know ahead of time how many types of items the buyer is going to buy.

What communicates the intent of the code better:

for(bool buyMore = true; buyMore == true; buyMore = UserWantsToBuyMore())
{
    // get items bought and compute price
    runningTotal += price;
}



Or

do
{
    // get items bought and compute price
    runningTotal += price;
} while (UserWantsToBuyMore());



Or

while (UserWantsToBuyMore())
{
    // get items bought and compute price
    runningTotal += price;
}


Was This Post Helpful? 0
  • +
  • -

#11 Xinzin  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 13-July 12

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 01:03 AM

@ snoopy
I see. To your most recent question: yes and no. The version for this upcoming due date will be similar to this; however, as the class learns about functions arrays etc... in the upcoming weeks, we will be required to edit the code accordingly.

@ sky diver
out of the three, my skill with for loops are the weakest, so I tried to copy and paste your piece of code to see how it would work. I think I was supposed to edit something more, but I get the error each variable is undeclared. I changed the price and running total variable to match the variable names in my program, but it still doesn't work. What did I do wrong?
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 495
  • View blog
  • Posts: 1,546
  • Joined: 20-March 10

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 01:11 AM

Hi,

Skydiver just gave you a generic example,

it wasn't supposed to be taken literally...

In this case for - loops are not that useful.

Just take it as read, that using the do while loop for you
is the best way to go....

I would have implemented 'classes' for this program...

But since you probably haven't covered classes yet there is no point

in even going there.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#13 Xinzin  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 13-July 12

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 01:42 AM

Yup, I understand at my level do while loops are the best for menus. And now seeing how for loops are not effective, I won't try to use it anymore. With that chapter finished, I'm bring a former question Snoopy.

Quote

this is your final program ??

Why do you ask? I admit that I am not an exceptional programmer, I need to remove Clearance and Discount variables, because they are excess code, I need to add more comments and I need to add validation statements for the quantity of items; however, I thought for a person of my limited skill it was an average program at the very least.

However, you are my senior. Do you think it's a sub-par program? Do you have any more thoughts about it?
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 495
  • View blog
  • Posts: 1,546
  • Joined: 20-March 10

Re: How to include a for loop to keep a running total

Posted 14 July 2012 - 01:51 AM

No,

You are an ok programmer...

If you remove the Clearance and Discount
variables that will be fine I thought you were
going to use them as some total of
what the person had saved making those
purchase decisions....

I was obviously wrong.

For your level of understanding
The program is not sub-par.

Best Wishes

Snoopy.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1