943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1998
  • C++ RSS
Aug 30th, 2004
0

Playing with inputs

Expand Post »
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())?
Last edited by Asif_NSU; Aug 30th, 2004 at 5:17 pm. Reason: none
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Aug 30th, 2004
0

Re: Playing with inputs

One way:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int a,b;
  6. char line [ BUFSIZ ];
  7. for ( ;; )
  8. {
  9. fputs("Enter two integer: ", stdout);
  10. fflush(stdout);
  11. if ( fgets(line, sizeof line, stdin) )
  12. {
  13. if ( sscanf(line, "%d%d", &a, &b) != 2 )
  14. {
  15. return 0;
  16. }
  17. printf("a = %d, b = %d\n", a, b);
  18. }
  19. }
  20. return 0;
  21. }
  22.  
  23. /* my output
  24.  Enter two integer: 12 45
  25.  a = 12, b = 45
  26.  Enter two integer: 11
  27.  */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 30th, 2004
0

Re: Playing with inputs

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.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Sep 4th, 2004
0

Re: Playing with inputs

easy.
You can use 'exit(0)' instead of 'return 0'
Reputation Points: 11
Solved Threads: 0
Newbie Poster
GDFans is offline Offline
4 posts
since Sep 2004
Sep 4th, 2004
0

Re: Playing with inputs

Quote ...
easy.
You can use 'exit(0)' instead of 'return 0'
What are u talking about? I dont see how replacing 'return 0' with exit(0) in the program provided by dave makes any difference.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Sep 4th, 2004
0

Re: Playing with inputs

Quote 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.
There are several ways. I would prefer to respond to one that says, "I've tried this <insert code>, but it doesn't do what I expect." But...
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int a,b,n;
  6. char line [ BUFSIZ ];
  7. for ( ;; )
  8. {
  9. fputs("Enter two integers: ", stdout);
  10. fflush(stdout);
  11. if ( fgets(line, sizeof line, stdin) )
  12. {
  13. if ( sscanf(line, "%d%d%n", &a, &b, &n) == 2 && line [ n ] == '\n')
  14. {
  15. break;
  16. }
  17. }
  18. puts("try again");
  19. }
  20. printf("a = %d, b = %d\n", a, b);
  21. return 0;
  22. }
  23.  
  24. /* my output
  25.  Enter two integers: 1
  26.  try again
  27.  Enter two integers: 1 2 3
  28.  try again
  29.  Enter two integers: a b
  30.  try again
  31.  Enter two integers: 1 2a
  32.  try again
  33.  Enter two integers: 1 2
  34.  a = 1, b = 2
  35.  */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

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: Programming help plz! any one can...
Next Thread in C++ Forum Timeline: Craps, game help!





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


Follow us on Twitter


© 2011 DaniWeb® LLC