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!
#include <iostream>
#include <iomanip>
usingnamespace std;
int main() {
double mass,weightEarth,weightMoon,weightMars;
//constants for earth,moon and mars
constdouble earth = 9.81;
constdouble moon = 1.62;
constdouble 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";
elseif(weightEarth >=1000)
cout<<"\nThe mass is heavy";
}
return 0;
}
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.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double mass,weightEarth,weightMoon,weightMars;
//constants for earth,moon and mars
constdouble earth = 9.81;
constdouble moon = 1.62;
constdouble 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";
}
elseif(weightEarth >=1000)
{
cout<<"\nThe mass is heavy";
}
else
{
cout << "Who knows what happens here?"; // <--- that's why!
}
}
return 0;
}