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 ();
}

Dani AI

Generated

is right about braces: without {} only the very next statement belongs to the if. In your code the cout lines sit outside the if, so the following else has nothing to match and the compiler complains about an "else without matching if". A simple way to avoid both the error and repeated output is to choose acceleration inside a single if/else if/else chain, then print once at the end.

Also double-check units and boundaries. You convert seconds to hours (good, since your velocity formula is in km/h), but acceleration should be reported in m/s^2, not m/s. If those acceleration formulas expect velocity in km/h, keep using v as-is; if they expect m/s, convert with v_ms = v * 1000.0 / 3600.0. Prefer multiplication over pow(x,2) for simple squares (clearer and faster), and return 0 from main instead of calling main() again (as noted, recursion there will eventually crash).

// pick acceleration, then print once
double a;
if (v >= 0 && v < 100) {
    a = 3 - 6.2e-5 * v * v;
} else if (v >= 100 && v < 150) {
    a = 3 - 2.5e-5 * v * v;
} else {
    a = 1 + 0.5e-5 * v * v;
}
std::cout << std::fixed << std::setprecision(2)
          << "Velocity: " << v << " km/h, Acceleration: " << a << " m/s^2\n";

Bonus tips: validate input (seconds >= 0 and cin not failed), handle edge values exactly at 100 and 150 as shown, and consider a simple prompt-driven loop (e.g., do { ... } while(repeat);) if you want to run multiple calculations without restarting the program.

Recommended Answers

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, networking, learning, and sharing knowledge.