不兼容的指针错误,财务代码计算器

时间:2015-04-25 08:21:22

标签: c algorithm pointers

我正在尝试创建财务代码计算器算法。

以下是代码:

    #include<stdio.h>
#include<string.h>

int main()
{
    int Day,Month,Year,i;
    char Mo;
    char Name[1][30];
    char Surname[1][30];
    char A,B,C,D,E,H,L,M,P,R,S,T;

    printf("Insert your birthday day: ");
    scanf("%d",&Day);

    printf("Insert your birthday month: ");
    scanf("%d",&Month);

    printf("Insert your birthday year (last two numbers): ");
    scanf("%d",&Year);

    /*Month calculator*/

    switch(Month)
    {
        case 1:
            Mo="A";
            break;
        case 2:
            Mo="B";
            break;
        case 3:
            Mo="C";
            break;
        case 4:
            Mo="D";
            break;
        case 5:
            Mo="E";
            break;
        case 6:
            Mo="H";
            break;
        case 7:
            Mo="L";
            break;
        case 8:
            Mo="M";
            break;
        case 9:
            Mo="P";
            break;
        case 10:
            Mo="R";
            break;
        case 11:
            Mo="S";
            break;
        case 12:
            Mo="T";
            break;
    }

printf("Your fiscal code is: %d%c%d",Year,Mo,Day);
}

在开关的每种情况下,我都会收到相同的错误:指向整数转换的不兼容指针分配给&#39; char&#39;来自&#39; char [2]&#39;。

错误在哪里?

感谢所有人!

1 个答案:

答案 0 :(得分:1)

您正尝试将char分配给char*Mochar,双引号(")包围的字符串为char*,以\0结尾。使用单引号(')表示字符。

更改

switch(Month)
{
    case 1:
        Mo="A";
        break;
    case 2:
        Mo="B";
        break;
    case 3:
        Mo="C";
        break;
    case 4:
        Mo="D";
        break;
    case 5:
        Mo="E";
        break;
    case 6:
        Mo="H";
        break;
    case 7:
        Mo="L";
        break;
    case 8:
        Mo="M";
        break;
    case 9:
        Mo="P";
        break;
    case 10:
        Mo="R";
        break;
    case 11:
        Mo="S";
        break;
    case 12:
        Mo="T";
        break;
}

switch(Month)
{
    case 1:
        Mo='A';
        break;
    case 2:
        Mo='B';
        break;
    case 3:
        Mo='C';
        break;
    case 4:
        Mo='D';
        break;
    case 5:
        Mo='E';
        break;
    case 6:
        Mo='H';
        break;
    case 7:
        Mo='L';
        break;
    case 8:
        Mo='M';
        break;
    case 9:
        Mo='P';
        break;
    case 10:
        Mo='R';
        break;
    case 11:
        Mo='S';
        break;
    case 12:
        Mo='T';
        //break; Not needed
}