Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I have problems in a simple function that returns a string:

C++
if (format == style1)
    (value >= 0) ? str.Append(_T(">")) : str.Append(_T("<"));


I want to include another if condition without appending any symbols.ie;

C++
if (format == style2)
 (value >= 0) ? str.Append(_T(" ")) : str.Append(_T(" ")); 


But this does not work. Is there any other way to do this?


Edit: Added MFC tag as CString's an MFC class.
Posted
Updated 23-Apr-12 6:26am
v2
Comments
nv3 23-Apr-12 11:24am    
What exactly does not work? I guess, you will have to show us the entire function and tell us what you expect it do.
Sumal.V 23-Apr-12 11:56am    
Well It is a very complicated code. Before the above shown code : There is :

String1.Format(_T("%%02d°%%02d'%%1.5lf\""), 3);
str.Format( String1, deg, min, sec);

This string is displayed in an edit box in the software. If the format type = style1, Then append '>' or '<'. If the format type = style2, I don't want to append any letter.
CPallini 23-Apr-12 12:44pm    
Why don't you:
1. post the exact code
2. state clearly what is the expected behaviour
3. report the observed behaviour
?
Sumal.V 24-Apr-12 3:55am    
if (Fmt == Latitude)
(val >= 0) ? str.Append(_T("N")) : str.Append(_T("S"));

else if (m_dirFmt == Longitude)
(val >= 0) ? str.Append(_T("E")) : str.Append(_T("W"));

else if (m_dirFmt == Bearing)
//Here I want to append nothing as bearing is just a degree value
Sumal.V 24-Apr-12 4:19am    
All I want to know is can I append a blank space using CString.append?

1 solution

As every branch looks like it uses CString::Append why not simplify your code by doing something like:

C++
if (format == style1) str.Append( (value >= 0 ) ? "<" : ">" ); 

Or even go the conditional free route and use a lookup table:
C++
const *char appendable_strings[][] = { { "<", ">" }, { " ", " " } };

str.Append( appendable_strings[format][value < 0] );

and map format and style onto 0 based integers. The code will be smaller, probably faster and a lot easier to read. And stick it in a function so you've got a better idea what the compiler is whinging about when you do something incorrectly.

Cheers,

Ash

PS: Not sure the array indices are 'round the right way - I don't use arrays that often.

PPS: As another tip forget CString - std::string and std::ostringstream will perform all your formatting needs.

Edit: Caught Native Language Fail Exception

Edit II, the Horror Continues: Slung a conditional in as per Chuck's suggestion below and the fact that I'd missed the original poster wanting a space character appended for style2.
 
Share this answer
 
v3
Comments
Chuck O'Toole 23-Apr-12 18:44pm    
Basic logic flaw there. His condition is value >= 0. You can't use val (value) as the index because it can be *any* number, e.g. 999999 or -326 and that would be an unacceptable index for you "appendable_strings" array.
Chuck O'Toole 23-Apr-12 18:47pm    
Another nit, assuming you did
int val = (value >= 0)
to change the bool to an int, you'd have to reverse the logic (0 = false = '>', 1 = true = '>') or reverse the conditional (value < 0). Then you can use the shorter code.
Just nitpicking, assuming the OP wanted to use your shortcut.
Aescleal 23-Apr-12 21:07pm    
Thanks, modified the indexing to take your points into account.
Jochen Arndt 24-Apr-12 3:44am    
It can be more simplified (and optimized) by using AppendChar().
Aescleal 24-Apr-12 5:22am    
I got the impression from other comments he made that he wanted to be able to add arbitrary strings. If all he wants are characters then your way sounds fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900