how to stop infinite loop

I have to submit this coding for school and when I try to submit it, it says, "program end never reached(commonly due to an infinite loop or infinite recursion)."

How do I get my coding to stop infinite looping? If you could fix my coding, I would be grateful. Thanks!

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
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double mass,weightEarth,weightMoon,weightMars;
//constants for earth,moon and mars
const double earth = 9.81;
const double moon = 1.62;
const double mars = 3.77;
cout<<fixed<<setprecision(3); //using iomanip header set precision and fixed format
cout<<"Input the mass : ";
cin>>mass;
cout<<"\nThe mass is "<<mass <<" kg";
if(mass <=0)
cout<<"\nThe mass must be greater than 0";
else
{
weightEarth = mass * earth;
weightMoon = mass * moon;
weightMars = mass * mars;
//formatting using setw ,left and right
cout<<"\n"<<left<<setw(8)<<"Location"<<right<<setw(12)<<"Weight (N)";
cout<<"\n"<<left<<setw(8)<<"Earth"<<right<<setw(12)<<weightEarth;
cout<<"\n"<<left<<setw(8)<<"Mars"<<right<<setw(12)<<weightMars;
cout<<"\n"<<left<<setw(8)<<"Moon"<<right<<setw(12)<<weightMoon;
if(weightEarth < 10)
cout<<"\nThe mass is light";
else if(weightEarth >=1000)
cout<<"\nThe mass is heavy";
}
return 0;
}


[/code][/code]
Last edited on
It's not a problem with the code, what are you entering for mass ?



C:\Temp>test126
Input the mass : 100

The mass is 100.000 kg
Location Weight (N)
Earth 981.000
Mars 377.000
Moon 162.000
C:\Temp>test126
Input the mass : 123456789

The mass is 123456789.000 kg
Location Weight (N)
Earth 1211111100.090
Mars 465432094.530
Moon 199999998.180
The mass is heavy
I'm entering 150 as the mass because that what my assignment says..
closed account (48T7M4Gy)
Setting code out clearly and with regular use of braces and whitespace helps. if ... else if ... else probably wasn't needed and is a tip it's a structure to avoid for maintaining clarity.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double mass,weightEarth,weightMoon,weightMars;
    //constants for earth,moon and mars
    const double earth = 9.81;
    const double moon = 1.62;
    const double mars = 3.77;
    
    cout<<fixed<<setprecision(3); //using iomanip header set precision and fixed format
    cout<<"Input the mass : ";
    cin>>mass;
    cout<<"\nThe mass is "<<mass <<" kg";
    
    if(mass <=0)
    {
        cout<<"\nThe mass must be greater than 0";
    }
    else
    {
        weightEarth = mass * earth;
        weightMoon = mass * moon;
        weightMars = mass * mars;
        //formatting using setw ,left and right
        cout<<"\n"<<left<<setw(8)<<"Location"<<right<<setw(12)<<"Weight (N)";
        cout<<"\n"<<left<<setw(8)<<"Earth"<<right<<setw(12)<<weightEarth;
        cout<<"\n"<<left<<setw(8)<<"Mars"<<right<<setw(12)<<weightMars;
        cout<<"\n"<<left<<setw(8)<<"Moon"<<right<<setw(12)<<weightMoon;
        
        if(weightEarth < 10)
        {
            cout<<"\nThe mass is light";
        }
        else if(weightEarth >=1000)
        {
            cout<<"\nThe mass is heavy";
        }
        else
        {
            cout << "Who knows what happens here?"; // <--- that's why!
        }
    }
    return 0;
}
Topic archived. No new replies allowed.