Question

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++ ;
}
}

I need to separate user who have discountcode or not by enter 3charofname and 1 digit eg. cat2 if digit more than 0 so, the user have discount if digit is 0 so, they not have discount example i have cat0 bee1 ear2 eye0 when i print notdiscount : cat0 , eye0 discount : bee1 , ear2

i check digit by isdigit and i have problem with copy username by strcpy . Thanks for help . :]

Was it helpful?

Solution

Use 2D arrays as:

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

where N is max number of users, you can #define it to some value.

What you are trying to do is:

char discountUser[32];

This is a string, if you use char discountUser[i] you are referring to the char (A single character, not a string) at index i in the string discountUser.

Now, strcpy expects a string as input to both of its arguments, therefore you cannot pass discountuser[i] as its input.

When you declare a 2D array as I told above, discountuser[i] will refer to a string (actually dicountuser[i] will act as char pointer), so now you are allowed to pass it as argument to strcpy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top