How do I loop this?

I have to use a function getData aswell as displayOutput and make a for loop so so that it can loop 5 times.

Heres What I got so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main ()
{
    int height;
    int length;
    int width;
    int calculateVolume;

    cout << "Please enter the height of your room: ";
    cin >> height;
    cout << endl << "Please enter the width of your room: ";
    cin >> width;
    cout << endl << "Please enter the length of your room: ";
    cin >> length;
    cout << endl;

    calculateVolume = height * length * width;

    cout << "The volume of your room with height " << height << ", width " << width <<", and length " << length << " is " << calculateVolume << "." << endl;

    if (calculateVolume < 100) {
        cout << "Size: small" << endl;
    }
    else if (calculateVolume > 100 && calculateVolume < 500) {
        cout << "Size: medium" << endl;
    }
    else {
        cout << "Size: large" << endl;
    }


return 0;
}
Like this?
1
2
3
4
5
6
7
8
9
10
11
static const int MAX_LOOPS = 5; // <-- best to define number of loops as a constant

int main()
{
    for (int loopCounter = 0; loopCounter < MAX_LOOPS; ++loopCounter)
    {
        int data = getData(); // <-- change "int" to the actual return type of getData() here!
        int result = yourComputationHere(data); // <-- change "int" to the actual parameter type of displayOutput() here!
        displayOutput(result);
    }
}
Last edited on
Like this

1
2
3
4
5
 for (int i = 0; i < 5; i++)
  {
    getData(height,length,width);
    displayOutput(height,length,width);
  }


but I don't know how to let it work
What do you mean by "let it work"?

Just put that code in your main() function and it will be executed when control flow reaches there.
I put it in now again , everytime it says getData was not declared in this score ,
displayOutput was not declared in this scope


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

int main ()
{
    int height;
    int length;
    int width;
    int calculateVolume;

    for (int i = 0; i < 5; i++)
  {
    getData(height,length,width);
    displayOutput(height,length,width);
  }


    cout << "Please enter the height of your room: ";
    cin >> height;
    cout << endl << "Please enter the width of your room: ";
    cin >> width;
    cout << endl << "Please enter the length of your room: ";
    cin >> length;
    cout << endl;

    calculateVolume = height * length * width;

    cout << "The volume of your room with height " << height << ", width " << width <<", and length " << length << " is " << calculateVolume << "." << endl;

    if (calculateVolume < 100) {
        cout << "Size: small" << endl;
    }
    else if (calculateVolume > 100 && calculateVolume < 500) {
        cout << "Size: medium" << endl;
    }
    else {
        cout << "Size: large" << endl;
    }


return 0;
}
 In function 'int main()':
14:32: error: 'getData' was not declared in this scope
15:38: error: 'displayOutput' was not declared in this scope


You have to declare a function before you use it.
You have to define (implement) the function so it does something.

Functions take parameters by value and by reference.
See http://www.cplusplus.com/doc/tutorial/functions/


Is 4 * 5 * 5 small, medium, or large?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace std;

void getdata()
{
    cout<<"Enter height: ";
    cin>>height;
    cout<<"Enter width: ";
    cin>>width;
    cout<<"Enter length: ";
    cin>>length;
}
void displayOutput()
{
    cout<<"The height of the room is : "<<height<<"\n";
    cout<<"The width of the room is : "<<width<<"\n";
    cout<<"The length of the room is : "<<length<<"\n";
    cout<<"The volume of your room with height " << height << ", width " << width <<", and length " << length << " is " << calculateVolume << "." << endl;
}



int main ()
{
    int height;
    int length;
    int width;
    int calculateVolume;

   for (int i = 0; i < 5; i++)
  {
    getData(height,width,length);
    displayOutput(height,width,length);
  }



    calculateVolume = height * length * width;


    if (calculateVolume < 100) {
        cout << "Size: small" << endl;
    }
    else if (calculateVolume > 100 && calculateVolume < 500) {
        cout << "Size: medium" << endl;
    }
    else {
        cout << "Size: large" << endl;
    }


return 0;
}


Does it have to be something like this?
Line 5 you are defining the function with no parameters, yet at line 33 you call your function with 3 parameters.

Same problem with the function defined at line 14, no parameters and 3 parameters when calling the function at line 34.

The function at line 5 also has a different name (getdata) than the function call at line 33 (getData).
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

void getData(int& height, int& width, int& length) {
	std::cout << "Please enter the height of your room: ";
	std::cin >> height;

	std::cout << "Please enter the width of your room: ";
	std::cin >> width;

	std::cout << "Please enter the length of your room: ";
	std::cin >> length;
}

void displayOutput(int height, int width, int length) {
	const auto volume {height * width * length};

	std::cout << "\nThe volume of your room with height " << height << ", width " << width << ", and length " << length << " is " << volume << "." << '\n';

	if (volume < 100)
		std::cout << "Size: small\n";
	else if (volume >= 100 && volume < 500)
		std::cout << "Size: medium\n";
	else
		std::cout << "Size: large\n";
}

int main() {
	constexpr unsigned noLoop {5};

	for (unsigned i {}; i < noLoop; ++i) {
		int height {}, length {}, width {};

		std::cout << "\nCalculation " << i + 1 << '\n';
		getData(height, length, width);
		displayOutput(height, width, length);
	}
}

Notice the detail:
1
2
3
4
5
6
7
	if (volume < 100) {
		std::cout << "Size: small\n";
	} else if (volume > 100 && volume < 500) {
		std::cout << "Size: medium\n";
	} else {
		std::cout << "Size: large\n";
	}

If volume is 100, then it is "large" because 100<100 is false and 100>100 is false.


1
2
3
4
5
6
7
	if (volume < 100) {
		std::cout << "Size: small\n";
	} else if (volume >= 100 && volume < 500) {
		std::cout << "Size: medium\n";
	} else {
		std::cout << "Size: large\n";
	}

If volume is 100, then it is "medium" because 100<100 is false but 100>=100&&100<500 is true.


1
2
3
4
5
6
7
	if (volume < 100) {
		std::cout << "Size: small\n";
	} else if (volume < 500) {
		std::cout << "Size: medium\n";
	} else {
		std::cout << "Size: large\n";
	}

If volume is 100, then it is "medium" because 100<100 is false but 100<500 is true.
Registered users can post here. Sign in or register to post.