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.

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).

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?

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.

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.

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.

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?

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:

"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?

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.

"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 );
      }
      [B]rewind ( stdin );[/B]
   }
   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.

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?

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.

well instead of if(ch==13)
while(ch!=13)
{all the inputting inside}
what do u think..

>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!]

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.

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.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
	// create a string vector
	// inialized by all the words from standard input
	vector<string> v((istream_iterator<string>(cin)),
		             istream_iterator<string>());

	//sort elements
	sort( v.begin(), v.end() );

	//print all elements ignoring subsquent duplicates
	unique_copy( v.begin(), v.end(),
		         ostream_iterator<string>(cout, "\n") );


	return 0;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.