Help with input

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

Help with input

 
0
  #1
Nov 23rd, 2004
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.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
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: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help with input

 
0
  #2
Nov 23rd, 2004
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).
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
  #3
Nov 23rd, 2004
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?
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
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: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help with input

 
0
  #4
Nov 23rd, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
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: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with input

 
0
  #5
Nov 23rd, 2004
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.
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
  #6
Nov 23rd, 2004
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.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
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
  #7
Nov 23rd, 2004
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?
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
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: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with input

 
0
  #8
Nov 23rd, 2004
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?
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
  #9
Nov 24th, 2004
"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?
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
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: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help with input

 
0
  #10
Nov 24th, 2004
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.
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