My goal was to make the program as efficient as possible. I have been stuck on this problem for quite some time and when I searched it up I was told to flush
/endl
the cout
statements.
When I began debugging I deduced it was the for
loops that were the issue. It would just skip over the for
loop, resulting in length, width, height being 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 output is as follows:
The Volume is: 0
The New Volume is: -10
Press Enter to End Program