Hi guys I'm working on a small portion of a larger program here that will accept lines of text and based on the first word in each line needs to conditionally branch...I am using the tolower() function to make the words lowercase and then I test them in an if statement...The problem is that it's not branching on any of the conditional statements...for example I ran the file with the following line of text:
"ThRoW garbage 45 56"
It should have output "throw" to the screen but it did not...at the very end when I output the formatted line of code, it outputs correctly:
"throw 45 56"
I ran the program through gdb and when I got to line 24 I did an info locals to check the values of the variables. It showed:
order = 0x22cbb0 "throw"
I stepped and it went straight to line 26...
I don't understand why the if statement isn't catching it...Any help you could give me would be greatly appreciated...Thanks!

#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
int main(){
 char command[256];
 char *order=0;
 char *token=0;
 //char c;
 int i=0;
 int xcoord, ycoord;
 cout << "Enter the command: ";
 while(cin.getline(command, 256)){
  token = strtok(command," ");
  order = token;
  while(order[i]){
   //c=order[i];
   //order[i] = static_cast<char>(tolower(c));
   order[i] = tolower(order[i]);
   i++;
  }
  if(order == "add")
   cout << order << endl;
  if(order == "throw")
   cout << order << endl;
  if(order == "remove")
   cout << order << endl;
  token = strtok(NULL," ");
  token = strtok(NULL," ");
  xcoord = atoi(token);
  token = strtok(NULL," ");
  ycoord = atoi(token);
  cout << order << " " << xcoord << " " << ycoord << endl;
  i=0;
 }
 return 0;
}

Recommended Answers

All 5 Replies

C-style strings aren't compared with ==, you have to use strcmp or equivalent. What you're doing now is comparing two addresses that are unlikely to be the same, not the contents of two strings.

Ok, here's my updated code...

#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main(){
 char command[256];
 char *order=0;
 char *token=0;
 char Throw[] = "throw";
 char add[] = "add";
 char remove[] = "remove";
 char c;
 int i=0;
 int xcoord, ycoord;
 cout << "Enter the command: ";
 while(cin.getline(command, 256)){
  token = strtok(command," ");
  order = token;
  while(order[i]){
   //c=order[i];
   //order[i] = static_cast<char>(tolower(c));
   order[i] = tolower(order[i]);
   //order[i] = static_cast<char>(order[i]);
   i++;
  }
  if(strcmp(order,add))
   cout << order << endl;
  if(strcmp(order,Throw))
   cout << order << endl;
  if(strcmp(order,remove))
   cout << order << endl;
  token = strtok(NULL," ");
  token = strtok(NULL," ");
  xcoord = atoi(token);
  token = strtok(NULL," ");
  ycoord = atoi(token);
  cout << order << " " << xcoord << " " << ycoord << endl;
  i=0;
 }
 return 0;
}

I ran the code with the following line of input: ThRoW garbage 45 56 and it gave me this:

throw
throw
throw 45 56

when it should have given me this:

throw
throw 45 56

stepping through the program in gdb I see that it is branching on each of the other if statements than the on it should be (ie with the above input it would branch on the add and remove test, but not the throw test)...Checking info locals it shows that the value of order is in fact "throw"...I don't understand why this is happening...any ideas? thanks...

strcmp() returns 0 when the strings are equal. So you would in fact have to write if(strcmp(order,add) == 0) if you wanted to test whether 'order' and 'add' are the same.

oh crap!! I completely forgot that!! Thanks!
::slinking ashamedly away::

Member Avatar for iamthwee

You should really be using c++ functions to do that instead of c functions, unless you are constrained by the folly of some school education establishments.

Personal preference isn't really an excuse :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.