为什么程序会跳过带有字符串数组和 cin/cout 的 for 循环

Why does program skip over for loop with string arrays and cin/cout

我的目标是使程序尽可能高效。我已经在这个问题上停留了很长一段时间,当我搜索它时,我被告知 flush/endl cout 语句。

当我开始调试时,我推断问题出在 for 循环上。它会跳过 for 循环,导致长度、宽度、高度为 0。

#include <iostream>
#include <string>
#include <array>
using std::cout;    using std::cin;
using std::string;   using std::flush;
using std::endl;

void main()
{
    int length=0, width=0, height=0, volume=0;
    int VolCalcs[3]={length, width, height};

    string Prompt[3] = {"Please enter length: ", "Please enter width: ", "Please enter height: "};
    string NewResult[3] = {"The new length is ", "The new width is ", "The new height is "};

    for(int i=0;i==3;++i)
    {
        cout<<endl;
        cout<<Prompt[i]<<flush;
        cin>>VolCalcs[i];
    }

    volume=length*width*height;
    cout<<" The Volume is: "<<volume<<endl;

    length++;
    width--;
    height+=10;


    for(int i=0;i==3;++i)
    {
        cout<<NewResult[i] << VolCalcs[i] <<endl;
    }
    volume=length*width*height;
    cout<<" The New Volume is: "<<volume<<endl<<endl;
    cout<<"Press Enter to End Program"<<flush;
    cin.ignore();
}

输出结果如下:

The Volume is: 0

The New Volume is: -10

Press Enter to End Program

我看到以下错误:

  1. main 应该总是 return int.
  2. for循环条件应该是for (int i = 0; i != 3; ++i)。请注意,您需要更改两个 for 循环。 for 循环条件 i != 3 表示如果 i != 3.
  3. 则循环应该继续
  4. 您混合使用数组和变量。有两种选择:
    • 使用变量:删除 VolCalcs 并仅使用普通变量 length, width, height。删除循环。
    • 使用数组。删除 length, width, height 并仅使用数组。在下面的修复中,我仅将 length, width, height 用于数组初始化,但这些变量不会在代码中进一步使用。

#include <iostream>
#include <string>
#include <array>
using std::cout;    using std::cin;
using std::string;   using std::flush;
using std::endl;
int main()
{
    int length=0, width=0, height=0, volume=0;
    int VolCalcs[3]={length, width, height};
    string Prompt[3] = {"Please enter length: ", "Please enter width: ", "Please enter height: "};
    string NewResult[3] = {"The new length is ", "The new width is ", "The new height is "};

    for (int i=0; i != 3; ++i)
    {
        cout<<endl;
        cout<<Prompt[i]<<flush;
        cin>>VolCalcs[i];
    }

    volume=VolCalcs[0]*VolCalcs[1]*VolCalcs[2];
    cout << " The Volume is: "<<volume<<endl;

    VolCalcs[0]++;
    VolCalcs[1]--;
    VolCalcs[2]+=10;


    for(int i=0;i!=3;++i)
    {
        cout<<NewResult[i] << VolCalcs[i] <<endl;
    }
    volume=VolCalcs[0]*VolCalcs[1]*VolCalcs[2];
    cout<<" The New Volume is: "<<volume<<endl<<endl;
    cout<<"Press Enter to End Program"<<flush;
    cin.ignore();

    return 0;
}

现在让我稍微玩一下你的代码...

#include <iostream>
#include <string>

using namespace std;


int main()
{
    int VolCalcs[3] = {0, 0, 0};
    string Prompt[3] = {"Please enter length: ", "Please enter width: ", "Please enter height: "};
    string NewResult[3] = {"The new length is ", "The new width is ", "The new height is "};

    for (int i=0; i != 3; ++i) {
        cout << endl;
        cout << Prompt[i] << flush;
        cin >> VolCalcs[i];
    }

    int volume = VolCalcs[0] * VolCalcs[1] * VolCalcs[2];
    cout << " The Volume is: " << volume << endl;

    ++VolCalcs[0];
    --VolCalcs[1];
    VolCalcs[2] += 10;

    for (int i = 0; i != 3; ++i) {
        cout << NewResult[i] << VolCalcs[i] << endl;
    }
    volume = VolCalcs[0] * VolCalcs[1] * VolCalcs[2];
    cout << " The New Volume is: " << volume << endl << endl;
    cout << "Press Enter to End Program" << flush;
    cin.ignore();

    return 0;
}

有什么变化?

  1. 优先使用前缀运算符 ++i 而不是后缀 i++。例如在 ++VolCalcs[0].
  2. 在运算符周围写上空格。这是一个很好的格式化习惯。
  3. 尽可能推迟变量定义。在需要时创建 volume
  4. 不需要#include <array>。在普通的旧 C 样式数组之前更喜欢 std::array。它们不在我的代码中。这可能是你的下一步:)