LinuxQuestions.org
Help answer threads with 0 replies.
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 11-02-2006, 03:01 PM   #1
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Rep: Reputation: 15
Problem with C loop !


Hello there.... I have a strange problem with loops in C.
Code:
#include <stdio.h>
int main() {
     char ch;
     while (1) {
         printf("Give char: "); scanf("%c",&ch);
         if (ch == 'q') break;
     }
}
The above loop prints
First line: Give char: <char>
Second Line: Give char: Give char: <char>
Third Line: Give char: Give char: <char>
.
.
.

Well my question is an obvius one. Why the program prints 2 times the "Give char:" in second, third......n line ??
And how i can fix that?

Thanx in Advance !!!
 
Old 11-02-2006, 03:07 PM   #2
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
The reason why the loop never ends is because the condition you set is always true. For a loop to finish, the condition has to be false at some point.
 
Old 11-02-2006, 03:25 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by linuxlover1
Well my question is an obvius one. Why the program prints 2 times the "Give char:" in second, third......n line ??
when you are reading in from stdin a \n gets left in the buffer, you need to clear that. just call getchar() once after the call to scanf and that should do it..

Quote:
Originally Posted by reddazz
The reason why the loop never ends is because the condition you set is always true. For a loop to finish, the condition has to be false at some point.
did you over look the break in the loop..
 
Old 11-02-2006, 03:32 PM   #4
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Quote:
Originally Posted by xhi
...did you over look the break in the loop..
My bad, it looks like I did.
 
  


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
ksh and while..do loop problem Harry Seldon Programming 1 04-17-2006 04:18 PM
problem with do while loop. RHLinuxGUY Programming 12 02-06-2006 03:44 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
bash for loop problem deadlock Programming 5 09-04-2003 04:32 AM
loop problem andym Linux - Newbie 0 06-05-2003 05:12 AM

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

All times are GMT -5. The time now is 06:57 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