| | |
Help with input
![]() |
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.
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)
try something like
replace \n with the corresponding value for your OS (or find some way to detect it and use the detected value).
C Syntax (Toggle Plain Text)
vector<int> v; string s; while (cin<<s) { if (s != "\n") v.push_back(atoi(s.c_str())); }
replace \n with the corresponding value for your OS (or find some way to detect it and use the detected value).
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?
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)
•
•
•
•
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).
I suppose you could write your own input functions, but that seems like much more work than reading and parsing a line.
•
•
•
•
Another way would have you create a linked list of ints and read them into that instead of into a vector.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
•
•
•
•
These two requirements may be somewhat at odds. Input functions that are whitespace delimited may not distinguish between a space and a newline.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
"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?
>>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)
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.
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.
![]() |
Similar Threads
- PHP Form Input (PHP)
- need input or suggestions for new website-thx (Website Reviews)
- i want the user to enter an input without obligating him to press enter (C++)
- Input Signal Out of Range (Windows NT / 2000 / XP)
- Tab control on hidden input fields (HTML and CSS)
Other Threads in the C Forum
- Previous Thread: need help outputting arrays
- Next Thread: Help on assignment
| Thread Tools | Search this Thread |
* adobe ansi api array binarysearch centimeter changingto char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax database directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping lowest match meter microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi






