ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to learn how to program in C, so I'm taking my C# programs I did in my computer class and trying to convert them to C using my "The C Programming Language" book. I'm working on a really simple program, but scanf() is giving me a lot of trouble. It works the first time I call it, but the second time I call it, the program just skips right over it and goes to the next statement in my code. I looked it up on google, and I read something that said to try flushall();. I tried that, but it wouldn't compile because that is evidently a Microsoft Visual C specific command, and then I found a webpage that said to try fflush(stdin);. I tried tha too, but it isn't working either. I'm using SuSE 9.1 with GCC version 3.3.3, and here's my code: A suggestion that is intended for GCC and not M$ Write Our Own Version of Every Language Just to Be Different would be very helpful.
#include <stdio.h>
long length, width, perimeter, area;
int main() {
printf("\nApplication: Ch3Prb1 -- Calculate the perimeter and area of a rectange\n\n");
printf("Length: ");
fflush(stdin);
scanf("%ld", &length);
printf("Width: ");
fflush(stdin);
scanf("%ld", &width);
It works the first time I call it, but the second time I call it, the program just skips right over it and goes to the next statement in my code.
it seems to work fine for me. (used "gcc t.c" to compile; "./a.out" to run)
how are you running this a "second" time? via an IDE? or on the command line.
doesn't do what you think it does. If there is data in the input stream that data must be read or ignored. fflush() is used for flushing the output buffer, when writing to the screen or the file.
That's why your program is skipping the second scanf() call, you have data left in the input stream after the first call and you must read that data.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.