I can't see anything wrong with this code but for some reason, scanf for char doesn't work. Any idea?
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello, world!\n");
double income, intrest;
char marital_status;
printf ("How much did you earn this year? ");
scanf ("%lf", &income);
if (income > 100.00)
{
printf ("Please enter M=married or S=Single for marital status: ");
scanf ("%c", &marital_status);
printf ("How much intrest did you earn this year? ");
scanf ("%lf", &intrest);
printf ("your marital status is %c and your income is %.2lf intrest is %.2lf \n",marital_status, income,intrest);
}
return 0;
}
scanf ("%d", &income);
%d is for int data type. Also, the issue is with
scanf ("%c",&marital_status);
not with other data type.
Quote from: puredesi5 on April 15, 2013, 02:08:56 AMthe issue is with
scanf ("%c",&marital_status);
scanf (" %c",&marital_status);
or
scanf ("%*c%c",&marital_status);
to handle LF in input stream.
Thanks timovji! It worked.
I checked so many books and online but either they didn't address it or I didn't recognize. Also, the odd thing is same exact code would work if you bring it outside of the if block but would not work when it's in the if block.
Would someone please point me where can I get more information on why it works outside of the if block but not in the if block.
I really appreciate this forum and people who take time to respond.
Once again, thanks!