errors with volume and area calculator

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.

Here's the code.
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
#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


Thanks for the help!
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
Last edited on
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.

You should really use this link: http://www.cplusplus.com/doc/tutorial/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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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...

it would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.

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
#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;
}
Two little things. On line 7 and 24, one says recetangle and the other says prism lol.

And in both your functions you are missing the return; statement to return to the caller - in this case main().
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!
Topic archived. No new replies allowed.