cin help.

Hi,

I amin the process of writing a program that takes and input string and stores each character into a vector element. My problem is though how can i tell then the user has entered all of there text? right now i just said end with '*' and put that in the while loop condition.

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
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<vector>

using namespace std;

vector<char> GetInput ();

int main (){

vector<char> test;

test = GetInput();

for(int i=0;i<the.size();i++){
cout << test[i] << endl;


}

return 0;
}

vector<char> GetInput (){

vector<char> InputString;
 
cout << "Please input the string of symbol you would like to gain all permutations of: ";

char temp = 0 ;

while( temp != '*' ){  // here is my issue what should i put for the while loop condition?

cin >> temp;

InputString.push_back(temp);

}

return  InputString;

}
Using cin >> temp; will skip all the spaces entered so its better to use cin.get(temp);
cin is unlikely to provide any of the characters to the program until the user pressess the return key. Then the program will get everything typed all at once. So, the return code that is inserted into the input is the natural way to terminate your input:
1
2
3
4
5
while(temp != '\n')
{
    cin.get(temp);
    InputString.push_back(temp);
}

The natural way to read everything up to the return code is std::getline:
1
2
3
std::string line;
std::getline(std::cin, line);
// now you have everything the user typed before pressing the return key in std::string line 

That way you can then put every character in the string into your std::vector.
that worked, thanks for the help!
Topic archived. No new replies allowed.