-
how do i fix it?
hi i m new 2 the whole C++ n i think its lods of fun i'vebeen puttin a program 2gether...but 4 some reason it always seems 2 crash when askin 4 the person status( married etc..)
the program has been written in french....but i doubt any1 is interested in wat is written with scanf
i would like 2 thank u all in advance 4 ur time n help
Code:
#include <stdio.h>
int main(void)
{
int nblivre, c;
float kg, cels, frnht;
printf("Taper le poids en livres:\n");
scanf("%d", &nblivre);
printf("Taper la temperture en degres farenheit:\n");
scanf("%f", &frnht);
cels = ((frnht - 32) / 9) * 5;
kg = 0.454 * nblivre;
printf("\nLe patient pese %.1f kg, a une temperature de %.1f degres", kg, cels);
fflush(stdin);
getchar();
printf("Selectionnez votre status civile:");
fflush(stdin);
getchar();
printf("\n\nAffichage avec switch (suivant que) :\n");
switch (c)
{
case 'c':
printf("celibataire.");
break;
case 'C':
printf("celibataire.");
break;
case 'a':
printf("un autre cas.");
break;
case 'A':
printf("un autre cas.");
break;
case 'm':
printf("marie.");
break;
case 'M':
printf("marie.");
break;
case 'd':
printf("divorce.");
break;
case 'D':
printf("divorce.");
break;
printf("\n");
fflush(stdin);
getchar();
return 0;
}
}
believe my problem is located around the switch level, n i dont know how 2 use it 2 well, that is IF i used it well....tks 4 every1;s help
with the c>>> As far as I can tell, the variable c is never assigned a value. That may or may not be your problem.
how do i set it up as a value 4 letters...do i use char?
ty all for ur time n help
-
Don't flush stdin, it's naughty see, http://faq.cprogramming.com/cgi-bin/...&id=1043284351 for an alternative
Consider converting uppercase to lowercase in the switch, rather than repeating code.
Code:
#include <ctype.h>
/* ... */
switch(tolower(c))
{
case 'c':
/* ... etc */
'c' isn't assigned a value, call
-
program still crashes :S dont know y...when i want 2 do this part:
Code:
printf("Selectionnez votre status civile:");
fflush(stdin);
getchar();
printf("\n\nAffichage avec switch (suivant que) :\n");
switch (c)
{
case 'c':
printf("celibataire.");
break;
case 'C':
printf("celibataire.");
break;
case 'a':
printf("un autre cas.");
break;
case 'A':
printf("un autre cas.");
break;
case 'm':
printf("marie.");
break;
case 'M':
printf("marie.");
break;
case 'd':
printf("divorce.");
break;
case 'D':
printf("divorce.");
break;
printf("\n");
fflush(stdin);
getchar();
return 0;
}
}
in dos mode it crashes..:S
wat must i use instead of flush stding?
i m very new 2 C adn i dont know 2 much..so bare with me :)
n thakes the upper n lower :)
-
You have defined 'c' as an integer, but you are trying to use it as a char. I doubt any of your switch statements are going to work that way. Change your definition of 'c' to char for starters. And nowhere do you assign the getchar() to c !!
-
its all good i got it workin tks alot :)
-
> You have defined 'c' as an integer, but you are trying to use it as a char. I doubt any of your switch statements are going to work that way. Change your definition of 'c' to char for starters. And nowhere do you assign the getchar() to c !!
getchar() returns an int... it should be an int.
-
Just a little notice... there's another way of structuring your switch statement you might want to know..
Code:
switch (c)
{
case 'c':
case 'C':
printf("celibataire.");
break;
case 'a':
case 'A':
printf("un autre cas.");
break;
case 'm':
case 'M':
printf("marie.");
break;
case 'd':
case 'D':
printf("divorce.");
break;
}
or
Code:
switch (c)
{
case 'c':
case 'C':
printf("celibataire.");
break;
case 'm':
case 'M':
printf("marie.");
break;
case 'd':
case 'D':
printf("divorce.");
break;
default:
printf("un autre cas.");
break;
}
And yeah, the remarks of kcpilot about the int thing isn't right...
-
foxman ur from quebec? so u speak french, caliss finalemen qqun qui peut vraiment m'aider
check mec g un devoir a rendre c l'autre poste est ce que je px t'envoyer exactement ce que g a faire comme ca tu px m'expliquer ce que g a faire...merci