Basically what I'm trying to do it take the area of a rectangle and break each step down into functions for a user to enter in information. I don't have any syntax errors just cant seem to find why it wouldn't work correctly. I'm using codeblocks..
#include <iostream>
usingnamespace std;
int calculation(int, int);
void setwidth()
{
int width;
cout << "Please enter the width." << endl;
cin >> width;
}
void setheight()
{
int height;
cout << "Please enter the height." << endl;
cin >> height;
}
int calculation(int width, int height)
{
cout << "your area of the reactangle is " << endl;
setwidth();
setheight();
int area = width * height;
return area;
}
int main(){
int height, width, area;
cout << "Please enter just the whole number by itself. \n ";
cout << " DO NOT put ex: 15ft. Thanks" << endl;
cout << endl;
setwidth();
setheight();
cout << "Your area is " << area;
return 0;
}
void setwidth()
{
int width;
cout << "Please enter the width." << endl;
cin >> width;
}
In the above function, width exists only within the scope of the function, that is, between the opening and closing braces. The width declared in main is a different variable completely.
What you need is for your function to return a value, much like your calculation function.
For example:
1 2 3 4 5 6 7 8 9 10
int SetHeight()
{
int height;
std::cout << "Please enter a height: ";
std::cin >> height;
return height;
}
// Then in main
int height = SetHeight();
However, being completely honest, I think it's probably a little overkill in this scenario to use SetWidth and SetHeight functions. I'd be tempted to gather all of the input in main and keeping any functions cin/cout free.
It was just a class project because I mostly do it all in the main but my teacher wanted me to break it all down in functions just to see if we get the concept. I really appreciate your input. I honestly read your comment and said, "ooooooohhhh". LOL.