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 08-02-2004, 04:11 PM   #1
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
Question Getchar, scanf, loops and so on


Hello, i have a little problem which i cannot understand before, it is input keyboard functions and loops (or anything makes them to repeat)

when you do this:

int c;
do
c = getchar();
while(1);

the getchar calling function is skipped several times, (i.e. my program does not stop, it does not wait for keyboard input), and i must do the following in order to my program waits for the keyboard input:

int c;
do{
c = getchar();
while(getchar() == '\n');
}
while(1);

But i don't understand

why must i do that??

TIA
 
Old 08-02-2004, 04:49 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
The logic you're using doesn't make a lot of sense. In your first loop there's no way out. The program will just sit there in the loop forever. What are you trying to accomplish? If you want to get a line of text from stdin try this:
Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
  int c;

  do
  {
    c = getchar();
    printf("%c", c);
  } while(c != '\n');

  return 0;
}
Quote:
$ ./getchar
This is a string
This is a string
$
The first "This is a string" is what I typed in. The second line shows up as soon as I hit ENTER. This is exactly the way the program is expected to work.
 
Old 08-03-2004, 01:48 AM   #3
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
Thank you, i thought the program don't wait for the keyboard input, and it skip the getchar calling few times.
 
  


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
getchar() satellite Programming 10 12-24-2017 01:47 PM
about getchar() captainstorm Programming 6 10-11-2003 04:14 AM
getchar() NSKL Programming 18 01-11-2003 10:19 AM
getchar() not working? lackluster Programming 3 06-24-2002 08:09 AM
getchar() Winter Programming 6 05-11-2002 01:49 AM

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

All times are GMT -5. The time now is 10:13 PM.

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