Error in code, please help?

I am writing a code that will find the area of a circle, triangle, and volume of a cube. It should be displayed as a switch choice and the user can select an option from the menu and receive the result.

I'm trying to use switch case statements, cout and cin, and format output.

However, the compiler will not accept my program and keeps giving me a warning message...

[Warning] converting to 'int' from 'double'.

It still runs, but that warning message still shows up in line 62.

Also I can't figure out why choice 2 isn't working. It keeps giving 0 as the result despite the numbers you input for base and height.

I'm not sure what I did wrong.

Help would be appreciated. Thank you!

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
66
67
68
69
70
71
72
73
// This program uses a switch statement to determine
// the item selected from a menu.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()

{
int choice;            // To hold menu choice
int area;              // To hold area
int volume;            // To hold volume
int radius;            // To hold radius
int base;              // To hold base
int height;            // To hold height
int sidelength;        // To hold side length
          
// Constants for menu choices
const int CIRCLE_CHOICE = 1,
          TRIANGLE_CHOICE = 2,
          CUBE_CHOICE = 3,
          QUIT_CHOICE = 4;

// Display the menu and get a choice.
cout << "\t\tWhich would you like to calculate?\n\n"
     << "1. Area of a circle\n"
     << "2. Area of a triangle\n"
     << "3. Volume of a cube\n\n"
     << "4. Quit\n\n"
     << "Enter your choice: ";
cin >> choice;

// Set the numeric output formatting.
cout << fixed << showpoint << setprecision(2);

// Respond to the user's menu selection.
switch (choice)
{
case CIRCLE_CHOICE:
     cout << "What is the radius? ";
     cin >> radius;
     area = 3.1416 * radius * radius;
     cout << "The result is " << area << endl;
     break;

case TRIANGLE_CHOICE:
     cout << "What is the base? ";
     cin >> base;
     cout << "What is the height? ";
     cin >> height;
     area = 1/2 * base * height;
     cout << "The result is " << area << endl;
     break;
     
case CUBE_CHOICE:
     cout << "What are the side lengths? ";
     cin >> sidelength;
     volume = sidelength * sidelength * sidelength;
     cout << "The result is " << volume << endl;
     break;

case QUIT_CHOICE:
     cout << "Program ending.\n";
     break;
     
     default:
     cout << "The valid choices are 1 through 4. Run the \n"
          << "program again and select one of those.\n";
}
system ("PAUSE");
	return 0;
}
The issue for why choice 2 never works is that you are multiplying by (1/2). In terms of integers (both base and hight are integers) this rounds down to zero. Hence, it ends up as zero. Really, the simplest solution would be to replace the instances of "int" for the inputs of base, hight, sidelength, and radius with double. That should resolve most of your issues. Also, make sure that area is also type double. Choice must, however, remain as an integer.
I fixed it, thank you!
Topic archived. No new replies allowed.