Computer Science Canada

char arrays

Author:  Justin_ [ Wed Feb 15, 2006 9:35 pm ]
Post subject:  char arrays

I don't know, I feel like I'm not learning anything. I figured that I'd use streams to and convert to them to chars with the transform function, but then I realized I needed a unary function that would deal with the chars.

So then I started to write a test version of just such a function when I ran into some complications that I don't understand and I feel stupid and retarded for not understanding.

Here's the code.

c++:

#include <iostream>

using namespace std;

char toBinary(char hexNum)
{
   char charArray[16];
   for (int i = 0; i < 16; i++)
   {
      if (i < 10)
         charArray[i] = i;
      else
         charArray[i] = (char) i + 87;
      cout << charArray[i];
   }
   return "f";
}

int main()
{
   toBinary("f")
   return 0;
}

Author:  wtd [ Wed Feb 15, 2006 10:08 pm ]
Post subject: 

One immediate problem jumps out at me. When you call toBinary, you're providing a const char*, but your function takes a char.

Author:  Justin_ [ Wed Feb 15, 2006 11:10 pm ]
Post subject: 

Yeah I don't get how it figures its a constant char. If i declare it a char, and then pass it a char, even though its a constant char shouldn't it be treated as a char?

But thanks though, I didn't realize that was what the compiler was talking about when it said error converting from const char to char.

Author:  wtd [ Wed Feb 15, 2006 11:12 pm ]
Post subject: 

Chars are delineated with single quotes. String literals are surrounded by double quotes.

Author:  Justin_ [ Wed Feb 15, 2006 11:36 pm ]
Post subject: 

Oh! and i realized my other problem was not doubling up on the equal signs. *smacks himself*

But I'm not even going to bother doing it this way, I'll just stick to strings since it will be much easier.


: