[英]about 'strcpy' function using
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,則他們沒有折扣示例例如,當我打印notdiscount時,我有cat0 bee1 ear2 eye0:cat0,eye0折扣:bee1,ear2
我通過isdigit檢查digit,並通過strcpy復制用戶名時遇到問題。 感謝幫助 。 :]
將2D數組用作:
char discountUser[N][32];
char notDiscountUser[N][32];
其中N
是最大用戶數,您可以將其#define
為某個值。
您正在嘗試做的是:
char discountUser[32];
這是一個字符串,如果使用char discountUser[i]
,則是在字符串discountUser
索引i
處引用char
(單個字符,不是字符串)。
現在, strcpy
希望將字符串作為兩個參數的輸入,因此您不能將discountuser[i]
作為其輸入。
當您如上所述聲明2D數組時, dicountuser[i]
discountuser[i]
將引用一個string
(實際上dicountuser[i]
將用作char
指針),因此現在您可以將其作為參數傳遞給strcpy
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.