I have to make a program that gets as an input a and b, gets as input a number "a" of lines of the following form: "studentId studentName studentPhone" and b lines of input in the form "stId mark1 mark2 mark3". The program then outputs all the stid from the first input and if the same id exists in input b the program outputs the students marks besides its id.
I've gone through hell to getting the input properly and I think this is close but I get a strange behavior: after I input the marks in the second input it seems like some of the student ids in the first input are changed.
This is my code: (here i try to input just the student IDs. http://ideone.com/dBYzwe )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
void chomp(char *s);
struct listA{
int stId;
char stName[19];
char stPhone[12];
};
struct listB{
int stId;
char marks[11];
};
int main(void) {
int i, j, m, n;
scanf("%d%d", &m, &n);
struct listA *a = malloc(sizeof(m * sizeof(struct listA)));
struct listB *b = malloc(sizeof(n * sizeof(struct listB)));
for(i = 0; i < m; i++)
{
scanf("%d", &a[i].stId);
fgets(a[i].stName, 19, stdin);
fgets(a[i].stPhone, 11, stdin);
chomp(a[i].stName);
chomp(a[i].stPhone);
}
for(i = 0; i < m; i++)
printf("%d ", a[i].stId);
for(i = 0; i < n; i++)
{
scanf("%d ", &b[i].stId);
fgets(b[i].marks, 12, stdin);
fflush(stdin);
}
printf("\n");
for(i = 0; i < n; i++)
{
printf("%d ", b[i].stId);
}
printf("\n");
for(i = 0; i < m; i++)
printf("%d ", a[i].stId);
return 0;
}
void chomp(char *s) {
while(*s && *s != '\n' && *s != '\r') s++;
*s = 0;
}
The problem is in the
struct listA *a = malloc(sizeof(m * sizeof(struct listA)));
struct listB *b = malloc(sizeof(n * sizeof(struct listB)));
The result of m * sizeof(struct listA)
is an integer, so when you put that into sizeof
you get the size of the integer, not the number you want. You should change this to:
struct listA *a = malloc(m * sizeof(struct listA));
struct listB *b = malloc(n * sizeof(struct listB));