2012-11-27 3 views
0

내 수식 중 하나가 잘못되었거나 잘못된 영역에 배치했는지 또는 완전히 놓친 것이 확실하지 않습니다. 하지만 내가 프로를 실행할 때. 새 테이블마다 다시 설정하는 대신 고객의 이전 테이블에서 식사 가격을 합산합니다. 어떤 도움이 필요합니까?내 루프 문에서 재설정하는 대신 내 값이 계속 유지됩니다.

당신의 변수를 초기화하지 매우입니다 : - 당신은 while 루프 내에서 단지 for 전에 0으로 모든 변수를 재설정 할

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; 

    } 

답변

1

, 특히 subtotal

+0

그냥 궁금 {여기 당신의 가격 공식

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

사용 //

}가 다른 인 변수를

동안 (카운터 < = 사람)를 선언하는 것을 잊지 마세요 이 일을하는 방법? – user1804751

+0

while 루프 내에서만 변수를 정의 할 수도 있습니다. 초기화에 기본값을 사용하는지 확인하십시오. –

1

은 (초보자의 코드 같은데 특히 C++에서 잘못된 프로그래밍 반드시 수행해야합니다 (변수 생성시) FYI : 초기화되지 않음 = 가비지 포함 (정의되지 않은 값)

당신은 total+=tax+subtotal+tip;

-1

subtotal=price;

total=tax+subtotal+tip;subtotal+=price;을 변경해야하는 것은 사용해보십시오 대신 루프

INT 카운터 = 1이; //