[英]How do I print a string from kernel buffer? Am I using copy_from_user() right?
[英]How do I read and print the n string that I am reading from the user in this mini billing system project in C?
因此,我有一個來自大學的小型項目,我必須在C中制作一個簡單的計費系統。
現在,從代碼開始,這就是我要輸入的數據的方式。
printf("How many items do you have in your basket?:\n");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the name of the item:\n");
scanf("%s",&item_name[i]);
printf("Enter the MRP printed on the box:\n");
scanf("%f",&mrp[i]);
printf("Enter the quantity:\n");
scanf("%f",&qty[i]);
}
現在,假設某人在他的購物籃中有2件物品,因此,他將輸入兩個名稱。 最后,如何打印兩個名稱?
for (i=0;i<n;i++)
{
printf("%s\n",item_name[i]);
printf("Quantity:%.2f\n",qty[i] );
printf("MRP:%.2f\n",mrp[i] );
}
上面打印的代碼僅打印字符串的第一個字母。
它可以正確打印,這只是您的scanf
對char數組是錯誤的。 您不必傳遞指針的地址。 與整數或雙精度數相反,傳遞指針就足夠了。
scanf("%s",&item_name[i]);
應該
scanf("%s",item_name[i]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.