displayData

Pages: 12
Hi I have to write a small program using three functions...input, calculate and display. The program is executing and returning the calculation but after that wont complete the display function. Could someone please point me to where i'm going wrong

Last edited on
How would I get the main function to loop 5 x before it exits the program?

void getData();//getData function
for (int getData = 0; getData < 6; ++ getData)
{
cout << "Please enter the height of your room: ";
cin >> theHeight;
cout << endl << "Please enter the width of your room: ";
cin >> theLength;
cout << endl << "Please enter the length of your room: ";
cin >> theWidth;
}
Last edited on
One of the things that stinks about C++ is that it's easy to write code that is syntactically correct but does something completely different from what you think. Here is your code indented and commented to reflect what it's really doing.
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
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
using namespace std;
int
main()                          //Main function
{
    // The following 3 lines declare (but don't execute) functions.
    // When you include the reutrn type of a function, it's a declaration:
    void getData();
    int calculateVolume();
    void displayData();


    int theHeight, theLength, theWidth;
    float volume;

    // Declare getData() again.
    void getData();             //getData function


    // Begin a block of code that will be executed. This block is
    // inside main().
    {
        cout << "Please enter the height of your room: ";
        cin >> theHeight;
        cout << endl << "Please enter the width of your room: ";
        cin >> theLength;
        cout << endl << "Please enter the length of your room: ";
        cin >> theWidth;
    }


    // Declare another function
    int calculateVolume();      //calculate function

    // start another block of code.
    {
        volume = theHeight * theLength * theWidth;

        // Since this block of code is inside main(), the next line
        // returns from main()
        return volume;
    }

    // Declare another function
    void displayData();         //display function

    // Start another block of code.
    {
        cout << "The volume of your room with width " << theWidth << ", height \
" <<
            theHeight << ", and length " << theLength << " is " << volume << ".\
" << endl;

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

    }

}


To define a function, you don't put a semicolon after the name:
1
2
3
4
int func() // no semicolon
{
    // Code for func
}


So to fix this code, you need to move the functions outside of main and define them properly. Also, it's a good idea to put the declarations outside of main:
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
55
56
#include <iostream>
#include <iomanip>
using namespace std;

// Declarations
void getData();
int calculateVolume();
void displayData();

int theHeight, theLength, theWidth;
float volume;

int
main()                          //Main function
{
    getData();
    calculateVolume();
    displayData();
}


void getData()
{
        cout << "Please enter the height of your room: ";
        cin >> theHeight;
        cout << endl << "Please enter the width of your room: ";
        cin >> theLength;
        cout << endl << "Please enter the length of your room: ";
        cin >> theWidth;
}


int calculateVolume()   //calculate function
{
    volume = theHeight * theLength * theWidth;

    // Since this block of code is inside main(), the next line
    // returns from main()
    return volume;
}


void displayData()              //display function
{
    cout << "The volume of your room with width " << theWidth << ", height " <<
        theHeight << ", and length " << theLength << " is " << volume << "." <<\
 endl;

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

Is this the output you're expecting?
http://s9.postimg.org/9zwmytmen/Screenshot_3919.png

I dont see a reason for the calculateVolume function, you can completely remove it and add
volume = theHeight * theLength * theWidth;
Dhayden thank you so much... Your explanation made me understand functions far better than before. This is helping me in jumps not strides..grateful for your time!
How would I get the whole program to repeat 5 x with a for loop

for (int getData = 0; getData < 6; ++ getData)?
That is easy to test:
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
  for ( int getData = 0; getData < 6; ++getData ) {
    std::cout << "Hello: " << getData << '\n';
  }
  return 0;
}

How many lines does that print?
yes but I don't want to print. The program must loop 5 x after the output is given. After " the volume of room is...."

it should ask " Please enter height ...etc"

This must repeat 5 x before it says press any key to continue

basically a redo 5 x
Last edited on
keskiverto was giving you an example of how a for loop works, as well as hinting about a mistake you made in the code you posted.

It should be pretty clear from what s/he posted, and also from your textbook, that you put the lines of code that you want to repeat, inside the loop.
Last edited on
I don't want to reprint lines of code. I just want the program to restart 5 x
Yes, you said. Did you actually read what I wrote?
How would I get the whole program to repeat 5 x with a for loop

Yes.

for (int getData = 0; getData < 6; ++ getData)?

Almost. Put the current contents of main() into the for loop:
1
2
3
4
5
for (int getData = 0; getData < 6; ++ getData) {
    getData();
    calculateVolume();
    displayData();
}


Now run it and see what happens. It will run several times, but not five times. Can you tell what's wrong?
yes should be

[code]
int getData = 0; getData = 5; ++ getData)
[code]

now it just goes on forever
Yes it does. Look at the conditional part of the for loop carefully.
You cant declare a new function inside the for-loop having the same name as a function that you already have, change getDate inside the for() to another name.

Thank you guys.

I understand the basics of a for loop and did so much research

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main ()
{
   // for loop execution
   for( int a = 10; a < 20; a = a + 1 )
   {
       cout << "value of a: " << a << endl;
   }

   return 0;
}


The syntax of a for loop in C++ is:
for ( init; condition; increment )
{
statement(s);
}
I understand the execution ..then check ..then the increment change. This is very logic when printing a number out.

I also understand that I need to give the order of events in main as the statements. Thus telling it what to do. But I just cant understand how the loop will do the request I have ..Could someone please explain so I can understand it.
Last edited on
Just put the code that you want to repeat, inside the loop.

What's wrong with doing it how dlhayden showed you?
I declared a new function ...

1
2
3
4
5
6
7
void redomain ()
        {
        for (int redomain = 0; redomain = 1; redomain = 5)
        {
        getData();
        calculateVolume();
        displayData();


repeats but does not stop after 5 x
Remember how we do for loops in C++?

slex04 wrote:
The syntax of a for loop in C++ is:
for ( init; condition; increment )
{
statement(s);
}


Where in your for statement is the condition? Where is the increment?
Last edited on
Pages: 12