CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

Cant make single char in a loop. topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 05-30-2010, 02:03 AM
Junior Member
 
Join Date: May 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
mmondal71 is on a distinguished road
| More
Cant make single char in a loop.

Hello.

So I've been doing some C today... and wondering how the heck do I get a single char in a loop? I've used getchar and scanf, they both read my newline and skip prompt on 2nd iteration.
Reply With Quote
  #2  
Old 05-30-2010, 02:04 AM
Junior Member
 
Join Date: May 2010
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
spoddar66 is on a distinguished road
| More
Re: Cant make single char in a loop.

getchar() should work.

Show us the code and the input.
Reply With Quote
  #3  
Old 05-30-2010, 02:05 AM
Junior Member
 
Join Date: May 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
mmondal71 is on a distinguished road
| More
Re: Cant make single char in a loop.

Code:
#include <stdio.h>
 
main()
{
        char c;
 
        while(1)
        {
                printf("Enter a char: ");
                c = getchar();
                printf("%d\n", c);
        }
}

Last edited by pankaj.sea; 05-30-2010 at 03:26 AM. Reason: BB Code misplaced
Reply With Quote
  #4  
Old 05-30-2010, 02:06 AM
Junior Member
 
Join Date: May 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
singh_r85 is on a distinguished road
| More
Re: Cant make single char in a loop.

Its doing exactly the correct thing getting one character at a time.
The problem is that you have two sources going to the same device.

Keyboard input is going to terminal.
Output from your program is also going to the terminal.

Simple solution:

Run the command in one terminal and look at the output of the program in another terminal.

How:
Execute program and output to a file:
./a.out > PLOP

tail the file from another terminal

tail -f PLOP

PS. You need the -f. It makes tail wait for more input to the file.
Reply With Quote
  #5  
Old 05-30-2010, 02:07 AM
Junior Member
 
Join Date: May 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
mmondal71 is on a distinguished road
| More
Re: Cant make single char in a loop.

Re: Properly get a single character.
« Reply #6 on: September 17, 2008, 08:38:32 AM »

Quote from: myork on September 17, 2008, 08:14:38 AM
The problem is that you have two sources going to the same device.

Are you sure?

I think the problem is the newline stays in buffer, and is read up on next getchar() call.

I think reading the stream until newline is a simpler solution:

Code:
int myGetChar()
{
        int c, dummy;
 
        c = dummy = getchar();
        while(dummy != '\n' && dummy != EOF)
                dummy = getchar();
 
        return c;
}

Last edited by pankaj.sea; 05-30-2010 at 03:26 AM. Reason: BB Code misplaced
Reply With Quote
Reply

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is loop techinspiration C Programming 3 08-07-2010 06:11 AM
Loop problem creative C Programming 6 04-02-2010 09:17 PM
Creating Multiple Dialog Frames in a single MFC Application manmfc MFC Programming 1 02-26-2008 02:23 AM
Displaying more than one vedio in a single window using vmr7 mahanesh Win32 Programming 0 02-20-2008 02:23 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 05:02 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board