C command-line I/O question

Reply

Join Date: Oct 2003
Posts: 6
Reputation: phickman7872 is an unknown quantity at this point 
Solved Threads: 0
phickman7872's Avatar
phickman7872 phickman7872 is offline Offline
Newbie Poster

C command-line I/O question

 
0
  #1
Oct 4th, 2003
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.
Nothing is impossible, given time and money.
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 22
Reputation: Mike29936 is an unknown quantity at this point 
Solved Threads: 0
Mike29936 Mike29936 is offline Offline
Newbie Poster

Re: C command-line I/O question

 
0
  #2
Oct 7th, 2003
Does this command exist in C?
I believe it's fflush(). I could be wrong, though.

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

  1. while ((input = getche()) != '\n' && length <= max_length) {
  2. buffer[length] = input;
  3. length++;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: C command-line I/O question

 
0
  #3
Oct 8th, 2003
You need to flush the:
printf("What street do you live on? ");
to stdout.
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 6
Reputation: phickman7872 is an unknown quantity at this point 
Solved Threads: 0
phickman7872's Avatar
phickman7872 phickman7872 is offline Offline
Newbie Poster

Re: C command-line I/O question

 
0
  #4
Oct 12th, 2003
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.
Nothing is impossible, given time and money.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: yap.nice is an unknown quantity at this point 
Solved Threads: 0
yap.nice yap.nice is offline Offline
Newbie Poster

Re: C command-line I/O question

 
-1
  #5
Aug 4th, 2008
Originally Posted by phickman7872 View 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.
i dont know
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC