【问题标题】:Why does program skip over for loop with string arrays and cin/cout为什么程序会跳过带有字符串数组和 cin/cout 的 for 循环
【发布时间】:2015-09-29 13:30:36
【问题描述】:

我的目标是使程序尽可能高效。我被这个问题困扰了很长一段时间,当我搜索它时,我被告知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

【问题讨论】:

  • 你的循环条件应该是i &lt; 3,而不是i == 3
  • 您的代码非常混乱,我担心在这里为您提供各种错误的修复并不能真正帮助您掌握 C++。我建议您从a good book 重新开始。不要再听那些试图向你解释endlflush的人了。
  • 另外,您将VolCalcs 设置为length, widthheight 的副本,而不是对它们的引用。所以输入操作不会影响length, width, height。您稍后修改lengthwidthheight 的尝试也不会反映在您在第二个循环中输出的VolCalcs 中。摆脱单独的lengthwidthheight 变量,并在任何地方使用VolCalcs 数组。修复循环条件,您应该更接近可行的解决方案。

标签: c++ arrays performance for-loop cout


【解决方案1】:

我看到以下错误:

  1. main 应始终返回 int
  2. for 循环条件应为for (int i = 0; i != 3; ++i)。请注意,您需要更改两个 for 循环。 for 循环条件i != 3 表示如果i != 3 则循环应该继续。
  3. 您将数组的使用与变量的使用混合使用。有两种选择:
    • 使用变量:删除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 &lt;array&gt;。在普通的旧 C 样式数组之前,更喜欢 std::array。它们不在我的代码中。这可能是你的下一步:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2013-02-05
    • 2011-09-08
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多