0
char input[32];
char name[32];
char discountUser[32];//not sure to using about arrays.
char notDiscountUser[32];//not sure to using about arrays.
int i,j;
int len;
fgets(input,32,stdin);
sscanf(input,"%s",name);
len = strlen(name);
for(i=0,j=0; i < len; i++)
{
if(isdigit(name[i]))
{
    digits[j] = name[i];

    if (digits[j] > 48)
    {
        strcpy(discountUser[i],name); //i want to stored the name at i index
        printf("you have discount code\n");
    }
    else if (digits[j] <= 48)
    {
        strcpy(notDiscountUser[i],name); //i want to stored the name at i index
        printf("you don't have discount code\n");
    }
    j++ ;
}
}

我需要通过输入 3charofname 和 1 位数字来区分是否有折扣码的用户。cat2 如果数字大于 0 所以,如果数字是 0,用户有折扣,所以他们没有折扣示例我有 cat0 bee1 ear2 eye0 当我打印 notdiscount : cat0 , eye0 折扣: bee1 , ear2

我通过 isdigit 检查数字,我在通过 strcpy 复制用户名时遇到问题。感谢帮助 。:]

4

1 回答 1

0

将二维数组用作:

char discountUser[N][32];  
char notDiscountUser[N][32];  

N最大用户数在哪里,您可以#define将其设置为某个值。

你想要做的是:

char discountUser[32];

这是一个字符串,如果您使用char discountUser[i],则指的是 string 中 index 处的( charA single character, not a string) 。idiscountUser

现在,strcpy期望一个字符串作为其两个参数的输入,因此您不能将discountuser[i]其作为输入传递。

当你像我上面所说的那样声明一个二维数组时,discountuser[i]将引用一个string(实际上dicountuser[i]将充当char指针),所以现在你可以将它作为参数传递给strcpy.

于 2013-09-26T08:02:28.777 回答