Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Strange behavior in a standard conditional loop, help wanted! Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Azu

March 08, 2005, 02:53 PM

Hi,

I am a relative n00b at C programming. I have however spent considerable time programming in basic before. I found it was time to learn a lower level language.

Now I have a very strange problem concerning a "if - else if - else" loop in a C program I am doing. Somehow, the conditional loop decides to skip the "if" and "else if" stage even though the condition is true. It goes straight to the "else" stage, thus outputting "no help for command. check syntax." to the console, even if "help" or "quit" is used as input via "cin >> cmd". cmd is a 64 byte char array to store input from the console.

I have pondered the web looking for answers and looked up all the C books I could find... It's just weird.

it's located in "int main" and it uses a function declared as:
void sysmsg(char sysmsg_msgtext[], int sysmsg_context);
which outputs a text to the console differently based on the context (error, warning, standard or tip)

  1.  
  2.         while(cont_loop == 1)
  3.         {
  4.                 cout << ">";
  5.                 cin >> cmd;
  6.  
  7.                 if (cmd == "help") {sysmsg("not implemented.", ERRMSG);}
  8.                 else if (cmd == "quit") {cont_loop = 0;}
  9.                 else {sysmsg("no help for command. check syntax.", ERRMSG);}
  10.         }
  11.  


I really can't see anything wrong with this...

If anybody can spot the problem, I'd appreciate the answer to it.

...release me from this hell...















\

 
bignobody

March 08, 2005, 03:06 PM

the conditional loop decides to skip the "if" and "else if" stage even though the condition is true


Logic doesn't change, so it must mean that the condition is not true. Try changing the if condition to use the standard string comparison function:

  1.  
  2. if ( strcmp(cmd,"help") )
  3. {
  4.      sysmsg("not implemented.", ERRMSG);
  5. }
  6. ...
  7.  


Hope this helps.

-bignobody

 
bignobody

March 08, 2005, 03:12 PM

Oops. Been a while since I've done any C. strcmp returns 0 if the strings are equal:

  1.  
  2. if (strcmp(cmd,"help") == 0)
  3. {
  4.     sysmsg("not implemented.", ERRMSG);}
  5. }
  6. ...
  7.  

 
Azu

March 08, 2005, 03:17 PM

thank you.

after changing it to
. if (strcmp(cmd,"help") == 0) {}
it worked!

thanks once more!

 
bignobody

March 08, 2005, 03:31 PM

No problem. Glad I could assist your "release from hell" ;)

 
Reedbeta

March 08, 2005, 03:50 PM

For future reference, an expression like cmd == "help" will compare the pointers, not the string values. It'll only compare true if cmd is actually pointing to the location where the string "help" appears in the code - possible, but not very likely =D

 
Nico

March 08, 2005, 05:29 PM

Since you're using c++ anyway, you should use c++ strings, then the comparioson would have worked:

#include
using namespace std;

string blubb;
cin >> blubb;
if(blubb == "whatever")
cout

 
Corre

March 09, 2005, 03:35 AM

But keep in mind that std::string has huge overheads! In my opinion, you're better of using the normal strings and stricmp.

 
Azu

March 09, 2005, 02:31 PM

yeah i'll stick with the strcmp for now. It works fine.
learning bit by byte

tanks!

 
This thread contains 9 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.