【问题标题】:C++: When entering numbers into array first number is 0C ++:将数字输入数组时,第一个数字是0
【发布时间】:2020-07-17 06:03:42
【问题描述】:

试图制作一个计算器,根据用户的输入计算数组中的值。但是当我离开 'p undefined 或 p = 1 时,数组中的第一个值总是 0 会给我同样的问题。它应该是用户输入的第一个值等等。

#include <iostream>
using namespace std;



int main() {

double x;
int p = 1, y = 0;
double sum = 1;
int many[p];
char op;

cout << "How many numbers are you working with today?" << endl;
cin >> x;

do
{
    if (y > x)
    break;

cout << "Enter number " << y + 1 << ":" << endl;
cin >> many[p];

cout << "What would you like the numbers to do: (+ - / *)" << endl;
cin >> op;


if (op == '+')
{
sum+=many[p];
cout << sum <<endl;
}

else if (op == '-')
{
sum-=many[p];
cout << sum <<endl;
}

else if (op == '*')
{
sum*=many[p];
cout << sum <<endl;
}

else if (op == '/')
{
sum/=many[p];
cout << sum <<endl;
}

else {cout << "ERROR: Enter correct value." << endl;}

    y++;

}

    while (y < x);
}

总和应该是 3 而不是 4。

How many numbers are you working with today?
2
Enter number 1:
1
What would you like the numbers to do: (+ - / *)
+
Enter number 2:
2
What would you like the numbers to do: (+ - / *)
+
4

【问题讨论】:

  • 为什么在这种情况下使用数组?从您的代码中我可以看出,您只是在尝试执行重复的数学运算,对吗?
  • 您有未定义的行为,因为您尝试读取或写入 many[p] 超出了范围。您已将数组声明为int many[p];,因此最后一个成员是many[p-1]。但是,正如@uglyCoder 所说,你为什么还需要一个数组呢?此外,可变长度数组 (VLA) 不是标准 C++!!
  • 总和是 4 而不是 3 的原因是您的 sum 变量从 1 开始。您应该将 sum 设置为输入的第一个数字(从 1 开始中断加法;从 0 开始中断乘法),并修复@AdrianMole 提到的未定义行为。

标签: c++ calculator iostream


【解决方案1】:

程序无效且行为未定义。

对于初学者来说,可变长度数组不是标准的 C+ 特性

int p = 1, y = 0;
double sum = 1;
int many[p];

在任何情况下,您都定义了一个包含一个元素的数组。所以访问数组元素的唯一有效索引是 0。

即使在使用数组的第一条语句中

cin >> many[p];

它在其边界之外被访问。

您应该使用标准类模板std::vector。或者实际上您正在处理一个值,那么使用容器甚至没有意义,定义一个标量对象而不是数组。

【讨论】:

    【解决方案2】:

    总和的初始值为1,这就是它加1的原因。我们也不能把它保持为 0,因为那样它会弄乱 '*' 和 '/' 的情况。 我已经为所有案例添加了初始总和值。 另外,我建议您使用 switch 案例而不是 if、else 语句。

    #include <iostream>
    using namespace std;
    
    
    
    int main() {
    
    double x;
    int p = 1, y = 0;
    double sum = 1;
    int many[p];
    char op;
    
    cout << "How many numbers are you working with today?" << endl;
    cin >> x;
    
    do
    {
        if (y > x)
        break;
    
    cout << "Enter number " << y + 1 << ":" << endl;
    cin >> many[p];
    
    cout << "What would you like the numbers to do: (+ - / *)" << endl;
    cin >> op;
    
    if (op == '+')
    {
    if (y == 0) {
      sum = 0;
    }
    sum+=many[p];
    cout << sum <<endl;
    }
    
    else if (op == '-')
    {
      if (y == 0) {
        sum = 0;
      }
      sum-=many[p];
      cout << sum <<endl;
    }
    
    else if (op == '*')
    {
      if (y == 0) {
        sum = 1;
      }
      sum*=many[p];
      cout << sum <<endl;
    }
    
    else if (op == '/')
    {
      if (y == 0) {
        sum = 1;
      }
      sum/=many[p];
      cout << sum <<endl;
    }
    
    else {cout << "ERROR: Enter correct value." << endl;}
    
        y++;
    
    }
    
        while (y < x);
    }
    

    【讨论】:

      【解决方案3】:

      这里有很多没有意义的东西。

      • 您从 sum = 1 开始。这就是该值始终为 +1 的原因
      • many 是一个大小为 1 的数组,可以更改为单个 int。
      • 您正在访问many[p],即many[1],超出范围。你只能访问many[0]

      剩下的交给你去寻找,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        • 1970-01-01
        相关资源
        最近更新 更多