Taking strings from overloaded std::cin

How would i code this?

A simple program that accepts the user to put any amount of words in.
They can ONLY enter words one time.
The program will process through the strings and simply cout the strings
The program will end after the last word is processed

This is what i have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream> #include<string>
std::string word;

int main(int argc, char* args[]) {
 std::cin >> word;
 std::cout << word; //This will print the first word

 while(std::cin) {  //This is where im stumped, 
                    //i want to tell the program WHILE CIN IS NOT EMPTY
  std::cin >> word;
  std::cout << word;
  }
}


I thought about taking one string through getline(cin,line) And then weeding through that until i reached the end file, while it will work, Im still curious about how i would get this to work with taking words from the overloaded input buffer

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


int main(int argc, char* args[]){
	std::string line, word;
	getline(std::cin,line);
	for(std::string::size_type i= 0; i!=line.size(); ++i)
	{
		if(isspace(line[i]))
		{
			std::cout << word << std::endl;
			word ="";
		}
		else
		 word += line[i];
	}
	std::cout << word;
	return 0;
}

The above code works exactly like i intended the program to function...
But im still looking for the other answer. How would i take ONE input (from user)
Then assign each word to a string FROM STD::CIN

Any help would be appreciated
Last edited on
closed account (NUj6URfi)
Try

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<string>
using namespace std;

string word;

int main() {
 cin >> word;
 cout << word;
 while(cin >> word; word == "") {
  cin >> word;
  cout << word;
  }
} 



No luck, I like the attempt, but it still will constantly ask the user for more input

having cin >> word in the while statement will always prompt the user for input. It will test "cin >> word" and continue with the loop unless there was an error according to the input
(error would translate to false)

The only way to have the cin in the while statement, and not promt the user for input, would have to be something like this

while(word == "" && cin >>word)

If word was NOT ""(blank) in this case, then the condtion would skip over cin>>word due to a short circuit
Last edited on
closed account (NUj6URfi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
using namespace std;

string word;
int i;

int main() {
 cin >> word;
 cout << word;
 i = 1;
while (i == 1) {
cin >> word;
while(word == "") {
  cin >> word;
  cout << word;
i++;
  }
}
}  
Last edited on
Thanks for your reply, but it doesnt solve anything

in your code, once you enter the loop
while(i == 1)
The cin will take all its words, jam it into (string)word ((word would be the last word the user entered)), then eventually ask the user for more input. The code will never enter while(word =="")


What im looking for is something like a function that asks "Is cin empty?"

if(std::cin.empty)
break;

^^ i wish it was that easy =P ^^
Last edited on
closed account (NUj6URfi)
null?
It will never reach that point, because once the cin IS null... then it will goto asking the user for input

Look back at the code
Lets say the user types in "Hello There Friend
1
2
3
4
5
6
7
8
9
while (i == 1) {
 cin >> word;  //First word == Hello //Then word ==There // word == Friend
                       //Still word  == Friend //  cin asks the user for input
 while(word == "") { //word will never == ""
   cin >> word;
   cout << word;
   i++;
  }
}

word will NEVER == null unless i tell it to
Last edited on
closed account (NUj6URfi)
That's not my current code though.
Any other ideas guys? still stumped with this little problem.

That's not my current code though.

Thanks for your attempt toad, the snippit was copy-pasted.
It wouldnt matter if i copy-pasted it ALL, the method still has no effect toward my problem.
What im looking for is something like a function that asks "Is cin empty?"

1
2
if(std::cin.empty)
break;


An istream is not a container. There is no such function. What you are asking is not possible with console streams.

However, another way to write the second piece of code in your original post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::istringstream is(line);

    std::string token;
    while (is >> token)
        std::cout << token << '\n';
}
Topic archived. No new replies allowed.