Please Help

I am taking a programing class for school and I cannot figure out the error in this program:

#include <stdio.h>

int main (void)
{
int s, h;
printf ("Please enter the MPH that you have been traveling at: ");
scanf("%i", &s);

printf ("Please enter the number of hours that you have been traveling: ");
scanf("%i", &h);

printf("HOURS\tDistance\n");
for (h=1; h<=24; h=h+1);
for (s=1; s<=100; s=s+1);

{
float Distance = s * h;
printf("%h\%s\n %.1f\n", h, Distance);
}

}

I am trying to get the MPG and hours drivin to calculate distance. P[lease help
Your outer for statements is null; that is, it doesn't do anything. I think you want something more like:

1
2
3
4
5
6
7
8
9
	for (h=1; h<=24; h=h+1)
	{
		for (s=1; s<=100; s=s+1)

		{
			float Distance = s * h;
			printf("%h\%s\n %.1f\n", h, Distance);
		}
	}


You also have a formatting error in your printf statement. Let me know if you want help with that as well.
I checked the program and it was correct when I wrote it. I know on the printf line it is missing the t after the h int. I am curious though to find out where the errors are. I keep getting a segmentation error.
I'm not sure how you're getting a segmentation error. I also don't see the need for a loop, if you're only computing one distance. I removed both of the for statements from your code, and the calculation works fine.

And again, your formatted printf statement needs some attention.

Finally, may I ask why you're using a float to hold the product of two integers? And, I'd recommend that you declare Distance up at the top of the routine, instead of in the middle of your code.

Topic archived. No new replies allowed.