LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-28-2013, 08:06 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
getchar() and '\n' problem


Ignore this post!. Found solution to problem in the next chapter of book I'm learning from!



C beginner, trying to learn how to pass over '\n' in the following simple program. I just don't get it! Program gets stuck with ch = '\n'.

Code:
#include <stdio.h>

int main(void)
{
int oper;
char ch;
printf("Enter a character or \"q\" to exit\n");

ch = getchar();
while(ch!= 'q')
{
    printf("Enter an integer:\n");
    while(scanf("%d", &oper)==1)
    {
     printf("%d is the integer entered\n",oper);
    printf("Enter a character or \"q\" to exit:");
    ch=getchar();
    }
}
return 0;
}
Thanks for your help.

Last edited by atlantis43; 07-28-2013 at 09:50 PM. Reason: problem solved
 
Old 07-29-2013, 02:05 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
instead of 'char ch;' use 'int ch;' -- this way you can check for 'ch==EOF' meaning 'no input -- end of file'
 
  


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
Help with getchar() please geraneum Programming 9 12-18-2008 04:42 AM
getchar() problem (also conio.h) faizlo Programming 21 09-06-2008 06:27 AM
getchar() problem. Read other threads. jstephens84 Programming 3 11-20-2005 04:10 PM
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 02:47 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