2013-03-10 3 views
1

주문한 각 콤보 및 총 가격 수를 표시하려고합니다. 왜 A, B, C에 값을 저장하지 않는지 잘 모르겠다. 초심자 프로그래머는 그렇게 쉬워. 잠시 동안 if 문에 문제가 있었으므로 분명히 if 문을 잘못 처리하고 있습니다. 문이 어떤지 ==를 사용해야하는 경우FOR 루프 및 IF 문과 관련된 문제

#include <iostream> 
using namespace std; 
int main() 
{ 
int group = 0; 
char combo = ' '; 
int A = 0; 
int B = 0; 
int C = 0; 
double total = 0.0; 
cout << "How many customers are in the group? "; 
cin >> group; 
for (int counter = 0; counter < group; counter = counter + 1) 
{ 
    cout << "Enter combo ordered: "; 
    cin >> combo; 
    if (combo = A) 
    { 
     A = A + 1; 
     cout << "Enter combo ordered: "; 
     cin >> combo; 
    } 
    else if (combo = B) 
    { 
     B = B + 1; 
     cout << "Enter combo ordered: "; 
     cin >> combo; 
    } 
    else if (combo = C) 
    { 
     C = C + 1; 
     cout << "Enter combo ordered: "; 
     cin >> combo; 
    } 
    total = A*6 + B*6.25 + C*5.75; 
} 
cout << "# of Combo A ordered: " << A << endl; 
cout << "# of Combo B ordered: " << B << endl; 
cout << "# of Combo C ordered: " << C << endl; 
cout << "Total price: $" << total << endl; 
system("pause"); 
return 0; 
} 
+1

해결책을 찾았습니까? – tmwoods

답변

1

몇 가지 사항을 조정해야 할 수도 있습니다. 당신이 int을 사용하여 double을 계산할 때 소수의 컴파일러가 그것을 좋아하지 않으며 프로그램이 작기 때문에 double을 여기에서 사용하지 않아도됩니다. 또한 약간의 구문 오류 (예 : == 대신 =). 출력을 고립 된 방식으로 표시해 보았습니까?

main(){ 
double A = 1; 
double B = 2; 
double C = 3; 
double total = A*6 + B*6.25 + C*5.75; 

cout << "# of Combo A ordered: " << A << endl; 
cout << "# of Combo B ordered: " << B << endl; 
cout << "# of Combo C ordered: " << C << endl; 
cout << "Total price: $" << total << endl; 
system("pause"); 
return 0; 
} 

당신의 수정 코드 : 같은 뭔가

#include <iostream> 
using namespace std; 
int main() 
{ 
int group = 0; 
char combo = ' '; 
double A = 0; 
double B = 0; 
double C = 0; 
double total = 0.0; 
cout << "How many customers are in the group? "; 
cin >> group; 
for (int counter = 0; counter < group; counter++) 
{ 
    cout << "Enter combo ordered: "; 
    cin >> combo; 
    if (combo == 'A') 
    { 
     A++; 
    } 
    else if (combo == 'B') 
    { 
     B++; 
    } 
    else if (combo == 'C') 
    { 
     C++; 
    } 
    cout << "Enter combo ordered: "; 
    cin >> combo;  
} 

total = A*6 + B*6.25 + C*5.75; 

cout << "# of Combo A ordered: " << A << endl; 
cout << "# of Combo B ordered: " << B << endl; 
cout << "# of Combo C ordered: " << C << endl; 
cout << "Total price: $" << total << endl; 
system("pause"); 
return 0; 
} 

groupif 루프도 한 번 이상 실행되고 있는지 확인하기 위해 나는 또한 당신의 값을 표시합니다. 여기에 몇 가지 단점이 있으며 개별적으로 테스트 할 것입니다.


편집 :

아마 이것을 테스트 :

#include <iostream> 
using namespace std; 
int main() 
{ 
int group = 0; 
cout << "How many customers are in the group? "; 
cin >> group; 
for (int counter = 0; counter < group; counter++) 
{ 
    cout << "Test success"; 
} 

보고 당신도 당신의 for 루프에 입력하는 경우.

+0

이 코드를 실행했지만 여전히 모든 수량에 0이 붙어 있습니다. – user2040308

+1

@ user2040308 내 편집을보고 어쩌면 테스트를 실행하십시오. – tmwoods

+0

좋아, 이제 if (combo == 'x')를 ab 및 c로 변경하고 그룹 #에 4를 입력 할 때를 제외하고 루프를 4 번 반복하는 대신 작업의 종류를 두 배로 늘립니다. 루프 – user2040308

3

를 들어 루프 for (int counter = 0; counter < group; counter++)

을해야합니다. =은 배정을위한 것입니다.

귀하의 if 진술 문은 당신이 비교하고있는 성함을 인용해야합니다 : if (combo == 'A') {. 또한 다음과 같이 콤보를 문자 배열로 액세스해야 할 수도 있습니다. if (combo[0] == 'A') {

+0

좋아, 그 것들을 수정했지만 여전히 모든 디스플레이 수량에 대해 0을 표시합니다. – user2040308

+0

몇 가지 가능한 수정 사항을 추가했습니다. – Daedalus