Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 3rd, 2005, 5:49 PM   #1
levkur
Newbie
 
Join Date: Apr 2005
Posts: 5
Rep Power: 0 levkur is on a distinguished road
Do-While loop runs only twice

I'm fairly new at programming and need some help whit this program. The DO-while loop only prompts to Run Again only once and just quites after running.

code:

#include<stdin.h>
main()
{
int x,y,z;
char ans;
do
{ ...
printf("Run Again?(y,n)");
scanf("%c",&ans);
}
while(ans==89 || ans==121);
}

Please help
levkur is offline   Reply With Quote
Old Apr 3rd, 2005, 6:04 PM   #2
Daniel_kd
Newbie
 
Daniel_kd's Avatar
 
Join Date: Apr 2005
Posts: 22
Rep Power: 0 Daniel_kd is on a distinguished road
the problem is that scanf("%c",ans); reads only one character the '\n'(new line character) that is inserted by the enter button is read burring the second run and gets out of the loop as the while condition is 0.

to solve this add getchar() at the end of the loop.
so the code will be looking like this
#include<stdio.h>
main()
{
int x,y,z;
char ans;

do
{ 
printf("Run Again?(y,n)");
scanf("%c",&ans);
getchar();
}
while(ans==89 || ans==121);
}

Actually if the user enters a lot of charactrs this will take only the first one. If you want to "clean" more chars you have to declare an array of chars and use gets(char *) like this:
#include<stdio.h>
main()
{
int x,y,z;
char ans, useless[80];

do
{ 
printf("Run Again?(y,n)");
scanf("%c",&ans);
gets(useless);
}
while(ans==89 || ans==121);
}

finally the same effect will have the command "fflush(stdin);" after the fscan in the loop. This cleans everything that has been typed by the user and hasn't yet been "read" by the program

Last edited by Daniel_kd; Apr 3rd, 2005 at 7:47 PM.
Daniel_kd is offline   Reply With Quote
Old Apr 3rd, 2005, 6:56 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I suggest using the fflush(stdin); statement instead of getchar - getchar might screw up on a different system.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 5th, 2005, 12:55 AM   #4
levkur
Newbie
 
Join Date: Apr 2005
Posts: 5
Rep Power: 0 levkur is on a distinguished road
thanks alot daniel and ooble. I was looking for the fflush function but i could remeber it until i saw it here. anyway, the fflush fuction didnt work for some reason, it said stdin was undefined. I don;t know whats going on. The getchar function worked so thanks alot for all you help.
levkur is offline   Reply With Quote
Old Apr 5th, 2005, 6:54 AM   #5
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
Just a small note:

1. Do not use the fflush function on input buffers, this is undefined behaviour.

2. Never use the gets function, use the fgets function instead.

To flush the input buffer, you can use the getchar function in a while loop:
while ((ch = getchar()) != '\n' && ch != EOF);
Monster is offline   Reply With Quote
Old Apr 5th, 2005, 7:23 AM   #6
nadiralishah
Newbie
 
Join Date: Apr 2005
Posts: 1
Rep Power: 0 nadiralishah is on a distinguished road
Quote:
Originally Posted by levkur
I'm fairly new at programming and need some help whit this program. The DO-while loop only prompts to Run Again only once and just quites after running.

code:

#include<stdin.h>
main()
{
int x,y,z;
char ans;
do
{ ...
printf("Run Again?(y,n)");
scanf("%c",&ans);
}
while(ans==89 || ans==121);
}

Please help
Try This........this will work

#include<stdio.h>
#include<conio.h>
main()
{
int x,y,z;
char ans;
do
{ ...
printf("Run Again?(y,n)");
ch = getche();
}
while(ch != 'n' || ch!= 'N");
}
nadiralishah is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC