1

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
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
  • 3
    Your loop condition should be `i < 3`, not `i == 3`. – Andrew Sep 29 '15 at 13:31
  • 4
    Your code is very confused, and I'm afraid giving you the fixes for the various mistakes here will not really help you get to grips with C++. I suggest you start over with [a good book](http://stackoverflow.com/q/388242/1171191). And stop listening to whoever tried to explain `endl` and `flush` to you. – BoBTFish Sep 29 '15 at 13:36
  • Also, you're setting `VolCalcs` to be copies of `length, width` and `height`, not references to them. So the input operations won't affect `length, width, height`. Your attempts to modify `length`, `width`, and `height` later also aren't reflected in `VolCalcs`, which you output in the second loop. Get rid of the separate `length`, `width` and `height` variables, and just use the `VolCalcs` array everywhere. Fix the loop condition, and you should be closer to a working solution. – Andrew Sep 29 '15 at 13:38

1 Answers1

2

I see the following errors:

  1. The main should always return int.
  2. The for loop condition should be for (int i = 0; i != 3; ++i). Note you need to change both of your for loops. The for loop condition i != 3 means the the loop should continue if i != 3.
  3. You mix the use of an array with the use of variables. There are two options:
    • Use variables: Remove VolCalcs and use only plain variables length, width, height. Remove loops.
    • Use array. Remove length, width, height and use only array. In the fix below I use length, width, height only for array initialization, but these variables are not used further in the code.

 

#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;
}

Now let me play with your code a little bit...

#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;
}

What changed?

  1. Prefer prefix operator ++i to postfix i++. For example in ++VolCalcs[0].
  2. Write spaces around operators. This is a good formatting practice.
  3. Postpone variable definitions as long as possible. Create volume when it is needed.
  4. The #include <array> is not needed. Prefer std::array before plain old C style arrays. They aren't in my code. It's probably your next step :)
Lukáš Bednařík
  • 2,578
  • 2
  • 15
  • 30