So I have run into this problem a few times in class lately. I have found one way around it but wanted to see if anyone could provide a little better information.
When I go to use cin >> temp; where char = temp; Anything that I input with a space will not show up if I am to output the temp. Here are my examples of this issue:
This first we are supposed to read an input char by char and add it to a string.
//Taylor Sanchez
//CSCI202
//Feb 22, 2011
#include <string>
#include <iostream>
usingnamespace std;
int main()
{
char temp;
string thread;
cout<< "Type a ~ to exit";
cin>> temp;
while (temp!='~')
{ if (temp==' ') //wanted to check if cin>>temp even reads the space ' '
{
thread+= "hello"; //it never outputs hello even if a space appears
}
thread+=temp;
cin >> temp;
}
cout<<endl<<"thread= " <<thread <<endl;
return temp;
}
With the input being: "The quick brown fox jumps over the lazy dog."
The output is like this:
taylor@taylor-Vostro-220-Series:~/csci202/6lab$ g++ char2string.cpp -o char
./taylor@taylor-Vostro-220-Series:~/csci202/6lab$ ./char
Type a ~ to exitThe quick brown fox jumps over the lazy dog.~
thread= Thequickbrownfoxjumpsoverthelazydog.
I'm mostly just curious what the issue is and if there is a way to resolve it. My lab instructor didn't have an answer for me, but they are just a student who is farther along in their degree.