Nov 1, 2010 at 7:35pm UTC
I assume ms is a std::string
try this:
ms += (t!=' ' )? t: '\0' ; // use 0 as a char constant
Last edited on Nov 1, 2010 at 7:36pm UTC
Nov 1, 2010 at 7:55pm UTC
std::string s keep \0 characters in their buffers, so the solution above may not be optimal
Nov 1, 2010 at 7:57pm UTC
why would you want to use the ternary operator for this? It seems awfully inappropriate.
+1 to kooth's solution
Nov 3, 2010 at 4:02pm UTC
@disch I'm just trying to learn and become more fluent in syntax I have had practically no experience using, and it is stylistically consistent with code I looked up on google. Why wouldn't I want to use a terniary operator for something like this?
Nov 3, 2010 at 4:20pm UTC
The ternary operator is used to replace an if-else. In your case, as kooth's solution showed, you only need
an if() case.
Nov 3, 2010 at 4:53pm UTC
again any reason why the following won't compile?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <string>
#include <iostream>
using namespace std;
string str;
char t;
char null = '' ;
int main()
{
str = "Hello " ;
do {
t = cin.get();
str += (t != ' ' ) ? t : null;
}while (!isspace(t));
str += '!' ;
cout << endl << endl << endl << endl;
cout << str << endl;
}
If I replace char null = '' with
It will compile but it will output:
If all I'm guilty of is trying to write elegant code, sue me.
Edit: jsmith I didn't see your post. I may consider using what kooth showed, but still am curious why don't they allow char null = '';
Last edited on Nov 3, 2010 at 4:55pm UTC
Nov 3, 2010 at 6:03pm UTC
because you must have a character value in a character literal, it would be like doing
int i = 0x ;
It doesn't make sense