Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an assignment and I'm still a beginner.
I have a problem on being able to input a single student data and displaying it using only functions (function calls, prototype and definitions) in the main function.
I tried doing it but what goes on to the screen during execution has a word null in where the word is supposed to be.
Please help.

What I have tried:

Doing the functions and even foregoing the functions and doing everything in the main function but still there is a null during execution.
C
/* The program will have 2 versions of the function used to   *
 * input student records: getStudentV1() and getStudentV2().  *
 *                                                            * 
 * Function displayStud() will display information of 1       *
 * student record in 1 horizontal line.                       *
 * Function displayHeader() is called before displayStud() is *
 * called.                                                    *
 **************************************************************/

#include <stdio.h>
#include <string.h>

typedef struct {
	char fname[24];     /* firstname */
	char lname[16];     /* lastname  */ 
	char mi;            /* middle initial */ 
}nametype;

typedef struct {
	nametype name;
	unsigned int ID;
	char course[8];
	int yrLevel;
}studtype;

studtype getStudentV1();        
void getStudentV2(studtype *S); 
void displayHeader();          
void displayStud(studtype S);     

int main()
{

  getStudentV1();
  displayHeader();
  displayStud(student);

  return 0;
}


void displayHeader()
{
  printf("\n%-10s", "ID");   
}

void getStudentV1(){
  printf("Enter student ID Number: \n);
  scanf("%d", &student.ID);

void displayStud(studtype S){
	
  studtype student;
  printf("\n%-10s", student.ID);
}
Posted
Updated 6-May-20 7:36am
v3
Comments
phil.o 6-May-20 10:20am    
Please use the green "Improve question" widget and qualify the "What I have tried" section with the code causing the problem.
phil.o 6-May-20 10:45am    
Some part of the code appears to be missing from your sample (in the getStudentV1() function. Could you correct that please? And there is a double-quote missing at the end of the string in the printf() function in it.

C++
void getStudentV1(){
  printf("Enter student ID Number: \n);
  scanf("%d", &student.ID); // you refer to student.ID but there is no variable student declared anywhere.

void displayStud(studtype S){ // you pass in a structure named S but never refer to it
	
  studtype student; // you are creating a local student structure which will disappear when this function returns.
  printf("\n%-10s", student.ID); // the local student structure does not contain any information
}


You should create your structure variables in main and pass pointers to them to your functions; something like:
C++
int main()
{
    studtype student;

    getStudentV1(&student);
    displayHeader(&student);
    displayStud(&student);

  return 0;
}

void getStudentV1(studtype *student)
{
    // you can now refer to the individual fields
    // using 'arrow' notation thus:
    scanf("%d", &student->ID);

    // use similar references to collect the other details
    // but you will need to use both 'arrow' and 'dot' when
    // collecting the name fields:
   //        student->name.fname
}
 
Share this answer
 
Comments
CPallini 6-May-20 12:12pm    
5.
Richard MacCutchan 6-May-20 13:00pm    
Thanks Carlo.
Solution 1 is correct, but when you want to store more information you need to allocate some memory.
C++
 int studentCount = 10;//or ask the user
//allocate memory for the students
studtype *students = new studtype[studentCount];

 //loop and work with students
for( int i = 0; i < studentCount; i++ ){
//use a single student
studtype *student = students[i];
}

delete students; // free the memory
This has the advantage that you can use some students in your code. Visit the Learn C++ tutorial to about the language and its usage.

Learn to use the debugger. Good luck :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900