【发布时间】:2014-11-03 05:14:18
【问题描述】:
我正在尝试用 C 编写一个小程序,它将存储用户输入的学生数量的名字、姓氏和年级。到目前为止,我最大的问题是如何让每个学生的姓名和成绩打印在新的一行中。使用字符串运算符时,我得到一个错误,而使用字符运算符时,我只能得到第一个字母和等级。我将如何让名称完全打印?感谢您提前提供的所有帮助。
#include <stdio.h>
#include <stdlib.h>
int main(){
int classsize,i;
printf("Please indicate number of records you want to enter (min 5, max 15):\n");
scanf("%d", &classsize);
char *first, *last;
double *mark;
first=(char*)malloc(classsize*sizeof(char));
last=(char*)malloc(classsize*sizeof(char));
mark=(double*)malloc(classsize*sizeof(double));
printf("Please input records of students (enter a new line after each record), with following format 1. first name 2. last name 3. score.\n");
for (i=0; i<classsize; i++) {
scanf("%s", &first[i]);
scanf("%s", &last[i]);
scanf("%lf", &mark[i]);
}
for (i=0; i<classsize; i++) {
printf("%s, %s has a %lf\n", *(first+i), *(last+i), *(mark+i));
}
}
【问题讨论】:
标签: c arrays pointers dynamic-memory-allocation