Click here to Skip to main content
15,311,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Develop a simple subject registration system. The program requires user to enter student’s full
name, I/C number, matric number, subject code and subject name. Then the program will print out
all the data that has been entered on the screen.

What I have tried:

#include <stdio.h>

int main()
{
char name[30];
int icnum;
char matricnum;
char subcode;
char subname;

printf("\nEnter full name :");
gets(name);

printf("\nEnter ic number :");
scanf("%d", &icnum);

printf("\nEnter matric number :");
scanf("%s", &matricnum);

printf("\nEnter subject code :");
scanf("%s", &subcode);

printf("\nEnter subject name :");
scanf("%s", &subname);

printf("\nFull name :");
puts(name);
printf("\nIc number : %d", icnum);
printf("\nMatric number : %s", matricnum);
printf("\nSubject code : %s", subcode);
printf("\nSubject name : %s", subname );


}
Posted
Updated 5-Nov-21 1:23am

1 solution

Simple: you are reading data into a string - so the scanf expects and gets a pointer to a character. But the space you have allocated to that character is a single char value, so there is nowhere for more than one character or the string terminating null character to go!

Try allocating more space:
C
char matricnum[100];
printf("\nEnter matric number :");
scanf("%s", matricnum);
printf("\nMatric number : %s", matricnum);
And it should work.

Obviously, you will need to do the same for all other strings - but you probably worked that out already!
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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