Click here to Skip to main content
15,311,151 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[100];
char icnum[100];
char matricnum[100];
char subcode[100];
char subname[100];

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

printf("\nEnter ic number :");
scanf("%s", &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 : %s", icnum);
printf("\nMatric number : %s", matricnum);
printf("\nSubject code : %s", subcode);
printf("\nSubject name : %s", subname );


}
Posted
Updated 5-Nov-21 4:24am

1 solution

C++
printf("\nEnter ic number :");
scanf("%s", &icnum);

You do not use the addressOf operator (&) on arrays. The array name is a pointer to the memory address. Change it to:
C++
printf("\nEnter ic number :");
scanf("%s", icnum);

And do the same for the other arrays.
   
Comments
Denisha Gunalan 5-Nov-21 10:41am
   
Can you help me with sub name? For an example if I enter computer science as input, it only comes as computer in output. How can I fix that one?
Richard MacCutchan 5-Nov-21 10:58am
   
Then do not use scanf, use gets. scanf captures the next token you type in which is terminated by white space. So if you type "Computer Science", it captures only the first word. I would strongly suggest that you go tob the documentation for all these functions and learn the rules that they follow; guessing never works. See scanf, _scanf_l, wscanf, _wscanf_l | Microsoft Docs[^].
OriginalGriff 5-Nov-21 10:56am
   
I gave him sample code to that effect last time he posted this question... :sigh:
https://www.codeproject.com/Questions/5316908/Can-anyone-help-me-with-this-code-I-can-enter-inpu
Richard MacCutchan 5-Nov-21 11:00am
   
It seems to be the pattern that you have to give the same answer numerous times before it sinks in. And no one seems to teach them how to read the documentation (apart from us, see above).
Richard MacCutchan 5-Nov-21 11:01am
   
Oh, and I did like your comment on that answer, "you probably already worked that out". ROFL.
OriginalGriff 5-Nov-21 12:02pm
   
My work here is done! :D

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

  Print Answers RSS


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