1

试图显示订购的每个组合的数量和总价。不知道为什么它不会在 A、B 和 C 中存储值。这里是新手程序员,所以要简单。if 语句的问题已经有一段时间了,所以很明显我做错了整个 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;
}
4

2 回答 2

3

For循环应该是for (int counter = 0; counter < group; counter++)

如果语句应该==用于相等。 =仅用于分配。

您的if陈述需要引用您要比较的字符:if (combo == 'A') {. 您也可能需要将组合作为字符数组访问,如下所示:if (combo[0] == 'A') {

于 2013-03-10T18:37:21.800 回答
1

我认为您可能只需要调整几件事;double当你计算一个using时,一些编译器不喜欢它,因为程序很小int,所以没有理由不在这里使用它。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;
}

我还将显示您的值,group以确保if循环甚至至少运行一次。这里有一些故障点,我将单独测试。


编辑:

也许测试一下:

#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循环。

于 2013-03-10T20:35:29.547 回答