Playing with inputs

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Playing with inputs

 
0
  #1
Aug 30th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,406
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Playing with inputs

 
0
  #2
Aug 30th, 2004
One way:
  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.  */
"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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Playing with inputs

 
0
  #3
Aug 30th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 4
Reputation: GDFans is an unknown quantity at this point 
Solved Threads: 0
GDFans GDFans is offline Offline
Newbie Poster

Re: Playing with inputs

 
0
  #4
Sep 4th, 2004
easy.
You can use 'exit(0)' instead of 'return 0'
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Playing with inputs

 
0
  #5
Sep 4th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,406
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Playing with inputs

 
0
  #6
Sep 4th, 2004
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...
  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.  */
"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
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC