944,103 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3613
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 23rd, 2004
0

Help with input

Expand Post »
I want to take integer inputs separated by spaces and terminated by newline character. suppose the user will be inserting integers this way
123 566 789 45 34 8999 341 57 67 and then the user presses enter.
so the program will read all the integers when the user presses enter. The integers will be put into a linked list.

Note: 1. I dont want to do any parsing(first taking them as string and then separating the integers).
2. I could use an end condition like enter -1 to stop taking inputs, but dont like that idea either.

The thing is i m trying to make a electronic menu driven index, where there is a binary search tree and each treenode contains a word and a linked list of integers to hold the pages where the word appears in. I will first prompt the user for a word and then for the page numbers. But i need to read the inputs(page numbers) in a smart and convenient way. Let me know if u guys can help me with that.
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 23rd, 2004
0

Re: Help with input

try something like
  1. vector<int> v;
  2. string s;
  3. while (cin<<s)
  4. {
  5. if (s != "\n") v.push_back(atoi(s.c_str()));
  6. }

replace \n with the corresponding value for your OS (or find some way to detect it and use the detected value).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 23rd, 2004
0

Re: Help with input

To Jwenting,
ahem ahem,:rolleyes:
Thanx for that boss,BUT
i havent learned to use STL yet, and i dont plan to use them until i study them thoroughly and that's not gonna happen anytime soon. Do u have any non-STL solution to the problem?
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 23rd, 2004
0

Re: Help with input

hmm, I do believe in using the best tool for the job and that's the STL.

Another way would have you create a linked list of ints and read them into that instead of into a vector.
The STL solution probably does something similar internally, clearing you of all that messing around with pointers.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 23rd, 2004
0

Re: Help with input

Quote originally posted by Asif_NSU ...
I want to take integer inputs separated by spaces and terminated by newline character. suppose the user will be inserting integers this way
123 566 789 45 34 8999 341 57 67 and then the user presses enter.
so the program will read all the integers when the user presses enter. The integers will be put into a linked list.

Note: 1. I dont want to do any parsing(first taking them as string and then separating the integers).
These two requirements may be somewhat at odds. Input functions that are whitespace delimited may not distinguish between a space and a newline.

I suppose you could write your own input functions, but that seems like much more work than reading and parsing a line.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 23rd, 2004
0

Re: Help with input

Quote ...
Another way would have you create a linked list of ints and read them into that instead of into a vector.
I do have a linked list of integers and i have to insert the page numbers into it, but thats not the problem --the problem is about the end condition when it will stop inserting.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 23rd, 2004
0

Re: Help with input

Quote ...
These two requirements may be somewhat at odds. Input functions that are whitespace delimited may not distinguish between a space and a newline.
Well i got ur point. Can u suggest me any other technique to read in the page numbers easily one by one untill we meet some "end condition". What would be a smart end condition?
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 23rd, 2004
0

Re: Help with input

Quote originally posted by Asif_NSU ...
Can u suggest me any other technique to read in the page numbers easily one by one untill we meet some "end condition". What would be a smart end condition?
EOF?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 24th, 2004
0

Re: Help with input

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

Re: Help with input

you're parsing numbers from the input.
I guess you're using atoi to turn the input into ints as you'll be reading it as char* ?
Check the docs for atoi, it will return 0 if the input is not recognised.
Pagenumber 0 is highly unusual, you could use that as a boundary condition (or a negative number might even be better).

So say you want the user to terminate the input by typing -1. atoi("-1") will return -1 which will flag you that the user is done inputting text.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 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: need help outputting arrays
Next Thread in C Forum Timeline: Help on assignment





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


Follow us on Twitter


© 2011 DaniWeb® LLC