我决定在夏天看看目标C,所以我下载了Xcode,我是编程的新手,请同情
我想我会尝试创建一个将字母转换为数字的程序,我确信有更好的方法可以做到这一点,但目前我遇到了问题"变量' A& #39;在其自己的初始化中使用时未初始化"
这是我的代码,朋友们:
#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;
}
}
}
我确信这本书可以写得更好,所以欢迎任何其他批评或建议,指导我指导学习c的正确方向也欢迎!
答案 0 :(得分:1)
初始化char类型变量时,该值必须位于一对撇号
中e.g。 char A ='A';
答案 1 :(得分:1)
如果您要检查实际字符,那么您需要用撇号包围字母(否则您实际上是将A分配给变量A而不是字符&#39; A&#39;正如您可能想要的那样:
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';