try something like
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).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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? :twisted:
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
"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 asclearerr 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 );
}
<strong>rewind ( stdin );</strong>
}
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.I suppose you could write your own input functions, but that seems like much more work than reading and parsing a line. 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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>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!]
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337