| | |
C command-line I/O question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 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.
•
•
Join Date: Sep 2003
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
Does this command exist in C?
•
•
•
•
And is this the best way to read a line of input?
C++ Syntax (Toggle Plain Text)
while ((input = getche()) != '\n' && length <= max_length) { buffer[length] = input; length++; }
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.
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.
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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
- Command line question (Python)
- command line question... (Windows NT / 2000 / XP)
- What is the Command-Line Argument for??? (C++)
- Command-line Arguments. (C++)
Other Threads in the C++ Forum
- Previous Thread: Question about C++
- Next Thread: Custom Cout<< method
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





