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
#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
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
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?"
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++;
}
}
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.