0

This is the output I need. This is what I've got so far. I've only been able to figure out how to add 1 subject, but I need to add 2 more like the picture. I tried to save directly into the array but it didn't work. So i used strcpy.

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

typedef struct {    
 char subjectID[10];  
 char subjectName[10]; 
 float marks;    
}Subject;        
  
typedef struct {   
 char Stu_ID[10];  
 char Stu_Name[30]; 
 Subject subjects; 
}Student;      

int main()
{
 Student a[3]; 
 Student b;   
 int i;     

 strcpy(b.Stu_ID, "S100011");
 strcpy(b.Stu_Name, "James");
 strcpy(b.subjects.subjectID, "TOS10005");
 strcpy(b.subjects.subjectName, "Programming");
 b.subjects.marks=50;

 printf("Student details \n"); 
 printf("ID : %s\t Name : %s\n\n", b.Stu_ID, b.Stu_Name);         
 printf("Subject ID\t Name\t\t Marks\n");
 printf("%s\t %s\t\t %.2f\t \n",  b.subjects.subjectID, b.subjects.subjectName, b.subjects.marks);
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Have you considered making `Student::subjects` into an array? – Bill Lynch May 10 '21 at 03:28
  • first problem I see, `strcpy(b.subjects.subjectName, "Programming");`, the `subjectName` field is only 10 long, but `"Programming"` is 12 long (including `NUL` terminator). Overwriting the buffer invokes [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior), no point in trying to make sense of what your program does after that. Memory is cheap, make those 64 long at least. But you'll always need to make sure not overrun buffers. – yano May 10 '21 at 03:28
  • also, `fflush(stdin);` is undefined behavior. Not sure why @BillLynch removed that in an edit: https://stackoverflow.com/questions/18170410/what-is-the-use-of-fflushstdin-in-c-programming – yano May 10 '21 at 03:37
  • Welcome to StackOverflow! Please take the [tour] and read "[ask]". Please don't post screenshots of text, copy the text verbatim into your question. – the busybee May 10 '21 at 08:11

0 Answers0