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
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;
}
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.
#include <iostream>
#include <iomanip>
usingnamespace 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;
} elseif (volume > 100 && volume < 500) {
cout << "Size: medium" << endl;
} elseif (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:
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!
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.
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>
usingnamespace 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.