I with the below code:
#include <stdio.h>
int main() {
int numOfClasses;
do{
printf("How many classes do you have?\t");
scanf("%d", &numOfClasses);
}while(numOfClasses < 1);
int count = numOfClasses;
char nameOfClass[30];
int numOfGrades[30];
printf("Which classes do you have, how many grades in each class?\n");
for(int i = 0; i < count; i++){
scanf("%s %d", &nameOfClass[i], &numOfGrades[i]);
}
for(int i = 0; i < count; i++){
printf("%s : %d\n", nameOfClass[i], numOfGrades[i]);
}
return 0;
}
I want this program to to ask the user to input a class and the number of grades in that class. Later I will try to input each individual grade in that class, but for now I want to solve this.
When I run this I get this error message:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s : %d\n", nameOfClass[i], numOfGrades[i]);
^
答案 0 :(得分:1)
The variable char nameOfClass[30]
should be two dimensional array as char nameOfClass[30][30]
.
As you are reading the name of classes. So, I changed it to two dimensional array. And you are reading string array using the statement:
scanf("%s %d", &nameOfClass[i], &numOfGrades[i]);
If you do not use two dimensional array then, your nameOfClass
variable will point only last class name which is incorrect.
int count = numOfClasses;
char nameOfClass[30][30];
int numOfGrades[30];
printf("Which classes do you have, how many grades in each class?\n");
for(int i = 0; i < count; i++){
scanf("%s %d", nameOfClass[i], &numOfGrades[i]);
}
for(int i = 0; i < count; i++){
printf("%s : %d\n", nameOfClass[i], numOfGrades[i]);
}