Help with input

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2004
Posts: 4,461
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: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with input

 
0
  #11
Nov 24th, 2004
Originally Posted by Asif_NSU
"EOF"
>>I did try that too. But the problem is when i signal the EOF it stops taking input alright but doesnt take any other inputs after that. Say after inserting the page numbers the user might want to go back to the main menu and then from their might choose to search for a word in the dictionary, but using EOF as the end condition doesnt let u do that. So any other suggestions?
Narue will likely mention something I've overlooked with this, such as clearerr or something, but...
#include <stdio.h>

int main ( void )
{
   int i, number;
   for ( i = 0; i < 3; ++i )
   {
      fputs ( "page number(s) ? ", stdout );
      fflush ( stdout );
      while ( scanf ( "%d", &number ) == 1 )
      {
         printf ( "number = %d\n", number );
      }
      rewind ( stdin );
   }
   return 0;
}

/* my output
page number(s) ? 12 23 34 45^Z
number = 12
number = 23
number = 34
number = 45
page number(s) ? 78 89 90^Z
number = 78
number = 89
number = 90
page number(s) ? 1 2 3^Z
number = 1
number = 2
number = 3
*/
But I find this kinda clunky anyway.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: Neo99 is an unknown quantity at this point 
Solved Threads: 0
Neo99 Neo99 is offline Offline
Newbie Poster

Re: Help with input

 
0
  #12
Nov 24th, 2004
Hey everyone,

You all are high level programmers..and i'm really just a begginer in front

of you all but still if i might ask Asif why dont you use the function

which scan keys, as they do in programming games.

char ch;
ch=getch();
if(ch==13)
{....}
13 is the ascii code for Enter Key..

Is it possible this way?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
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: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with input

 
0
  #13
Nov 24th, 2004
Originally Posted by Neo99
You all are high level programmers..and i'm really just a begginer in front

of you all but still if i might ask Asif why dont you use the function

which scan keys, as they do in programming games.
I did mention this already.
Originally Posted by Dave Sinkula
I suppose you could write your own input functions, but that seems like much more work than reading and parsing a line.
Originally Posted by Neo99
char ch;
ch=getch();
if(ch==13)
{....}
13 is the ascii code for Enter Key..
Ew. You're going to have a lot of fun unlearning bad habits.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: Neo99 is an unknown quantity at this point 
Solved Threads: 0
Neo99 Neo99 is offline Offline
Newbie Poster

Re: Help with input

 
0
  #14
Nov 24th, 2004
well instead of if(ch==13)
while(ch!=13)
{all the inputting inside}
what do u think..
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
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: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with input

 
0
  #15
Nov 24th, 2004
>what do u think..

I think it would be a worthwhile exercise, then you'd know why I said, "but that seems like much more work than reading and parsing a line."

[Don't forget about backspaces!]
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help with input

 
0
  #16
Nov 24th, 2004
getch() is platform specific, it's DOS/Win only.
We try to keep code as much platform independent as possible for as long as possible.
Besides, getch() gets a single character and the problem is about reading and parsing a string.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: Help with input

 
0
  #17
Nov 25th, 2004
This is just an example, but demonstrates reading in and outputing to( could be file or whatever) the screen. Enter the numbers you want seperated by a space. when you are finished hit enter then crtl + z and enter key again to output to the screen.
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iterator>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. // create a string vector
  11. // inialized by all the words from standard input
  12. vector<string> v((istream_iterator<string>(cin)),
  13. istream_iterator<string>());
  14.  
  15. //sort elements
  16. sort( v.begin(), v.end() );
  17.  
  18. //print all elements ignoring subsquent duplicates
  19. unique_copy( v.begin(), v.end(),
  20. ostream_iterator<string>(cout, "\n") );
  21.  
  22.  
  23. return 0;
  24. }
big146
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: Help with input

 
0
  #18
Nov 25th, 2004
why is everyone so much into STL?
Did i not explain why i dont want to use STL? Cos i m not into it yet. My knowledge on C++ is limited. Well what the heck, i have done it by parsing -- seems like that's the best way (without STL ofcourse). The thing is i encounter this type of problems so often that i wanted to know if there were any easy alternatives. Becos my knwoledge on C++ is limited i thought u guys would suggest something i did not know but was very simple to use. And in return i get codes that are either STL or uses advance techniques that are no easier than the parsing(using getchar() -- platform independent). So back to old dog's trick. Thanx for the help though.
Reply With Quote Quick reply to this message  
Reply

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




Views: 3301 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC