There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop malware monitor motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound spyware trojan usb virus vista vista 32-bit windows windows xp windowsxp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Help this beginner with C


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
xxrsc's Avatar
Junior Member with 9 posts.
 
Join Date: Oct 2004
Experience: Intermediate
18-Oct-2004, 08:48 PM #1
Help this beginner with C
#include <stdio.h>

main()
{
int c, s;

c = 0;
printf("1.Deposit\n2.Withdrawal\n3.Exit\n");
while (c == 0)
{
s = getchar();
printf("%c", s);
c = getchar();
printf("%c", c);
}
}

I'm just beginning programming. This compiles ok. But when I execute it, the first getchar works fine. But it seems to skip the second getchar. What am I doing wrong?
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
19-Oct-2004, 12:24 AM #2
Use fflush(stdin) to clear the buffer after you do the first one.
xxrsc's Avatar
Junior Member with 9 posts.
 
Join Date: Oct 2004
Experience: Intermediate
19-Oct-2004, 07:26 PM #3
Thanks. That's what I was looking for.
bowmanjeff's Avatar
Junior Member with 3 posts.
 
Join Date: Oct 2004
21-Oct-2004, 10:48 AM #4
Quote:
Originally Posted by xxrsc
#include <stdio.h>

main()
{
int c, s;

c = 0;
printf("1.Deposit\n2.Withdrawal\n3.Exit\n");
while (c == 0)
{
s = getchar();
printf("%c", s);
c = getchar();
printf("%c", c);
}
}

I'm just beginning programming. This compiles ok. But when I execute it, the first getchar works fine. But it seems to skip the second getchar. What am I doing wrong?
The first getchar waits for you to press enter. Therefore you have actually
entered a single character followed by a newline.
The second getchar is comsuming the "newline" you entered.
The fflush is throwing away this newline.
xxrsc's Avatar
Junior Member with 9 posts.
 
Join Date: Oct 2004
Experience: Intermediate
22-Oct-2004, 11:21 PM #5
Excellent. Very helpful. Thanks.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:10 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.