• Advertisement
Sign in to follow this  

Error trying two compare to values. (C/C++)

This topic is 3698 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to look at a char array and see if an individual char is equal to 1. Heres my code.
#include <stdio.h>
#include <stdlib.h>
#include <string>

/*****************************************************/
int bit_to_ascii(const char core[ ], char data[ ]) {
/*****************************************************/

int i = 0; /* var for loop */

int len = strlen(core); /* calc lenth of core */

for (i; i < len ; i++){

  if (core  == "1"){

    printf ("%c", core );

  }
}

system ("pause");


}/*****************************************************/

int main()
{
  int x;
  const char stuff[48] = "r5 0 #! 1 1 gP 0 f66-:] 0 [ } 1 v4t^ 0 1 n>?:77";
  char things[2] = " ";

  bit_to_ascii(stuff,things);

  return 0;
}

I get the error ANSI C++ forbids comparison between pointer and integer. So then I change
if (core  == "1"){

to
if (core  == 1){

and it compiles error free, but when I execute it, none of the 1's show up. Where am I going wrong. Also try to leave thing specific to C++ out as it's kind of a long story.

Share this post


Link to post
Share on other sites
Advertisement
if (core == '1') - Compares a char with a char
if (core == "1") - Compares a char to a char*
if (core == 1) - Compares a char with an integer (i.e. the character associated with the ASCII code of 1 - the 'Start of Heading' control character)

Share this post


Link to post
Share on other sites
Since you're using C++, you may as well just use a std::string instead, unless you have special need for a char*.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement