2017-05-30 2 views
-1

Avant de continuer, je voudrais dire que c'est ma première fois ici et je ne sais pas comment les choses fonctionnent encore, alors s'il vous plaît pardonnez toute erreur de ma part.L'exécution du programme C renvoie -1. # QNAN0 au lieu du nombre stocké dans la variable à virgule flottante

Lors de la compilation, (code source ci-dessous) tout fonctionne bien, sauf pour le contenu de la disp float qui est égal à -1. # QNAN0. Toute aide à ce sujet? Merci d'avance. Certaines parties du code ne sont pas complètes comme la structure du commutateur. S'il vous plaît temporairement que (À moins que cela affecte le résultat).

Le code source pour le programme de 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

'fflush (stdin);' doit pas être utilisé. –

+2

Argent non initialisé –

+1

@VidorVistrom Cela n'a pas de prix! : D – KeyWeeUsr

Répondre

3

La variable money est non initialisé dans la fonction moneyup lorsqu'il est utilisé dans l'expression

money=(float)money+m; 
+0

Merci pour l'aide! – naynay