By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

error in the scanf in the do-while loop

P: 18
#include<stdio.h>
void main()
{
int i, n, month, day, calcday, calcmonth;
char flag='y';
char weekday[][10]={"Friday","Saturday","Sunday","Monday","Tuesday", "Wednesday","Thursday"};
int usermonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
do
{
printf("Enter the date (Day, Month): ");
scanf("%d %d",&day ,&month);
calcday=day-1;
calcmonth=month-1;
for(i=0;i<calcmonth;i++)
{
calcday+=usermonth[i];
}
n=calcday%7;

printf("%d/%d/2010 is %s \n",day, month, weekday[n]);

printf("Do you want to continue (y/n)? ");

scanf("%c",&flag);
}

while(flag=='y');

}
Oct 13 '10 #1
Share this Question
Share on Google+
10 Replies


100+
P: 687
Reproduced.
Well, if the question was "what to do" it depends on what do you need to achieve. If it doesn't matter, i'd continue with scanf("%c") till I get either 'y' or 'n'.
Oct 13 '10 #2

P: n/a
use getch() instead of scanf() it will work fine
Oct 14 '10 #3

P: 18
i can't understand you mr.newb16
i want in each time scanf for the flag to know if it complete in the loop or get out
Oct 14 '10 #4

P: 18
can u give me an example mr.gaurav kumar
Oct 14 '10 #5

P: 18
i try it mr.gaurav kumar
and it still wrong
nothing change
Oct 14 '10 #6

100+
P: 687
If scanf resulted in 'flag' that is not 'y' nor 'n', continue calling scanf until you get the desired letter.
Oct 14 '10 #7

Expert 100+
P: 2,396
Change "while(flag=='y')" to "while(flag!='n')".
(This is just a restatement of newb16's suggestion.)
Oct 15 '10 #8

100+
P: 687
This will not work - it will receive \r left after previous scanf and will never quit. It need to discard it in a separate loop.
Oct 15 '10 #9

P: 18
how mr.newb16 ??
Oct 15 '10 #10

P: 7
change
Expand|Select|Wrap|Line Numbers
  1. char flag='y'; -> char flag[2];
  2. scanf("%c",&flag); -> fgets( flag,2,stdin );
  3. while(flag=='y'); -> while(*flag=='y');
Oct 16 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.