LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2008, 09:38 AM   #1
alice_neo
Member
 
Registered: Feb 2007
Location: Las vegas, US
Distribution: Fedora 9.................nowdayz
Posts: 39

Rep: Reputation: 15
a problem in c in gcc 4.1.2


hi guys!!!!!!

i have a problem in c in gcc-4.1.2
the problem is

#include<stdio.h>
int main()
{
int x=10;
char ch;
do
{
printf("%d",x);
printf("\nDo you want it again? ");
scanf("%c",&ch); //problem is here
}while(ch=='y'||ch=='Y');
return 0;
}

on compiling and running it, the output is as shown

[bryan@laptop ~]$ gcc test_do_while.c -o test_do_while.o
[bryan@laptop ~]$ ./test_do_while.o
10
Do you want it again? y
10
Do you want it again? [bryan@laptop ~]$


The second time it does not wait for a character to be entered not even with getch() and fgetc(stdin)

but if i add a space in the scanf statement of ch as shown

#include<stdio.h>
int main()
{
int x=10;
char ch;
do
{
printf("%d",x);
printf("\nDo you want it again? ");
scanf(" %c",&ch); //space added before %c inside double quotes
}while(ch=='y'||ch=='Y');
return 0;
}

the program works fine.............


the problem is same with other loops too............. adding the space there removes the problem...........
can anyone tell me the reason for this......please.........
 
Old 01-07-2008, 02:16 AM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Because in the first case it reads the newline character too, when you press Enter.
man scanf explains it here:
Code:
       c      Matches  a  sequence  of characters whose length is specified by
              the maximum field width (default 1); the next pointer must be  a
              pointer to char, and there must be enough room for all the char-
              acters (no terminating null byte is added).  The usual  skip  of
              leading  white  space is suppressed.  To skip white space first,
              use an explicit space in the format.
Just remember that the newline character is a "white space character".
 
Old 01-07-2008, 09:14 AM   #3
author_unknown
Member
 
Registered: Aug 2007
Location: Buffalo, NY
Distribution: Fedora 7, RHEL5, CentOS 5 and Backtrack 2.0
Posts: 73

Rep: Reputation: 15
uncle_theoddre (sorry for spelling mistake) is right. the '\n' character gets qued up and is accepted as the arguement when the second time scanf statement is executed.

you can add the statement
fflush(stdin);

this would flush up the standard input buffer so u won't have any problem but this is not a permanent solution as fflush statement sometimes does not seem to function properly.....okay.....so use at your own risk!!!!!!!!!!!

hugs and codes
author_unknown
 
Old 01-07-2008, 10:13 PM   #4
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Ubuntu 10.04
Posts: 843
Blog Entries: 1

Rep: Reputation: 30
author_unknown,

I'm no expert in C, but according to what I've read before, fflush() is meant for output streams, not input.
If I'm correct, it won't work that way.

Does anyone have a different experience?
 
Old 01-08-2008, 04:26 AM   #5
author_unknown
Member
 
Registered: Aug 2007
Location: Buffalo, NY
Distribution: Fedora 7, RHEL5, CentOS 5 and Backtrack 2.0
Posts: 73

Rep: Reputation: 15
well mimithebrain,
its not so u can use fflush for stdin also but sometimes it may happen dat fflush is unable to flush the input buffer, i dont know the reason but its true.......
 
Old 01-08-2008, 09:23 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Yours is a variation on the popular 'character-at-time' terminal IO problem, or what I like to call the 'getch()' problem. Google search for "character-at-a-time IO" for a long list of solutions and descriptions of the issues involved. Searching for 'getch' in this forum will also lead to informative threads.
--- rod.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC Problem Toonses82 Ubuntu 8 11-24-2005 05:33 AM
Solved Problem Second GCC-Pass(or gettext sanity check problem) hoes Linux From Scratch 0 09-04-2005 10:20 AM
problem with Gcc 3.2,2 dogbird Programming 2 05-29-2005 10:08 AM
Problem with GCC thedeath2k4 Linux - Software 3 04-06-2004 10:05 PM
Problem with gcc 3.2.2 gbglass2000 Slackware 5 08-20-2003 09:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration