I'm trying to write a very simple program that converts quarts of water (user input) to molecules of water in C.
我正在尝试编写一个非常简单的程序,将几夸脱的水(用户输入)转换为C中的水分子。
#include <stdio.h>
#include <float.h>
int main(void)
{
float water_quarts;
float water_grams = (water_quarts*950.00);
float water_molecules = (water_grams/3e-23F);
printf("Enter an amount of water in quarts: \n");
scanf("%f", &water_quarts );
printf("%f quarts is %f grams of water.\n", water_quarts, water_grams );
printf("%f grams is %f molecules of water.\n", water_grams, water_molecules );
return 0;
}
However, when I compile and run it, it gives me:
但是,当我编译并运行它时,它给了我:
Enter an amount of water in quarts:
15
15.000000 quarts is 0.000000 grams of water.
0.000000 grams is 0.000000 molecules of water.
I'm not entirely sure what I'm doing wrong. I'm using gcc 4.9.1 on lubuntu 14.10, running through VirtualBox.
我不完全确定我做错了什么。我在lubuntu 14.10上使用gcc 4.9.1,通过VirtualBox运行。
1 个解决方案
#1
5
Your need to assign values to water_grams
and water_molecules
after you take input for water_quarts
from the user ( that is after your scanf()
).
在从用户(即scanf()之后)输入water_quarts后,需要为water_grams和water_molecules分配值。
#include <stdio.h>
#include <float.h>
int main(void)
{
float water_quarts;
printf("Enter an amount of water in quarts: \n");
scanf("%f", &water_quarts );
float water_grams = (water_quarts*950.00);
float water_molecules = (water_grams/3e-23F);
printf("%f quarts is %f grams of water.\n", water_quarts, water_grams );
printf("%f grams is %f molecules of water.\n", water_grams, water_molecules );
return 0;
}
or alternatively
float water_quarts;
float water_grams ;
float water_molecules ;
printf("Enter an amount of water in quarts: \n");
scanf("%f", &water_quarts );
water_grams = (water_quarts*950.00);
water_molecules = (water_grams/3e-23F);
Otherwise, you are assigning uninitialized values to them, which is why you are getting unexpected output.
否则,您要为它们分配未初始化的值,这就是您获得意外输出的原因。
When you declare a variable, it will have some garbage value ( unless you assign it some value ), and due to that, when you do
当你声明一个变量时,它会有一些垃圾值(除非你为它指定一些值),并且由于这个原因,当你做
float water_grams = (water_quarts*950.00);
float water_molecules = (water_grams/3e-23F);
water_quarts
has some garbage value, and thus, you are just assigning some garbage to the other variables.
water_quarts有一些垃圾值,因此,你只是给其他变量分配一些垃圾。
#1
5
Your need to assign values to water_grams
and water_molecules
after you take input for water_quarts
from the user ( that is after your scanf()
).
在从用户(即scanf()之后)输入water_quarts后,需要为water_grams和water_molecules分配值。
#include <stdio.h>
#include <float.h>
int main(void)
{
float water_quarts;
printf("Enter an amount of water in quarts: \n");
scanf("%f", &water_quarts );
float water_grams = (water_quarts*950.00);
float water_molecules = (water_grams/3e-23F);
printf("%f quarts is %f grams of water.\n", water_quarts, water_grams );
printf("%f grams is %f molecules of water.\n", water_grams, water_molecules );
return 0;
}
or alternatively
float water_quarts;
float water_grams ;
float water_molecules ;
printf("Enter an amount of water in quarts: \n");
scanf("%f", &water_quarts );
water_grams = (water_quarts*950.00);
water_molecules = (water_grams/3e-23F);
Otherwise, you are assigning uninitialized values to them, which is why you are getting unexpected output.
否则,您要为它们分配未初始化的值,这就是您获得意外输出的原因。
When you declare a variable, it will have some garbage value ( unless you assign it some value ), and due to that, when you do
当你声明一个变量时,它会有一些垃圾值(除非你为它指定一些值),并且由于这个原因,当你做
float water_grams = (water_quarts*950.00);
float water_molecules = (water_grams/3e-23F);
water_quarts
has some garbage value, and thus, you are just assigning some garbage to the other variables.
water_quarts有一些垃圾值,因此,你只是给其他变量分配一些垃圾。