1

当我运行它时,partName char 数组会打印垃圾字符。知道为什么吗?谢谢!

typedef struct {
char partName[30]; 
int partNumber; 
float price; 
int stock; 
int reorder;
} inventory;
struct address{
char streetAddress[25]; 
char city[20];
char state[3]; 
char zipCode[6];
};
void GetData(){

inventory order;
address orderAddress;
printf("Enter the part number (Database Includes orders 0001 - 0005):  ");

scanf("%d", &order.partNumber);

if(order.partNumber == 0001){
order.partName == "Pokeball";
order.price == 200;
order.stock == 8263;
order.reorder == 888273;

orderAddress.streetAddress == "21 Oak St.";
orderAddress.city == "Kanto Town";
orderAddress.state == "IA";
orderAddress.zipCode == "28832";

printf("Part was: %s ", order.partName);
//printf("Order was sent to: \n %s\n %s\n %s\n %s\n", orderAddress.streetAddress,         orderAddress.city, orderAddress.state, orderAddress.zipCode);
 }

它正在打印一些随机字符,我确信这是某个地方的简单错误。

多谢你们!

4

1 回答 1

2

以下不是作业。它们是比较

order.partName == "Pokeball";
order.price == 200;
order.stock == 8263;
order.reorder == 888273;

orderAddress.streetAddress == "21 Oak St.";
orderAddress.city == "Kanto Town";
orderAddress.state == "IA";
orderAddress.zipCode == "28832";

要分配给整数,请写

order.price = 200;

要分配给 C 字符串,请使用strcpy()et al。

于 2013-04-12T05:49:46.207 回答