how can i insert unknown number of strings with cin

hello all,

i would like to ask how could i use cin to take unknown number of strings
for example i want to take and use all the strings below

alpha beta gama

but also the strings below

alpha beta


or

alpha beta gama delta


for a known nuber of strings i use this

char* firstword=new char[20];
char *secondword=new char[20];
char *thirdword=new char[20];
cin>>firstword>>secondword>>thirdword;

but with unknown number of strings??
1
2
3
4
5
6
char newword[20][1000] = {{0}};

for (int i =0 ; (i<1000) && (newword[0][i]==NULL) ; i++)
{
    cin >> newword[][i];
}

Would that work?

[Edit]: Thanks for the correction vagelis
Last edited on
yes the double dimension table would be a good idea..
is your code correct??it seems strange to me..for example char[0][i]==null
did you mean newword[0][i]==null?

and another question is how can i clear the cin

for example
if it waits that three strings come and come only two how can make it not wait for the third?

Last edited on
closed account (DSLq5Di1)
This would be handled better using getline and a vector of strings. http://ideone.com/7uVIR
can i with some way go to the end of the command line input??
so that the next cin doesnt see something in the command line and so it will wait new entry
Actually in our school ,we are only required to use one library and that's <iostream.h>,with this one library we are still able to make programs that are unexpectedly amazing ,well I'm just a newbie with c++ but then, I'm just going to make some corrections with the program gaved by stewbond ,with that program ,the user is required to input 1000 characters, what if let's place a condition to make the inputting of characters by the user stop. Let's say inputting of character will stop if the user enter #0.

1
2
3
4
5
6
7
8
9
#include<iostream.h>
void main()
{
char a[25];
cout<<"Enter #0 if you want to stop inputting characters";
do{
cin>>a;
}while(a!=0); //condition stands as a stopper of the program.
}

Is it this?
1
2
3
4
5
char strs[100][80];
int i;
for(i = 0; i < 100 && cin; i++)
   cin >> strs[i];
cout << i - 1 << " strings were read.";
Syuf i want somthing like this to check the cin if it is at the end
but i see that neither cin nor !cin.eof work appropriate..

so how can i check if i am at the end of the cin?
and how can i clear the cin??
std::vector<std::string> +1

vagelis wrote:
can i with some way go to the end of the command line input?? so that the
next cin doesnt see something in the command line and so it will wait new entry

[EDIT]
vagelis wrote:
so how can i check if i am at the end of
the cin? and how can i clear the cin??
[/EDIT]

What do you mean? Can you give a concrete example of the problem you
have? What do you want your program to do, and what does it do instead?
Last edited on
so how can i check if i am at the end of the cin?
1
2
if(cin.eof())
   // You are at the end of the stream 

and how can i clear the cin?
1
2
cin.clear(); // clear all the error flags and set ios::good flag
cin.seekg(0, ios::beg); // sets the get pointer to the begin of the file 

can i with some way go to the end of the command line input?
cin.seekg(0, ios::end); // sets the get pointer to the eof symbol, but don't set the eof flag - it will be set after a try to read data
Okay, I think I know what the OP is looking for...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string         word;
    vector<string> words;

    // CTRL-Z and then ENTER to stop

    while (cin >> word) words.push_back(word);

    cin.clear();

    cout << "\nyour words are:\n\n";

    for (unsigned i = 0; i < words.size(); ++i)
        cout << words[i] << endl;

    return 0;
}

It's similar to sloppy9's solution, but somewhat
simplified, so that the OP can see what's going on.
thank you all for the replies
the questions were excactly what you answered but i dont know why it doesnt work for me that think

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char** word=new char*[20];
for(int i=0;i<20;i++)
{
	word[i]=new char[20];
}
	
cin>>word[0]>>word[1]>>word[2];
if(a condition)
{
       int i=0;
      while(!cin.eof())
      {
		cin>>word[i+2];
		i++;
      }
}


i type in command line
alpha beta gamma (for example)

and i dont know why the programm goes inside the while loop and the cin>>word[i+2];
any idea??
Last edited on
If you hit CTRL-Z and then ENTER, cin.eof() will return true and your while
loop will stop. Just remember to also add a cin.clear() call after your loop.
but i dont want to do it for my self to run
i want always with enter when i give three strings not to go inside the loop
but if i give more than three to go inside the loop and get more strings

i see that cin's cursor for a reason has returned at the beginning at letter 'a' of the word alpha..
Last edited on
so how can i find if cin has read all the input or something is still there and waits the next cin?
vagelis wrote:
but i dont want to do it for my self to run

Then use a istringstream, as sloppy9 does. Here, I modified my example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> words;

    string word;
    string line;

    // get the whole line ...
    getline(cin, line);

    // ... then use it to create
    // a istringstream object ...
    istringstream buffer(line);

    // ... and then use that istringstream
    // object the way you would use cin
    while (buffer >> word) words.push_back(word);

    cout << "\nyour words are:\n\n";

    for (unsigned i = 0; i < words.size(); ++i)
        cout << words[i] << endl;
}

Last edited on
ok i will try this..
with a search in the web i think this is the solution..

thanks!
Topic archived. No new replies allowed.