【发布时间】:2017-05-07 16:09:09
【问题描述】:
有人可以告诉我为什么这不打印数组吗?我不知道我的打印功能有什么问题。在将其他部分添加到我的代码之前,我想确保它正常工作。我猜我没有正确设置数组,这就是为什么没有打印出来。
#define NUMSTU 50
#include <stdio.h>
//function prototype
void printdata();
//Global variables
int stuID[NUMSTU];
int stuCount;
int totStu;
int main ()
{
int stuCount = 0;
int totStu = 0;
int studentID;
//Prompt user for number of student's in class
printf("Please enter number of student's in class:");
scanf ("%d", &totStu);
for (stuCount = 0; stuCount <totStu; stuCount++)
{
//Prompt user for student ID number
printf("\n Please enter student's ID number:");
scanf("%d", &studentID);
stuID[NUMSTU] = studentID;
}
//Call Function to print data
printdata();
return 0;
}//end main
void printdata(){
//This function will display collected data
//Input: Globals stuID[NUMSTU]
//Output: none
//Display column headers
printf("\n\n stuID\n");
//loop and display student ID numbers
for (stuCount = 0; stuCount <totStu; stuCount++){
printf("%d", stuID);
}
}
【问题讨论】:
-
stuID[NUMSTU] = studentID;具有未定义的行为。您正在写入越界元素。 -
printf("%d", stuID);具有未定义的行为。printf%d接受int,但您传递的是int *。 -
你有两个变量叫做
totStu。其中只有一个具有非零值。 -
谢谢 - 我做了这些更正。
-
格式化/缩进......