首页 > 解决方案 > C 编程。如何在结构数组中传递字符串

问题描述

非常感谢您的时间和精力

/* 在下面提供的空白处编写一个 C 程序,要求用户输入圣莫尼卡一打待售住宅物业的地址和价格,并按地址、价格和类型(即公寓、单身-家庭住宅或土地)在一系列结构中。然后它会打印那些低于 100 万美元的房产的信息。然后使用上面显示的嵌入图标,插入演示代码执行的屏幕截图。*/

请指教。即,当我输入地址 1439 Cedar Ave 时,它​​会将Cedar Ave作为单独的提示,而它实际上是地址和第一个提示的一部分。

#include <string.h>

//gotta make the structure first:
//declare properties as struct (short for structure) so that 
//the address, type and price all go under properties
struct property {
    char address[100];
    char type[100];
    double price;
};

int main() {
int n,i;//declare i
struct property p[2];
//ask how many properties to add, this'll determine the amount of iterations
    printf("Enter how many properties you want to add: ");
    scanf("%d",&n);

//save previous input in &n and iterates/asks that many times about property info
for (i = 0; i < n; i++) {
    
    //name + address
    printf("Property #%d\n",i + 1);
    printf("Address: ");
    scanf("%s", &p[i].address); //input goes into structure
    printf("\n");
    //property type
    printf("Enter type: \n");
    scanf("%s", &p[i].type); //input goes into structure
    printf("\n");
    //price
    printf("Enter price: \n");
    scanf("%lf", &p[i].price); //input goes into structure
    printf("\n");
    //makes a space to seperate different properties
    printf("\n");
    }
    
    printf("\nResidential properties in Santa Monica for sale under $1M: \n");
    printf("\n");

    //this for loop filters out the properties by price
    //excludes properties worth MORE than $1M
    //prints rest of properties UNDER $1M
    
    for (i = 0; i <n; i++) {
        if (p[i].price < 1000000) {
            printf("Property %d:\n", i + 1);
            printf("Address: %s\n", p[i].address);
            printf("Type: %s\n", p[i].type);
            printf("Price: %lf\n", p[i].price);
            printf("\n");
        }
    }
}```

标签: cstringscanf

解决方案


推荐阅读