0

I'm not sure whether one of my formulas are wrong, I placed something in the wrong area, or I'm missing something completely. But when I run the prog. It would add up the prices of meals from a previous table of customers, instead of resetting for each new table. Any help?

int main ()
{    
  //These are the variables used for the formulas and inputs.
  int people, counter;
  float price, subtotal, tip, tax, total;
  cout<<"How many people are at the table?" <<endl;
  cin>>people;
  //Use a while statement to start a loop
  while (people!=0)
   {
  //Use a for statement inside the while to make a nested loop. It will ask the price of each meal.
  for(counter=1; counter<=people; counter++)
    {
      cout<<"How much is the meal?: " <<endl;
      cin>>price;
        subtotal+=price;
        tax=subtotal*.06;
      if (people<5)
        {
          tip=subtotal*.18;
        }
      else
      tip=subtotal*.20;
      total=tax+subtotal+tip;

    }
  //This is the final output for the program. Which will be the bill.
  cout<<setprecision(2) <<fixed;
  cout<<left;
  cout<<setw(20)<<"Subtotal: " <<"$" <<subtotal <<endl;
  cout<<setw(20)<<"Sales Tax: " <<"$" <<tax <<endl;
  cout<<setw(20)<<"Tip: " <<"$" <<tip <<endl;
  cout<<setw(20)<<"Total: " <<"$" <<total <<endl;
  cout<<" " <<endl;
  cout<<setw(20)<<"How many people are at the table?" <<endl;
  cin>>people;

   }
4

3 に答える 3

1

初心者のコードのように見えます(-:

変数を初期化しないことは、特にC++では非常に悪いプログラミングです。あなたはそれをしなければなりません(変数の作成で)!! 参考:初期化されていない=ガベージ(未定義の値)が含まれています。

次のように変更する必要がありsubtotal+=price;ますsubtotal=price;

total=tax+subtotal+tip;total+=tax+subtotal+tip;

于 2012-11-27T02:05:39.200 に答える
1

特に、whileループ内および の直前で、すべての変数を 0 にリセットしたいforsubtotal

于 2012-11-27T01:49:56.947 に答える
-1

forループの代わりにこれを使用してみてください

intカウンター=1; //変数を宣言することを忘れないでください

while(counter <= people){

//ここで価格計算式を使用します

cout<< your desired outputs <<endl;
counter++;             // Update counter so the condition can be met eventually

}

于 2012-11-27T02:08:42.740 に答える