You are here:

C++/C++ While Loop

Advertisement


Question
Hi there,
I am trying to build a shopping program for my daughter but I'm stuck! Could you help me out?
Here's what I have so far:

#include <iostream>
using namespace std;
int main ()
{   double price, subtotal, tax, payment, change;
  int number;
  cout<<"Enter the Price: $ "<<flush;
  cin>>price;
  while ((price>0) || (price==0))
  {cout<<"Enter the Price: $ "<<flush;
  cin>>price;
  if ((price>0) || (price==0))
  {
  cout<<"Enter the Number of Items: "<<flush;
  cin>>number;
  }
  }
  subtotal= price*number;
  cout<<"The Subtotal is: $ "<<subtotal<<endl;
  tax=subtotal*0.05;
  char T;
  cout<<"Tax or No Tax: "<<tax<<endl;
  cin>>T;
  tax=0;
  if ((T=='y')||(T=='Y'))
     tax=subtotal*0.05;
  double t_total, t_tax, t_subtotal;
  t_subtotal=subtotal;
  t_tax=tax;
  t_subtotal=t_subtotal+tax;
  t_tax=t_tax+tax;
  t_total=t_subtotal+t_tax;
  cout<<"The Total is: $ "<<t_total<<endl;
  cout<<"Payment is: $ "<<flush;
  cin>>payment;
  change=payment-t_total;
  cout<<"Change is: $ "<<change<<endl;
}


From there, it asks me to "imput a price", but then it asks me again to imput another price without even asking the "number of items" from my first pricing. I can't seem to output "tax or no tax" inside the loop also. When I enter a negative price, the loop still goes on for one more round, therefore giving me a possible negative "grand total".

What I would like to do is form a loop with "enter the price" , "enter number of items" and "tax or no tax" up until a negative price value is entered (stopping the loop) From there, I would like the program to add every subtotal obtained, apply the tax to the merchandises who add some, and then give out the grand total... After that I will probably be ok to finish up this program.
Thank you so much! My daughter's gonna like to play cashier if I can get this darn program to work!!

Answer
> What I would like to do is form a loop ... up until a negative price value is entered

the simple way to do this is break out of the loop if value is negetive.

while( true )
            {
                        cout<<"Enter the Price: $ " ;
                        cin>>price;
                        if ( price < 0 ) break ;
                        // etc

> I would like the program to add every subtotal obtained, apply the tax
> to the merchandises who add some, and then give out the grand total

you need to initialize the totals to zero outside the loop.
in the loop, add to the totals.

you do not need to flush cout explicitly before every input. by default cout is tied to cin (i.e., cin.tie(cout)): whenever an operation on cin is requested, cout is flushed first.

also prefer using a '\n' to endl unless your intent is to force a flush of the stream (endl also calls the flush() method of the stream). endl is less efficient than '\n'. most of the time you want to flush a stream, it gets flushed for you automatically anyway.


#include <iostream>
using namespace std;

int main ()
{       
            double price, subtotal, tax, payment, change;
            int number;
            double t_total = 0.0, t_tax = 0.0, t_subtotal = 0.0 ;

            while( true )
            {
                        cout<<"Enter the Price: $ " ;
                        cin>>price;
                        if ( price < 0 ) break ;

                        cout<<"Enter the Number of Items: " ;
                        cin>>number;

                        subtotal= price*number;
                        cout<<"The Subtotal is: $ "<<subtotal<<'\n' ;

                        char T;
                        cout<<"Tax or No Tax: " ;
                        cin>>T;
                        tax=0;
                        if ((T=='y')||(T=='Y'))
                        {
                                 tax=subtotal*0.05;
                                 cout << "tax is $ " << tax << '\n' ;
                        }

                        t_subtotal += subtotal;
                        t_tax += tax;
                        t_total += t_subtotal+t_tax;

            }

              cout<<"The Total is: $ "<<t_total<<'\n' ;
              cout<<"Payment is: $ " ;
              cin>>payment;
              change=payment-t_total;
              cout<<"Change is: $ "<<change<<'\n' ;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.