943,338 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11984
  • C++ RSS
Oct 4th, 2003
0

C command-line I/O question

Expand Post »
Hi all,

I am new to C on linux and have a question about I/O. I am trying to read input from the command-line:

printf("What is your first name? ");
scanf("%s", firstName);
printf("What is your last name? ");
scanf("%s", lastName);
printf("What is your age? ");
scanf("%d", &age);
printf("What street do you live on? ");
streetAddress = fgets(buffer, 255, stdin);

However, when I run this code after entering the "age", the code totally skips the "Enter Address" and continues with next line of code. I think there was a command in C++ to flush the input buffer. Does this command exist in C? And is this the best way to read a line of input?

Thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phickman7872 is offline Offline
6 posts
since Oct 2003
Oct 7th, 2003
0

Re: C command-line I/O question

Quote ...
Does this command exist in C?
I believe it's fflush(). I could be wrong, though.

Quote ...
And is this the best way to read a line of input?
If all else fails, I do something similar to this:

C++ Syntax (Toggle Plain Text)
  1. while ((input = getche()) != '\n' && length <= max_length) {
  2. buffer[length] = input;
  3. length++;
  4. }
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003
Oct 8th, 2003
0

Re: C command-line I/O question

You need to flush the:
printf("What street do you live on? ");
to stdout.
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
Oct 12th, 2003
0

Re: C command-line I/O question

My thanks go to both of you for your responses.

I have just installed RedHat 9 on my home PC and am currently transitioning from GW-BASIC/QBASIC/Visual Basic to C/C++ using Qt.

That was always something that puzzled me by C, I guess it seems complex simply because of my background in other languages.

Thanks agin.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phickman7872 is offline Offline
6 posts
since Oct 2003
Aug 4th, 2008
-1

Re: C command-line I/O question

Hi all,

I am new to C on linux and have a question about I/O. I am trying to read input from the command-line:

printf("What is your first name? ");
scanf("%s", firstName);
printf("What is your last name? ");
scanf("%s", lastName);
printf("What is your age? ");
scanf("%d", &age);
printf("What street do you live on? ");
streetAddress = fgets(buffer, 255, stdin);

However, when I run this code after entering the "age", the code totally skips the "Enter Address" and continues with next line of code. I think there was a command in C++ to flush the input buffer. Does this command exist in C? And is this the best way to read a line of input?

Thanks in advance.
i dont know
Reputation Points: 6
Solved Threads: 0
Newbie Poster
yap.nice is offline Offline
3 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Question about C++
Next Thread in C++ Forum Timeline: Custom Cout<< method





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC