Hello!

So , I got a weird problem.In my code the value of the char N[cn] is ignored when I compare it to a constant value.

Here's the code:

Code:

for ( i = 1; i <= 220; i++  ){

// Generate Numbers   
  itoa (i,Number, (V+1));
  sscanf(Number, "%d", &ii);
  stringstream ss;
  ss << setw(NM) << setfill('0') << dec << ii;
  string N = ss.str() ;

   
// DDDDDDDDDDDDD
for ( cn = 0; cn < (NM-1); cn++ ){
   if ( ( N[cn] > 0 ) and ( N[cn+1] > 0 ) ){
   if ((N[cn] < N[cn+1]) ){ 
        C++; 
   }
   }
}


// Stuff

// End
for ( cn = 0 ; cn <= (NM-1); cn++ ){   
  if ( N[cn] == 5 ){ ST++; } 
}
if ( ST == NM ){ i = 220 ; cout << "Stopped \n";}


}

At the "// DDDDDDDDDDDDD" and "// End" parts where I have
if ( ( N[cn] > 0 ) and ( N[cn+1] > 0 ) ){
and
if ( N[cn] == 5 ){ ST++; }
the comparison is totally ignored.Why? How can I fix it?

Thanks!

Recommended Answers

All 4 Replies

Is and approved in C/C++ comparisons?

Is and approved in C/C++ comparisons?

I use it every time and it works. I also tried the alternate spelling "&&" and still the comparisons are ignored.

In the list here:
http://www.cppreference.com/wiki/operator_precedence
both && and "and" are recognized as valid.

The "and" is not really the problem , if I try to compare any char that exits the atoi() function , is ignored. I also tried to change the type with "int(N[cn])" , and still it doesn't work.

I use it every time and it works. I also tried the alternate spelling "&&" and still the comparisons are ignored.

In the list here:
http://www.cppreference.com/wiki/operator_precedence
both && and "and" are recognized as valid.

If you wish your code to be understood, you should use the syntax that the rest of the industry uses. Though and is acceptable in that list, && is the accepted way. In fact I've never seen and used until now, which is why I figured you had compile errors.

For standards experts: Are all those "extra" operator designations actually in the standard? If so, C++ seems to be looking more FORTran-like today.

I compared the character value instead of the ASCII value , that was the problem.

If you wish your code to be understood, you should use the syntax that the rest of the industry uses.

Next time.

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.