For loop in main function

Apr 9, 2016 at 3:42pm
Please help with including a for loop in main function that allows the user to repeat the calculation of the volume for new input values five times. The program and functions are working correct without the loop.


This is what I have done but its not working correctly:


//program that will compute the volume of a room. User inputs are the height, width and length of a room.
#include <iostream>
#include <string>
using namespace std;

void getData (int & height, int & width, int & length);
int calculateVolume (int & height, int & width, int & length, int & volume);
void displayOutput (int & height, int & width, int & length, int & volume);

int main ()
{
int height, width, length, volume, size;

for (int i = 1; i >= 5; i++)
getData(height, width, length);
calculateVolume(height, width, length, volume);
displayOutput(height, width, length, volume);

return 0;
}

void getData (int & height, int & width, int & length)
{
cout << "Enter height, width and length of room:" <<endl;
cin >> height >> width >> length;
}

int calculateVolume (int & height, int & width, int & length, int & volume)
{
volume = height * width * length;
}

void displayOutput (int & height, int & width, int & length, int & volume)
{
string size;

if (volume < 100)
{
size = "small";
cout << "The volume of a room with height "<<height <<", width "<<width <<" and length "<<length << " is "<<volume <<"."<<endl;
cout << "size: " <<size;
}
else if (volume >= 100 && volume <= 500)
{
size = "medium";
cout << "The volume of a room with height "<<height <<", width "<<width <<" and length "<<length << " is "<<volume <<"."<<endl;
cout << "size: " <<size;
}
else
{
size = "large";
cout << "The volume of a room with height "<<height <<", width "<<width <<" and length "<<length << " is "<<volume <<"."<<endl;
cout << "size: " <<size;
}
}


Apr 9, 2016 at 3:57pm
Deja vu. See http://www.cplusplus.com/forum/beginner/188464/

You do already have a for loop in your main() that repeats the statements within its body according to the condition that you have set.
You should read carefully: http://www.cplusplus.com/doc/tutorial/control/
Apr 9, 2016 at 4:10pm
The loop is not working.

I get the following output when I run the code:

Enter height, width and length of room:
23
33
1
The volume of room with height 23, width 33 and length 1 is 759.
size: largeThe volume of room with height 23, width 33 and length 1 is 759.
size: large
process returned 0 <0x0> execution time
Apr 9, 2016 at 4:26pm
That is not possible. Your loop
1
2
for (int i = 1; i >= 5; i++)
  getData(height, width, length);

Cannot call the getData() even once, because 1 cannot possibly be larger-or-equal to 5. Perhaps you don't post your actual code?


Please, use the code tags. See http://www.cplusplus.com/articles/jEywvCM9/
Apr 9, 2016 at 4:40pm
Thanks @keskiverto got it!

I was using i >=5 instead of i <=5.
Topic archived. No new replies allowed.