运行C程序返回-1。#QNAN0而不是存储在浮点变量中的数字

时间:2017-05-30 21:40:42

标签: c floating-point nan

在继续之前,我想说这是我第一次来这里,我不知道事情是如何运作的,所以请原谅我的任何错误。

编译时,(下面的源代码)一切正常,除了float disp的内容等于-1。#QNAN0。对此有何帮助?提前致谢。代码的某些部分不像开关盒结构那样完整。请暂时(除非它影响结果)。

C程序的源代码:

#include <stdio.h>
#include <stdlib.h>
float moneyup(float m);


 int main()
{
char name[20];
char x;
int y;
float disp;
int hunger;

printf("\t\t**********************************************\n");
printf("\t\t*                                            *\n");
printf("\t\t*             How To Get Rich Quick!         *\n");
printf("\t\t*                                            *\n");
printf("\t\t**********************************************\n");
printf("\nThis is an experimental command line interface game made by NayNay AKA Nathan\n");
printf("\nPlease pardon the poor user interface.");

for(;;)
{
    printf("\nPlease enter your name(one only)");
    scanf("%s", &name);
    printf("\nThe name you entered is %s. Is this correct? (type y/n for yes or no)\n");
    fflush(stdin);
    x=getchar();

    if(x=='y')                                                  /*This part with the for loop is used to get the name of the*/
    {                                                           /*user and confirm the correctness of that name. If the name is*/
        printf("Okay! Moving on...");                           /*wrong, the user has the option to change it. Bulletproofing used*/
        break;                                                  /*here*/
    }
    else if(x=='n')
    {
        printf("Alright let's try again.");
        continue;
    }
    else
    {
        printf("Let's try this again.");
        continue;
    }
}
printf("\nOkay %s, Let's get this story started",name);
printf("\n\nOne sad dreary morning, %s got up from sleep and went to the kitchen to get breakfast.");
printf("\nUnfortunately for him his pantry only contained a bunch of cockroaches going at it and laying their eggs everywhere");
printf("\nHe then checked his pockets and pulled out his last 5-dollar bill. That was all he had left,");
printf("\nHe bought a sandwich for $2 and decides to start a business with $3 as capital");
printf("\n\nChoose how to start");
printf("\n1. Begging.");
printf("\n2. Mow lawns.");
printf("\n3. Apply for post of newspaper boy.");
fflush(stdin);
y=getchar();

switch(y)
{
    case '1':
        printf("You begged for 6 hours and got $5.25\n");
        disp=moneyup(5.25);
        printf("You now have $%f\n",disp);
}
return 0;
}

float moneyup(float m)
{
float money;

money=(float)money+m;
return(money);
}

1 个答案:

答案 0 :(得分:3)

在表达式

中使用时,变量money在函数moneyup中未初始化
money=(float)money+m;