开发者

C programming elementary problem

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


int main (void)
{
    float inches;
    printf("Enter the number of inches\n");
    scanf("%f\n",&inches);


    float feet;
    float cm;
    float yards;
    float meter;

    feet = 12 * inches;
    cm = 2.54 * inches;
    yards = 36 * inches;
    meter = 39.37 * inches;

    printf("Amount in feet: %f\n"开发者_如何学运维, &feet);
    printf("Amount in cm:   %f\n", &cm);
    printf("Amount in yards: %f\n", &yards);
    printf("Amount in meters: %f\n", &meter);




   getchar();

   return 0;

}

I'm using Dev c++

Is the problem i'm problem I'm working on in C. Basically enter in a number in inches then print amount in cm,yards,meters and feet. This is giving me 0.0000 or something for all of them or actually the time it is up. I can't keep the screen up and I thought that was the purpose of getchar() but I must have been mistaken. Any help is great. Thanks!

EDIT 1

What about as far as keeping dev c++ on the screen instead of closing out after I put stuff in? I am also having to put 2 values in before it returns in anything when the screen pops up? Why??


Two problems:

  1. The usual problem with using scanf(), in that it leaves the newline after the number unread and the following read operation (the getchar() here) reads it.
  2. You shouldn't pass pointers to printf(), but the actual values.


You're trying to print the addresses of your floats as floats, you just want to say this:

printf("Amount in feet: %f\n", feet);

Note the lack of an address (&) operator on feet. You want to apply similar changes to your other printf calls.


With printf, you don't give it the address of the float values, you just give it the values. Remove the &s from the printf calls.

You need the address in scanf because the function modifies the variables that you pass in, but printf just needs the values. As it is, the printf is essentially reinterpreting the pointers as floats, which is why you get the garbage values displayed.


About I can't keep the screen up, it's a common problem to everyone trying to execute a console program in a graphical environment directly from the IDE, in particular Dev-C++. The problem is that there's no console for I/O, then one is provided but just for the time the program is running, and since programs are fast, if you do not add a pause after your last input and output, you won't have the time to read the output.

Many MS Windows Dev-C++ users add an horrorific system("pause"). I always suggest that, if Dev-C++ is not able to provide a console for I/O with the option "keep it opened even after the program ends", then it is better you open a shell (cmd or powershell on windows) and run your program directly from there.

About the input problem, unluckly scanf-ing has several buffering problem since the input that is not recognized for the given format is not discarded and is ready for the next reading. E.g.

scanf("%f", &aFloat);
scanf("%f", &theNextFloat); // in your case you have the extra getchar();

won't stop for the second scanf if you write 1.25 4.5 as first input, since the 4.5 is already available for the next scanf. In your case it was a newline that was left in the buffer and since getchar has found it, it does not need to wait for input. You could use a while( getchar() != EOF ) ; instead, and then to exit you need to hit Ctrl-D.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜