Thursday, July 9, 2009

Whats wrong with my C program?

i made this program for my computer class. when i execute it and go through the motions as they say it shuts off at the last step.


#include%26lt;stdio.h%26gt;





int main()


{


int a, b, c;


printf("*** WAGE CALCULATOR ***\n\n");


printf("Input hourly rate and hit enter.\n");


scanf("%d",%26amp;a);


printf("Input hours worked this week and hit enter.\n");


scanf("%d",%26amp;b);


c=a*b;


printf("your gross salary is: $");





printf("%d\n",c);


return 0;


}

Whats wrong with my C program?
I suspect the following problem:


If you run this program by double click on an icon or running it in a graphic environment (e. g. visual studio) you get the following behavior:





1/ the operating system opens a command window


2/ the program runs (correctly) in the window


3/ the program terminates and the operating system closes the window.





--%26gt; Hence you have no time to read the program results





Workaround 1:


============


run the program by typing the program name at the command prompt in a command window. the program will run (correctly) and then you will be prompted for a new command in the same window





Workaround 2 (my favorite)


=====================


Change the end of the program to make it





...


printf("%d\n",c);


getc(stdin); /* this line is added!!! */


return 0;


}





This will force the computer to wait that you press a key before terminating the program and closing the window
Reply:It compiles and runs fine on my Linux machine. Are you running this on a Windows computer? If so, you will have to run this in a DOS shell.





What do you want it to do that it isn't doing?
Reply:If you are using Windows, you can also put





system("pause");





before





return 0;





That also forces the screen to wait for a key press.
Reply:i guess





printf("%d\n",c);





should be





printf("%d\n",%26amp;c);





not too sure though.....:)


No comments:

Post a Comment