Click here to Skip to main content
15,902,840 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Please help me out. strcmp not working properly it's only return "70" even though 2 strings are diffrent.

pch contain(0.06666, 0.0333, 0.0666 and so on)
test contain ("0.0333")

C++
float ReadFile(float Elastmod[], int NumLines,float Elastmod3[])
{
ifstream myReadFile;
char inputfile[_MAX_FNAME ],
Extension[ _MAX_EXT ] = ".CSV";

_makepath(Infilename, "C:", Dir, inputfile, Extension);

 myReadFile.open("Rdm1.csv");

 char test[] = "0.0333";

 char* pch; 

char* Token; //DYNAMICALLY ASSIGN ARRAY
Token = new char[NumLines];

int i;
 if (myReadFile.is_open()) 
 {
 while (!myReadFile.eof()) 
 {

    myReadFile.ignore(256,'\n');  // to ignore first Line
	
    myReadFile.getline(Token,256,',');


    for(int i=0;i<(NumLines-1);i++)
    {
          pch = strtok (Token,",");

	  myReadFile.getline(Token,256,',');

	  Elastmod[i]= atof(pch) ;



	int result=  (strcmp (pch,test) == 0);

        if (result == 1)

	    Elastmod3[i]= atof("90");

        else if ( result == 0)

	     Elastmod3[i]= atof("70");

         else
             Elastmod3[i]= atof("888");
}
  }

 }

else
{
//cout << "Error opening file";
}

myReadFile.close();

return Elastmod3[NumLines]  ;
}


Thank you.


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 11-Dec-13 6:20am
v3

Have you cout'ed pch in the loop to see what the comparisons are?

Right now you are strcmp-ing pch and test, then comparing it to zero (false) and storing it into the result. This result will always either be zero, or one, so your "if" statement will never fall to the last "else".

It seems like strcmp is returning something other than zero in each loop. I would break it out and debug it, for example:

C++
int result=  (strcmp (pch,test) == 0);
cout << "Result=" << result << endl;
cout << "Pch=" << pch << endl;
cout << "Test=" << test << endl;
cout << "Strcmp=" << (strcmp(pch,test)) << endl;


And see what exactly is being compared, I'm thinking that pch may not be what you think it is.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Dec-13 11:43am    
The advice makes perfect sense, my 5, but I cannot find any sense in OP's sample. In particular, please see my answer. The code hurts my eyes too much to look at in any more. :-)
—SA
Ron Beyer 11-Dec-13 11:45am    
Yeah I stopped reading past the strcmp parts, I see that the rest of it makes little sense, if you want a float, why write atof("80") instead of just 80.0f?
Sergey Alexandrovich Kryukov 11-Dec-13 12:48pm    
Right. Only through a deep confusion...
—SA
There is so much of misuse here, so it's hard to discuss the code seriously. I'll only show what catches my eye at first glance.

First, the documentation of strcmp tells you that you should only compare the result with 0. You need to use '>0', '<0' or '== 0'; practically, only '==0' comparison makes sense in most cases, which indicated that strings are equal or not. I have no idea what is the sense of your comparison with 0 and 1. Please see:
http://www.cplusplus.com/reference/cstring/strcmp/[^].

Now, the expressions like atof("70"), atof("80") is the total absurd. You need to get double from string, but why? This is simply 70 or 80. Please see:
http://www.cplusplus.com/reference/cstdlib/atof/[^].

Also, 70, 80, , 256, "70" or "80" are called "immediate constants". Using them makes code quite unsupportable.

And it's good to explain the purpose of you code, if you want to get some help.

—SA
 
Share this answer
 
Comments
Ron Beyer 11-Dec-13 11:43am    
5'd
Sergey Alexandrovich Kryukov 11-Dec-13 12:47pm    
Thank you, Ron.
—SA
nv3 11-Dec-13 12:32pm    
You beat me by a couple of minutes :-) 5.
Sergey Alexandrovich Kryukov 11-Dec-13 12:47pm    
Thank you.
—SA
As Ron already pointed out, your assumption about what strcmp will return seems to be wrong. Here is what the documentation says:
CSS
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

So your code is probably supposed to be something like:
C++
int result = strcmp (pch, test);
    if (result > 0)
    Elastmod3[i]= 90.0;
    else if (result == 0)
    Elastmod3[i]= 70.0;
    else
        Elastmod3[i]= 888.0;

Note that I have removed the atof calls, which are just nonesense.

And while we are at it: The entire comparison with strcmp does not make much sense. You probably want to compare the float values. And in line
C++
Elastmod[i]= atof(pch) ;

you converted already pch to float. Why don't you just compare that value to another, like:
C++
float test = 0.0333;
...
float pchf = atof (pch);
...
if (pcfh > test)
    Elastmod3[i]= 90.0;
else if (pcfh == test)
Elastmod3[i]= 70.0;
else
    Elastmod3[i]= 888.0;

And one more comment: Test test on equality of two floating point values does in most cases make not much sense.
 
Share this answer
 
v2
Comments
peoria123 11-Dec-13 12:40pm    
Thank you....
nv3 11-Dec-13 12:55pm    
Welcome!
C++
    pch = strtok (Token,",");

myReadFile.getline(Token,256,',');

You have used strtok to get a pointer to a token in the line buffer, then you immediately read another line into the buffer so your pointer is no longer valid.
 
Share this answer
 
Comments
nv3 11-Dec-13 12:38pm    
Good catch! 5. It probably worked just accidentally by the fact that all his lines began at same position.
Richard MacCutchan 11-Dec-13 13:26pm    
Yes, you can see why it 'worked', but did not give the expected answer.
Sergey Alexandrovich Kryukov 11-Dec-13 12:47pm    
Right, a 5.
—SA
Richard MacCutchan 11-Dec-13 13:26pm    
Thanks.

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