我正在尝试用C创建一个发票程序,但是它没有按照我打算的那样做。
这是我的代码:
#include <stdio.h>
#include <string.h>
int main()
{
int choice;
char item_name[1000][3][20];
float item_price[1000][3];
float quantity[1000][3];
int k;
int j;
k=0;
j=0;
for (k=0;k<1000;k++)
{
printf ("\n");
printf ("Enter the item name: ");
scanf ("%s", item_name[k][j]);
printf ("\n");
printf ("Enter the item price: ");
scanf ("%f", &item_price[k][j]);
printf ("\n");
printf ("Enter the item quantity: ");
scanf ("%f", &quantity[k][j]);
printf ("\n");
printf ("| Quantity | Item Name | Item Price |");
printf ("\n");
for (j=0;j<1000;j++)
{
if (quantity[k][j]==0) break;
printf (" %.1f", quantity[k][j]);
printf (" %s", item_name[k][j]);
printf (" %.2f", item_price[k][j]);
printf ("\n\n");
}
printf (" Would you like to enter another item? Enter '1' for yes and '2' for no: ");
scanf ("%d", &choice);
printf ("\n");
if (choice == 2) break;
if (k>999) break;
}
return 0;
}
这是我想要的输出:
Enter item name: Chips
Enter item price: 0.70
Enter item quantity: 3
| Quantity | Item Name | Item Price |
3 Chips 0.70
Would you like to enter another item? Enter '1' for yes and '2' for no: 1
Enter item name: Drinks
Enter item price: 1.00
Enter item quantity: 3
| Quantity | Item Name | Item Price |
3 Chips 0.70
3 Drinks 1.00
Would you like to enter another item? Enter '1' for yes and '2' for no: 2
最佳答案
不需要[3]。
在for k循环中,仅引用[k]
在for j循环中,只需引用[j]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int choice;
char item_name[1000][20];
float item_price[1000];
float quantity[1000];
int k;
int j;
k=0;
j=0;
for (k=0;k<1000;k++)
{
printf ("\n");
printf ("Enter the item name: ");
scanf ("%19s", item_name[k]);
printf ("\n");
printf ("Enter the item price: ");
scanf ("%f", &item_price[k]);
printf ("\n");
printf ("Enter the item quantity: ");
scanf ("%f", &quantity[k]);
printf ("\n");
printf ("| Quantity | Item Name | Item Price |");
printf ("\n");
for (j=0;j<=k;j++)
{
printf (" %.1f", quantity[j]);
printf (" %s", item_name[j]);
printf (" %.2f", item_price[j]);
printf ("\n\n");
}
printf (" Would you like to enter another item? Enter '1' for yes and '2' for no: ");
scanf ("%d", &choice);
printf ("\n");
if (choice == 2) break;
if (k>999) break;
}
return 0;
}
关于c - 在c中累积数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28635037/