displayData

Pages: 12
Apr 6, 2016 at 11:13am
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 Apr 11, 2016 at 8:47am
Apr 6, 2016 at 11:52am
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 Apr 6, 2016 at 11:59am
Apr 6, 2016 at 11:54am
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;
    }
}

Apr 6, 2016 at 11:58am
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;
Apr 6, 2016 at 12:07pm
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!
Apr 6, 2016 at 12:42pm
How would I get the whole program to repeat 5 x with a for loop

for (int getData = 0; getData < 6; ++ getData)?
Apr 6, 2016 at 12:47pm
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?
Apr 6, 2016 at 4:26pm
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 Apr 6, 2016 at 4:33pm
Apr 6, 2016 at 4:28pm
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 Apr 6, 2016 at 4:29pm
Apr 6, 2016 at 4:52pm
I don't want to reprint lines of code. I just want the program to restart 5 x
Apr 6, 2016 at 5:02pm
Yes, you said. Did you actually read what I wrote?
Apr 6, 2016 at 6:26pm
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?
Apr 6, 2016 at 6:40pm
yes should be

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

Apr 6, 2016 at 6:45pm
now it just goes on forever
Apr 6, 2016 at 8:29pm
Yes it does. Look at the conditional part of the for loop carefully.
Apr 6, 2016 at 9:09pm
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.

Apr 7, 2016 at 7:31am
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 Apr 7, 2016 at 7:31am
Apr 7, 2016 at 8:16am
Just put the code that you want to repeat, inside the loop.

What's wrong with doing it how dlhayden showed you?
Apr 7, 2016 at 10:07am
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
Apr 7, 2016 at 11:14am
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 Apr 7, 2016 at 11:15am
Pages: 12