我的目标是让程序尽可能高效。我一直坚持这个问题很长一段时间,当我搜索它时,我被告知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
答案 0 :(得分:2)
我看到以下错误:
main
应始终返回int
。for
循环条件应为for (int i = 0; i != 3; ++i)
。请注意,您需要更改两个for循环。 for
循环条件i != 3
表示如果i != 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;
}
改变了什么?
++i
到postfix i++
。例如,在++VolCalcs[0]
。volume
。#include <array>
。在普通的旧C风格数组之前首选std::array
。它们不在我的代码中。这可能是你的下一步:)