VERY simple C question

lombard

Registered
I'm working my way through my college programming in C book as a first step to learning Cocoa. Anyway, I'm stuck playing on a Windows computer at work, and I'm still early in the process so I'm mostly writing command line programs in DOS. Anyway, I'm writing a very simple program but for some reason after I compile the code, the executable is kind of screwed up. I'm wondering if the compiler is messing up or what. Anyway, here's the code:

#include < stdio.h >
#include < math.h >

double hypotenuse(double, double);

main(){
double sideA, sideB, sideC;
char cont='Y';

while (cont == 'Y' || cont == 'y'){
printf("\nEnter the two known lengths, separated by a space: "
scanf("%lf%lf", &sideA, &sideB);

sideC = hypotenuse(sideA, sideB);

printf("Side 1 =\t%.4f\n", sideA);
printf("Side 2 =\t%.4f\n", sideB);
printf("Hypotenuse =\t%.4f\n", sideC);

printf("Would you like to enter another value [Y/N]? ");
scanf("%c", &cont);
}


return 0;
}



double hypotenuse(double a, double b){
double c;

c = sqrt(a*a + b*b);
return c;
}


The problem is that the program keeps breaking out of the while loop without executing the last scanf statement. If I place an identical scanf statement right before the last printf statement, it works fine. I'm thinking it must be a compiler and/or dos problem. So am I just blind and not seeing an obvious problem here?
 
I wonder if it has to do with buffering the keyboard input. I thought there were things you had to be careful with when using scanf because different platforms (or even shells) handle that differently.

...but I can't help you fix the problem. Sorry.

-Rob
 
scanf is very screwy. When you are hitting return from the first scanf, the second one is reading the '\n' since its still in the buffer. You should be able to use fflush(stdin), but who knows.

It would be a little extra work, but a good learning excersice:
use gets() and take the first char of the string array and use atoi() to convert it to an int for atof to convert if to a float.

-jdog
 
thanks for the help guys!

Glad to know there's not a whole lot I can do about it at my current knowledge level. I'll just keep plugging away at the problems in my book to learn the basics. In a few months I should be at the point where simple things like this aren't a problem for me any more.
 
Yo dudes. I can't tell you how frustrating it is to solve this problem. Probably because correct code isn't working the way it's supposed to, and now since I've upgraded to 10.1 I'm not allowed to use the gets() function (well that's what I'm assuming is the problem. I'm pretty sure I've used it before on X, but I could be wrong).

Well anyway, this is what I did to get it to work.

#include <stdio.h>
#include <math.h>

double hypotenuse(double a, double b);

int main()
{
double sideA, sideB, sideC;
char cont[10];

do
{
printf("\nEnter the two known lengths, separated by a space: ");
scanf("%lf%lf", &sideA, &sideB);

sideC = hypotenuse(sideA, sideB);

printf("Side 1 =\t%.4f\n", sideA);
printf("Side 2 =\t%.4f\n", sideB);
printf("Hypotenuse =\t%.4f\n", sideC);

printf("Would you like to enter another value [Y/N]?\n");
scanf("%s", cont);
} while ( cont[0] == 'Y' || cont[0] == 'y' );

return 0;
}

double hypotenuse(double a, double b)
{
double c;

c = sqrt(a*a + b*b);
return c;
}

I hope this works!

-Jim
 
The problem is definately the buffering of the keyboard input.

I don't do much C (more C++) so I use cin for all input..

anyway, as a general rule, whenever you get input from the keyboard you should ALWAYS put
"cin.ignore();"
on the next line, to clear the \n from the buffer.

(I guess you can figure out how to do that in C)

-Dan
 
Back
Top