DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   C command-line I/O question (http://www.daniweb.com/forums/thread1228.html)

phickman7872 Oct 4th, 2003 6:07 am
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.

Mike29936 Oct 7th, 2003 11:57 pm
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:

while ((input = getche()) != '\n' && length <= max_length) {
buffer[length] = input;
length++;
}

subtronic Oct 8th, 2003 11:56 am
Re: C command-line I/O question
 
You need to flush the:
printf("What street do you live on? ");
to stdout.

phickman7872 Oct 12th, 2003 12:40 pm
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.

yap.nice Aug 4th, 2008 6:51 am
Re: C command-line I/O question
 
Quote:

Originally Posted by phickman7872 (Post 6342)
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


All times are GMT -4. The time now is 4:47 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC