Need help making a force calculator

This is my first post so I hope I did this all right. My teacher wants me to create a force calculator that either outputs a calculation with acceleration set to 9.8 or acceleration specified. When I run this with the choice input as 1, it does what I want to. When I run it as choice 2, it runs the first if statement, than the second if statement. Entering a 0 or 4 doesn't work either because it just runs a calculator even though I want it to output an error message. Any help is greatly 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
  // Example program
#include <iostream>
#include <string>

using namespace std;

int main()

{
    int choice;
    cout << " 1. Peform force calculation with acceleration as 9.8m/s^2 " << endl;
    cout << " 2. Perform force calculation, specifying acceleration " << endl;
    cout << " 3. Exit " << endl;
    cin >>  choice;
    system ("pause");


if (choice == 1);
    {    
         float mass;
         float force;
         float acc = 9.8;
         cout << "Input mass: ";
         cin >> mass;
         force = mass * acc ;
         cout << "The force is: " << force << endl;
    
    system ("Pause") ;
    }
if (choice == 2)
    {
            float mass;
            float force;
            float acc;
            cout << "Input mass (assumed in kg): ";
            cin >> mass;
            cout << "Input Acceleration (assumed in m/s^2): ";
            cin >> acc;
            force = (mass * acc);
            cout << "The force is: " << force << endl;
            system ("pause");
    }

if (choice == 3);
    {
        exit(0);
    }
    
if (choice < 0);
if (choice > 3);
    {
        cout << " Error: incorrect input " << endl;
        system ("pause");
    }
return 0;
}
Line 18: remove the semicolon after the if-condition

line 49 and 50: Remove the semicolons after the if-conditions and change to if(choice < 0 || choice > 3)
Last edited on
@arsian7041 thanks :)
Hi,

You could change the if for choice 2 and 3 to else if, then have a plain else to catch the bad input.

1
2
3
4
5
6
7
8
9
10
11
12
13
if (choice == 1) {
   //call a function to do this
}
else if (choice == 2) {
   //call a function to do this
}
else if (choice == 3) {
    //call a function to do this
}
else {
   std::cout << " Error: incorrect input\n";
   std::system ("pause");  // try to avoid using system anything
}


Alternatively use a switch with a default: case. This has a loop too, as an improvement :+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool Quit = false;
while (!Quit) {
// show menu
// get menu selection
   switch (choice) {
   case 1:
        //call a function to do this
        break;
   case 2:
        //call a function to do this
        break;
   case 3:
        // user wants to quit
        Quit = true;
        break;
   default:  // bad input
        std::cout << " Error: incorrect input\n";
        break;
   }  // end switch

} // end while 
Some other things:

You are re-declaring your variables for force mass and acc.

If you have a constant like 9.8, make it a const variable and use that name in the code: const double acc = 9.8;

Delay the declaration until you have a sensible value to assign to it, or initialise the variable with something:

1
2
3
4
5
6
const double acc = 9.8;
double mass = 1.0;  // always initialise even when getting input straight away, 
                              // zero not necessarily a good choice.
std::cin >> mass;
double force = mass * acc; // declaration calc and assignment all at once


I sometimes advise initialising a variable with a wacky value: Then hopefully the lack of assigning it a proper value might be more apparent later when printing say:

1
2
3
4
int percent = 999; // valid range 0 to 100
//...
//...
std::cout << "percent is " << percent << "\n";  // oops, I see what I didn't do 


Prefer double rather than float. float precision is easily exceeded. double is the default for this reason.

Good Luck !!

Topic archived. No new replies allowed.