| | |
Playing with inputs
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
In my program i will be taking two inputs, lets say two integers. If the number of input is more than two the program will exit. If it is two then the loop will be executed and after that it will again ask for two new inputs-- and will continue this way. How do I accomplish this in PURE C(no C++)? I know this is not necessary to do in any sort of program, but asking just out of curiosity.
Another ques: also how do I clear the input stream in C(not clrscr())?
Another ques: also how do I clear the input stream in C(not clrscr())?
Last edited by Asif_NSU; Aug 30th, 2004 at 5:17 pm. Reason: none
One way:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int a,b; char line [ BUFSIZ ]; for ( ;; ) { fputs("Enter two integer: ", stdout); fflush(stdout); if ( fgets(line, sizeof line, stdin) ) { if ( sscanf(line, "%d%d", &a, &b) != 2 ) { return 0; } printf("a = %d, b = %d\n", a, b); } } return 0; } /* my output Enter two integer: 12 45 a = 12, b = 45 Enter two integer: 11 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
Originally Posted by Asif_NSU
thanx man! But the program should also quit if there are more than two inputs. I can see the program discards additional inputs but i want it to exit for more than two inputs.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int a,b,n; char line [ BUFSIZ ]; for ( ;; ) { fputs("Enter two integers: ", stdout); fflush(stdout); if ( fgets(line, sizeof line, stdin) ) { if ( sscanf(line, "%d%d%n", &a, &b, &n) == 2 && line [ n ] == '\n') { break; } } puts("try again"); } printf("a = %d, b = %d\n", a, b); return 0; } /* my output Enter two integers: 1 try again Enter two integers: 1 2 3 try again Enter two integers: a b try again Enter two integers: 1 2a try again Enter two integers: 1 2 a = 1, b = 2 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- computer freezes when playing games (Troubleshooting Dead Machines)
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- Hello I am new here. can someone explain how to fix a segmentation fault (C++)
- Computer generated lag while playing MOHAA (Troubleshooting Dead Machines)
Other Threads in the C++ Forum
- Previous Thread: Programming help plz! any one can...
- Next Thread: Craps, game help!
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






