-1

I decided I would take a look at objective C over the summer and so I downloaded Xcode, I'm a complete novice to programming so please sympathise

I figured I would try and create a program that would convert letters to numbers and I'm sure there are better methods to do this but at the moment I have the problem "Variable 'A' is uninitialized when used in its own initialization"

here's my code, friends:

#import <Foundation/Foundation.h>
#include <string.h>
#include <stdio.h>

char word[10];
char numbers[10];




int i = 0;
void Convert (char input[10]);

int main(int argc, const char * argv[]) {



    NSLog(@"Enter the word");


    gets(word);
    Convert (word);
    puts(numbers);

    return 0;
}

void Convert (char input[10]) {
    char A = A;
    char B = B;
    char C = C;
    char D = D;
    char E = E;
    char F = F;
    char G = G;
    char H = H;
    char I = I;
    char J = J;
    char K = K;
    char L = L;
    char M = M;
    char N = N;
    char O = O;
    char P = P;
    char Q = Q;
    char R = R;
    char S = S;
    char T = T;
    char U = U;
    char V = V;
    char W = W;
    char X = X;
    char Y = Y;
    char Z = Z;
    for (i=0;word[i] != '\0'; ++i)


    {
        if (word[i] == A || B || C){
            numbers[i] = 2;
        }
        else if (word[i] == D || E || F){
            numbers[i] = 3;
        }
        else if (word[i] == G || H || I){
            numbers[i] = 4;
        }
        else if (word[i] == J || K || L){
            numbers[i] = 5;
        }
        else if (word[i] == M || N || O){
            numbers[i] = 6;
        }
        else if(word[i] == P || Q || R || S){
            numbers[i] = 7;
        }
        else if(word[i] == T || U || V){
            numbers[i] = 8;
        }
        else if(word[i] == W || X || Y || Z){
            numbers[i] = 9;
        }
        else {
            numbers[i] = 0;
        }
    }


}

i'm sure this could have been written a lot better so any other criticism or advice is welcome, pointing me in the right direction for a guide to learn c is also welcome!

Dale Baker
  • 329
  • 1
  • 3
  • 14
  • Best advice, get a good **book** on the "C" language and study it. – zaph Dec 11 '14 at 11:33
  • You can just use the characters directly in single quotes, ex: `if (word[i] == 'A' || 'B' || 'C') {`. But that statement is incorrect. – zaph Dec 11 '14 at 11:35

2 Answers2

1

when you initialize a char type variable, the value needs to be inside a pair of apostrophes

e.g. char A = 'A';

aurelius
  • 3,946
  • 7
  • 40
  • 73
1

If you are checking against actual characters then you need to surround the letters with apostrophes (otherwise you are actually assigning A to the variable A and not the character 'A' as you may have intended:

char A = 'A';
char B = 'B';
char C = 'C';
char D = 'D';
char E = 'E';
char F = 'F';
char G = 'G';
char H = 'H';
char I = 'I';
char J = 'J';
char K = 'K';
char L = 'L';
char M = 'M';
char N = 'N';
char O = 'O';
char P = 'P';
char Q = 'Q';
char R = 'R';
char S = 'S';
char T = 'T';
char U = 'U';
char V = 'V';
char W = 'W';
char X = 'X';
char Y = 'Y';
char Z = 'Z';
Suhail Patel
  • 13,644
  • 2
  • 44
  • 49