Hi guys im making this homework on a program that the user will input the time in seconds and it will calculate the velocity and acceleration based on these formulas. Ive tried to compile the program but im getting 2 times "an illegal else without matching if" now the error is because of the acceleration formula on the second and third if because if i remove them it will compile. I was wondering were can i put the formula? or improove the program?

Thanks

``````// Homework 2
// Raul Gonzalez
// Y00339371

#include<iostream>
#include<cmath>
using namespace std;
int main ()

{
double seconds, time, velocity, acceleration;

cout<<"Please enter the time in seconds :"<<endl;
cin>>seconds;
time=(seconds/3600);
velocity=(pow(10,-5)*pow(time,3))-(0.0049*pow(time,2))+(0.758*time)+181.36;

if (velocity>=0 && velocity<100)
acceleration=3-(6.2e-5)*pow(velocity,2);
cout<<"You have a Velocity of "<<velocity<<"km/h and a Acceleration of "<<acceleration<<"m/s"<<endl;

else if (velocity>=100 && velocity<150)
acceleration=3-(2.5e-5)*pow(velocity,2);
cout<<"You have a Velocity of "<<velocity<<"km/h and a Acceleration of "<<acceleration<<"m/s"<<endl;

else
acceleration=1+(0.5e-5)*pow(velocity,2);
cout<<"You have a Velocity of "<<velocity<<"km/h and a Acceletation of "<<acceleration<<"m/s"<<endl;

return main ();
}``````

## All 3 Replies

i think if you are going to have more than one statement after an if then curly braces {} should be used(i.e line 19 to line 20, 23 to 24 and 27 to 28).
I'm also a bit wary of the return main line.

return main(); looks like an attempt to loop. Unfortunately, it will simply keep calling main() until the whole thing runs out of memory.

Calling the main function again is a very bad way to perpetually loop. Try instead wrapping the whole loop in

``````while (1)
{

// your forever looping code here

}``````

i think if you are going to have more than one statement after an if then curly braces {} should be used(i.e line 19 to line 20, 23 to 24 and 27 to 28).
I'm also a bit wary of the return main line.

Hey frogboy77 I appreciate your help that did the trick now the program runs beautifully.
Thanks for the suggestion moschop you guys really helped me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.