【问题标题】:Issue with FOR loops and IF statementFOR 循环和 IF 语句的问题
【发布时间】:2013-03-10 18:35:07
【问题描述】:

尝试显示订购的每个组合的数量和总价。不知道为什么它不会在 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;
}

【问题讨论】:

  • 找到解决方案了吗?

标签: loops if-statement for-loop repeat


【解决方案1】:

for循环应该是for (int counter = 0; counter &lt; group; counter++)

If 语句应该使用== 来表示相等。 = 仅用于分配。

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

【讨论】:

  • 好的,我修复了这些问题,但每个显示数量仍然显示 0
  • 我为您添加了一些可能的修复方法。
【解决方案2】:

我认为您可能只需要调整一些东西;当您使用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;
}

我还会显示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循环。

【讨论】:

  • 这是一个奇怪的错误,我真的不能建议修复它。但至少代码的主体是有效的。我应该注意到你正在输入字符串。我忽略了这一点。
  • 尝试后者,但将 cin、cout 部分移动到循环的底部(我在答案中的代码中这样做了)。
  • 您在每个 ifelseif 块中都有 cout &lt;&lt; "Enter combo ordered: "; cin &gt;&gt; combo;。而是将其移到底部。在我的回答中,我写了“您更正的代码”,然后输入了代码。我更改了该部分以反映我现在所说的内容。
  • 好的,我知道了。我取出了除了第一个 cout > combo 之外的所有内容,效果很好。
  • 没问题 :) 确保您将问题标记为已解决,供以后的查看者使用 :)
猜你喜欢
  • 2020-03-24
  • 2017-07-19
  • 2021-02-02
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
相关资源
最近更新 更多