0

Can someone give me a hint as to why this isn't printing the array? I don't know what is wrong with my print function. I want to make certain it's working correctly before I add in the other parts to my code. I'm guessing I have not setup the array correctly & that's why nothing is printing out.

#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);
}
}
5
  • 2
    stuID[NUMSTU] = studentID; has undefined behavior. You're writing to an out-of-bounds element. Commented May 7, 2017 at 16:13
  • 1
    printf("%d", stuID); has undefined behavior. printf %d takes an int, but you're passing an int *. Commented May 7, 2017 at 16:14
  • You have two variables called totStu. Only one of them has a non-zero value. Commented May 7, 2017 at 16:15
  • thanks - i made those corrections. Commented May 7, 2017 at 16:33
  • Formatting/indentation................... Commented May 7, 2017 at 19:02

1 Answer 1

1

You have more than one mistake here. First, you should get an out of boundaries exception because of this line (in higher level programming languages):

stuId[NUMSTU] = studentId;

stuId is an array that has an initial length of NUMSTU. You're trying to access it in NUMSTU even though it has accessible slots only between 0 and (NUMSTU-1).

You probably wanted to do this thing:

stuId[stuCount] = studentId;

and in the print, you're only printing the location of the array again and again. Instead of:

print("%d", stuId);

do:

print("%d", stuId[stuCount]);

Oh yeah, and a third mistake, here:

int stuCount = 0;
int totStu = 0;

stuCount and totStu were already declared as global variables (meaning that every function has access to them). What you're doing is defining new variables that have the same name, but cannot be accessed by other functions. So you should decide whether they are global, or local. Anyway, you should change it to:

stuCount = 0;
totStu = 0;

Now it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

This is C; you don't get 'out of bounds' exceptions — you get whatever you get which, which might be an exception or it could be anything else that the compiler decides on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.