LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-19-2004, 05:28 PM   #1
LUB997
Member
 
Registered: Jul 2003
Distribution: openSUSE Linux, Apple Darwin UNIX
Posts: 66

Rep: Reputation: 15
scanf() problem.


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);

return 0;
}
 
Old 12-19-2004, 05:49 PM   #2
niknah
Member
 
Registered: Dec 2002
Location: In front of a computer
Distribution: UPS, DHL, FedEx
Posts: 466

Rep: Reputation: 38
Quote:
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.
 
Old 12-19-2004, 06:46 PM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Code:
fflush(stdin);
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.
 
Old 12-19-2004, 08:03 PM   #4
LUB997
Member
 
Registered: Jul 2003
Distribution: openSUSE Linux, Apple Darwin UNIX
Posts: 66

Original Poster
Rep: Reputation: 15
Quote:
you have data left in the input stream after the first call and you must read that data.
That makes sense to me, but what do I have to do to read that data.
 
  


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
scanf question exvor Programming 5 07-24-2005 04:36 PM
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
scanf question jnusa Programming 4 01-07-2005 09:04 AM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM

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

All times are GMT -5. The time now is 03:56 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