473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

error in the scanf in the do-while loop

#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
10 2732
newb16
687 512MB
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
use getch() instead of scanf() it will work fine
Oct 14 '10 #3
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
can u give me an example mr.gaurav kumar
Oct 14 '10 #5
i try it mr.gaurav kumar
and it still wrong
nothing change
Oct 14 '10 #6
newb16
687 512MB
If scanf resulted in 'flag' that is not 'y' nor 'n', continue calling scanf until you get the desired letter.
Oct 14 '10 #7
donbock
2,426 Expert 2GB
Change "while(flag=='y')" to "while(flag!='n')".
(This is just a restatement of newb16's suggestion.)
Oct 15 '10 #8
newb16
687 512MB
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
how mr.newb16 ??
Oct 15 '10 #10
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

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

Similar topics

1
by: JohnC | last post by:
I have this exact same scenario. It is new and seems to be related to when we installed Adobe 7.0 Standard/Professional. We have an MDB on a LAN file server. Using Access 2K and Windows 2K. ...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
6
by: sreelal | last post by:
Dear friends, Please consider the following C program. #include<stdio.h> struct employee { int id;
6
by: Ab | last post by:
What does the "%%" format specifier in scanf do? eg if we write char ch; scanf("%%", &ch); what change if any does it make to ch. If it does not whats the use.
12
by: Sharon | last post by:
I’m wrote a small DLL that used the FreeImage.DLL (that can be found at http://www.codeproject.com/bitmap/graphicsuite.asp). I also wrote a small console application in C++ (unmanaged) that uses...
3
by: bob.needler | last post by:
I know On Error Resume Next is generally considered lazy. But can someone tell me why the resume next in Exit_Handler does not seem to work? It generates the typical unhandled runtime error...
9
by: shades234 | last post by:
When i go to open my Access database from certain computers, i get multiple popup messages stating the same thing. They all say "You do not have exclusive access to the database at this time. If...
1
by: Josta7 | last post by:
Hi. I've been using Access for a couple of years now, but everything I know about it is self-taught. This makes me a little self-conscious/worried - I feel like I know how to do a lot, but I could...
2
by: suneel pandey | last post by:
when I open aspx page from html this error occur Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the...
1
by: 2desperate2usedesperate | last post by:
ERROR: You do not have SELECT privileges on sys.V_$PARAMETER view. This process cannot proceed. Hello, I am using ORACLE 10g Standard Edition and I am trying to configure the Oracle Designer for...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.