2

In one part of my code I should get characters one by one. It's easy but my question is how can I add these characters one by one into a string. Note that I don't know how many characters I will get. It is important that just the characters be copied in string. In the other words, I want to generate words from characters until the character is not equal to ' ' or '\n' . the wrong code that I wrote is:

 char c;
 string str = NULL;
 cin.get(c);
 while (c != " ")
 {
      str += c;
      cin.get(c);
 }
 cout << str ; 

For example, if character c would be 'H' at first and then be 'i', I want to the string str to be "Hi" on cout!

Brian White
  • 8,332
  • 2
  • 43
  • 67
0bijan mortazavi
  • 356
  • 4
  • 13

6 Answers6

4

=! should be !=, and the string " " should be a character ' '.

If you want to check for new-line characters as well as space:

while (c != ' ' && c != '\n')

or perhaps

while (!std::isspace(c))

which will read up to any whitespace character.

You should also check for the end of the stream or other problems:

while (cin.get(c) && !std::isspace(c)) {
    str += c;
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

If you just want to read until a space or newline:

std::getline( cin, str, ' ' );
George Houpis
  • 1,729
  • 1
  • 9
  • 5
  • Nope. `std::getline` stops either at the default delimiting character (\n) or the one that you provide, not both. For instance, consider this code and input: http://ideone.com/rMLH5G The result is expected to be just "Foo" – Anton Poznyakovskiy Dec 16 '14 at 18:40
1

c++ stringstreams are also useful when building strings

stringstream ss;
while (cin.get(c) && !std::isspace(c)) {
    ss << c;
}
string s = ss.str();
cout << s;
Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
0

The mistake in your code was that you weren't checking for the newline character. Also, it should have been != instead of =!. The second option will actually behave as c = (!(" ")) in your code. Another mistake is that you should be checking for the blank space character ' ' instead of an empty string " ".

Here is the correct code:

char c;
string str = "";
while (true)
{
     cin.get(c);
     cout << "c is " << c << endl;       
         if ((c == ' ') || (c == '\n'))
     break;
     str += c;
}
cout << str << endl ; 

Also, if your requirement is that the I/O be stopped the moment a space character is encountered, refer to this question: C - Reading from stdin as characters are typed

All the solutions proposed here will continue to read input until the new line character is entered because until then your input hasn't been processed, but instead stored in a buffer.

Community
  • 1
  • 1
therainmaker
  • 4,253
  • 1
  • 22
  • 41
  • Obijan mortazavi : Sorry, I had done some stupid mistake in my code. I know its unacceptable, but in my defense its 4 am here. Anyway, this code should solve the problem. However, you still need to press enter as the I/O does not stop after a blank space. But the final returned answer just contains data till the first blank space. – therainmaker Dec 16 '14 at 18:43
  • the problem is not while !!!! is how can I copy or add characters one by in a string? – 0bijan mortazavi Dec 16 '14 at 18:52
  • Obijan mortazavi : the str += c adds a character to the end of string str. In other words, str += c is the same as str = str + c. The get in the loop will keep reading characters one by one. – therainmaker Dec 16 '14 at 18:53
0

If your str starts out empty, this is simple:

string str;

cin >> str; // cin's >> operator already reads until whitespace

cout << str;
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • I have to give characters one by one using char variable! ok? – 0bijan mortazavi Dec 16 '14 at 18:50
  • @0bijanmortazavi I'm not certain why you'd want to do it this way but if that's what you want then [Mike Seymour](http://stackoverflow.com/users/204847/mike-seymour)'s answer is a good one. – Jonathan Mee Dec 16 '14 at 18:56
  • how can i copy or add characters into string? this is my problem now. please help me – 0bijan mortazavi Dec 16 '14 at 18:59
  • @0bijanmortazavi If you are willing to type the whole line at the prompt and then press enter my code will use `cin`'s `operator>>` to put the first word in `str`. If you need to read in character by character, [Mike Seymour](http://stackoverflow.com/users/204847/mike-seymour)'s use of `string`'s `operator+=` will do the copy. – Jonathan Mee Dec 16 '14 at 19:05
-1

If you don't know in advance how many characters it would be, I would declare a std::list, push elements to it and then copy them into a string. This ensures that you don't reallocate the string memory on every addition:

char c;
list<char> buff;
cin.get(c);
while (c != ' ' && c != '\n')
{
    buff.push_back (c);
    cin.get(c);
}
string str (buff.size(), 0);
size_t i = 0;
while (!buff.empty())
{
    str[i] = buff.front();
    buff.pop_front();
    ++i;
}
cout << str ;
Anton Poznyakovskiy
  • 2,109
  • 1
  • 20
  • 38