I'm having problems with another calculator I'm writing. I'm trying to have it so that if a user types a and hits enter, it will then run the function to calculate area. And if the user types v, then it will do so with volume. However, I'm having some errors.
#include <iostream>
int area()
{
int length, width, area;
std::cout << "Please enter the following information to caluclate the area of a rectangle.\n\n";
std::cout << "Length as integer:\t";
std::cin >> length;
std::cin.ignore();
std::cout << "Width as integer:\t";
std::cin >> width;
std::cin.ignore();
area = length * width;
std::cout << area;
return 0;
}
int volume()
{
int length, width, height, volume;
std::cout << "Please enter the follwing information to calculate the voume of a prsim.\n\n";
std::cout << "Length as integer:\t";
std::cin >> length;
std::cin.ignore();
std::cout << "Width as integer:\t";
std::cin >> width;
std::cin.ignore();
std::cout << "Height as integer:\t";
std::cin >> height;
std::cin.ignore();
volume = length * width * height;
std::cout << volume
return 0;
}
int main()
{
char type;
std::cout << "Please type 'a' for area or 'v' for volume.\n\n";
std::cin >> type;
std::cin.ignore();
if(type="a") area();
if(type="v") volume();
return 0;
}
Here's the errors:
"calculator.cpp": E2379 Statement missing ; in function volume() at line 42
"calculator.cpp": W8070 Function should return a value in function volume() at line 43
"calculator.cpp": W8060 Possibly incorrect assignment in function main() at line 53
"calculator.cpp": W8060 Possibly incorrect assignment in function main() at line 54
calculator.cpp": E2379 Statement missing ; in function volume() at line 42
W8070 Function should return a value in function volume() at line 43
You are missing a semi-colon on the line before 42
calculator.cpp": W8060 Possibly incorrect assignment in function main() at line 53
"calculator.cpp": W8060 Possibly incorrect assignment in function main() at line 54
Equality is tested with ==. Also, character literals should be in single quotes
You're understanding of functions is a litte off. If you give a function a return type, in this case int, unless its part of some calculation you won't want it to return 0 like you are doing in your functions.
But i'll briefly explain. There are two ways to use functions: one way returns a value, one does not. If it does not return a value then it is of type void. I'll show an example with your volume() function:
void volume() // changed to void from int <<<<
{
int length, width, height, volume;
std::cout << "Please enter the follwing information to calculate the voume of a prsim.\n\n";
std::cout << "Length as integer:\t";
std::cin >> length;
std::cin.ignore();
std::cout << "Width as integer:\t";
std::cin >> width;
std::cin.ignore();
std::cout << "Height as integer:\t";
std::cin >> height;
std::cin.ignore();
volume = length * width * height;
std::cout << volume
return; // changed to 'return' from 'return 0' as you only want to return to main, and dont want to return a value<<<<<
}
The other method is returning with a value, any type other than void e.g. int, double, char etc...
int volume()
{
int length, width, height, volume;
std::cout << "Please enter the follwing information to calculate the voume of a prsim.\n\n";
std::cout << "Length as integer:\t";
std::cin >> length;
std::cin.ignore();
std::cout << "Width as integer:\t";
std::cin >> width;
std::cin.ignore();
std::cout << "Height as integer:\t";
std::cin >> height;
std::cin.ignore();
volume = length * width * height;
// remove this line >>>> std::cout << volume
return volume; // change this line to 'return volume' instead of 'return 0'
}
Also with this, you have to create variable in main and assign it to the functon e.g.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
char type;
std::cout << "Please type 'a' for area or 'v' for volume.\n\n";
std::cin >> type;
std::cin.ignore();
int a = area(); // a is assigned the returned value from the area function
if(type="a") std::cout << a; // outputting a which is the returned value from the area function
if(type="v") volume();
return 0;
}
Thanks for the help! I fixed it and it's working now! Here's the updated code. Can you please check it, because I don't want to get into any bad habbits.
#include <iostream>
void area()
{
int length, width, area;
std::cout << "\nPlease enter the following information to caluclate the area of a rectangle.\n\n";
std::cout << "Length as integer:\t";
std::cin >> length;
std::cin.ignore();
std::cout << "Width as integer:\t";
std::cin >> width;
std::cin.ignore();
area = length * width;
std::cout << "Area equals:\t" << area;
}
void volume()
{
int length, width, height, volume;
std::cout << "\nPlease enter the follwing information to calculate the voume of a prsim.\n\n";
std::cout << "Length as integer:\t";
std::cin >> length;
std::cin.ignore();
std::cout << "Width as integer:\t";
std::cin >> width;
std::cin.ignore();
std::cout << "Height as integer:\t";
std::cin >> height;
std::cin.ignore();
volume = length * width * height;
std::cout << "Volume equals:\t" << volume;
}
int main()
{
char type;
std::cout << "Please type 'a' for area or 'v' for volume.\n\n";
std::cin >> type;
std::cin.ignore();
if(type=='a') area();
if(type=='v') volume();
return 0;
}
okay, thanks. I know one says prism and the other says rectangle, cus a rectangle doesn't have volume. I'll be sure to add the return; statement. Thanks so much for the help!