【发布时间】:2021-12-12 21:18:15
【问题描述】:
所以我正在尝试创建一个将数据读入文件的程序。但在此之前,我需要将数据存储到一个结构中。如何将字符串存储在结构中?
#include <stdio.h>
#define MAX 100
int count;
struct cg {
float price;
char singer, song;
int release;
} hold[100];
int main() {
while (1) {
printf("Name of band of Singer: ");
scanf_s("%s,", &hold[count].singer);
printf("Name of Song: ");
scanf_s("%c", &hold[count].song);
printf("Price: ");
scanf_s("%f", &hold[count].price);
printf("Year of Release: ");
scanf_s("%d", &hold[count].release);
count++;
printf("\n");
}
}
【问题讨论】:
-
代替
char singer, song;使用char singer[100], song[200];代表最多99 个字符的歌手和最多199 个字符的歌曲。 -
或者考虑使用指向动态分配的字符数组的指针。