-1

For some reason, when I insert data to the student struct, the data that I inserted is not saved.

If I print st[] after each run, the data that I inserted in scanf() is not beign saved.

What should I do so that add_rec() will save my data?

my code is:

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

typedef struct
{
    char stnumber[10];
    char stname[20];
    char gender;
    float quiz1;
    float quiz2;
    float assigment;
    float midterm;
    float final;
    float total;
}student;

/*
Defining the add_rec(student[] st, int *itemcount) function to add a new record to the the array of student objects.
This function takes two arguments.
The first argument is the array of student objects(st) and the second argument is the number of items in the array.
This function firstly checks the new record(using the search function that is defined in the next step) before it is allowed to be appended to the array to avoid duplicate records.
When the new item is added the value of itemcount variable increases by 1 that means the number of records in the list increases.
*/
void add_rec(student st[],int *itemcount)
{
    againID:
    printf("\nEnter the student's ID (9 digits): ");
    scanf(" %s",&st[*itemcount].stnumber);
    if (strlen(st[*itemcount].stnumber) != 9)
    {
        printf("invalid\n"); goto againID;
    }

printf("Enter the student's Name: ");
scanf(" %s",&st[*itemcount].stname);

againGender:
printf("Enter the student's Gender(F or M): ");
scanf(" %c",&st[*itemcount].gender);
if (st[*itemcount].gender != 'm' && st[*itemcount].gender != 'M' && st[*itemcount].gender != 'f' && st[*itemcount].gender != 'F')
{
    printf("invalid\n"); goto againGender;
}

againquiz1:
printf("Enter the student's 1st quiz score: ");
scanf(" %f",&st[*itemcount].quiz1);
if (st[*itemcount].quiz1 < 0 || st[*itemcount].quiz1 > 100)
{
    printf("invalid\n"); goto againquiz1;
}

againquiz2:
printf("Enter the student's 2nd quiz score: ");scanf(" %f",&st[*itemcount].quiz2);
if (st[*itemcount].quiz2 < 0 || st[*itemcount].quiz2 > 100)
{
    printf("invalid\n"); goto againquiz2;
}

againAssigment:
printf("Enter the student's assigment score: ");scanf(" %f",&st[*itemcount].assigment);
if (st[*itemcount].assigment < 0 || st[*itemcount].assigment > 100)
{
    printf("invalid\n"); goto againAssigment;
}

againMidterm:
printf("Enter the student's mid-term score: ");scanf(" %f",&st[*itemcount].midterm);
if (st[*itemcount].midterm < 0 || st[*itemcount].midterm > 100)
{
    printf("invalid\n"); goto againMidterm;
}

againFinal:
printf("Enter the student's final score: ");scanf(" %f",&st[*itemcount].final);
if (st[*itemcount].final < 0 || st[*itemcount].final > 100)
{
    printf("invalid\n"); goto againFinal;
}

st[*itemcount].total = st[*itemcount].quiz1 + st[*itemcount].quiz2 + st[*itemcount].assigment + st[*itemcount].midterm + st[*itemcount].final;

++(*itemcount);
}

int main()
{
    student st[20];
    int itemcount=0;
    char confirm;
    do
    {
        add_rec(st, &itemcount);
        printf("Press y or Y to continue: ");
                scanf("%s",&confirm);
    } while(confirm=='y'||confirm=='Y');
    return 0;
}

Thanks to all the helpers :)

Ron Halfon
  • 105
  • 1
  • 2
  • 10

3 Answers3

1

I corrected your Code. Here is the Corrected code,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#pragma warning(disable:4996)

typedef struct
{
  int stnumber;
  char stname[20];
  char gender;
  float quiz1;
  float quiz2;
  float assigment;
  float midterm;
  float final;
  float total;
}student;


 void add_rec(student st[], int *itemcount)
 {
     againID:

      int temp = 0,count=0;
      printf("\nEnter the student's ID (9 digits): ");
      scanf("%d", &st[*itemcount].stnumber);
      temp = st[*itemcount].stnumber;
      while (temp != 0)//Checks if it 9 digit number or not
      {
       temp = temp / 10;
       ++count;
      }
      if (count!= 9)
      {
       printf("Error!9 digit Number!\n");
       goto againID;
      }

      printf("Enter the student's Name: ");
      scanf("%s", &st[*itemcount].stname);

    againGender:

      printf("Enter the student's Gender(F or M): ");
      scanf("%s", &st[*itemcount].gender);
      if (st[*itemcount].gender != 'm' && st[*itemcount].gender != 'M' &&   st[*itemcount].gender != 'f' && st[*itemcount].gender != 'F')
      {
        printf("invalid\n"); goto againGender;
      }

    againquiz1:

     printf("Enter the student's 1st quiz score: ");
     scanf("%f", &st[*itemcount].quiz1);
     if (st[*itemcount].quiz1 < 0 || st[*itemcount].quiz1 > 100)
     {
       printf("invalid\n"); goto againquiz1;
     }

    againquiz2:

      printf("Enter the student's 2nd quiz score: "); scanf("%f",   &st[*itemcount].quiz2);
      if (st[*itemcount].quiz2 < 0 || st[*itemcount].quiz2 > 100)
      {
         printf("invalid\n"); goto againquiz2;
      }

    againAssigment:

      printf("Enter the student's assigment score: "); scanf("%f",  &st[*itemcount].assigment);
      if (st[*itemcount].assigment < 0 || st[*itemcount].assigment > 100)
      {
        printf("invalid\n"); goto againAssigment;
      }

    againMidterm:

      printf("Enter the student's mid-term score: "); scanf("%f", &st[*itemcount].midterm);
      if (st[*itemcount].midterm < 0 || st[*itemcount].midterm > 100)
      {
         printf("invalid\n"); goto againMidterm;
      }

    againFinal:

       printf("Enter the student's final score: "); scanf("%f", &st[*itemcount].final);
       if (st[*itemcount].final < 0 || st[*itemcount].final > 100)
       {
          printf("invalid\n"); goto againFinal;
       }

       st[*itemcount].total = st[*itemcount].quiz1 + st[*itemcount].quiz2 +   st[*itemcount].assigment + st[*itemcount].midterm + st[*itemcount].final;

      ++(*itemcount);
}
int main()
{
  student st[20];
  int itemcount = 0,i=0;
  char confirm;
  do
  {
    add_rec(st, &itemcount);
    printf("Press y or Y to continue: ");
    scanf("%c", &confirm);
  } while (confirm == 'y' || confirm == 'Y');
  for (i = 0; i < itemcount; i++)
  {
    printf("%s\n", st[i].stname);
    printf("%d\n", st[i].stnumber);
    printf("%c\n", st[i].gender);
    printf("%f\n", st[i].quiz1);
    printf("%f\n", st[i].quiz2);
    printf("%f\n", st[i].assigment);
    printf("%f\n", st[i].midterm);
    printf("%f\n", st[i].final);
    printf("%f\n", st[i].total);
    printf("\n");
  }
  return 0;
 }

The Things which I changed/Added to your code are,

  1. I Changed char stnumber[10]; to int stnumber

  2. Accordingly,I changed this code

     againID:
      printf("\nEnter the student's ID (9 digits): ");
      scanf(" %s",&st[*itemcount].stnumber);
      if (strlen(st[*itemcount].stnumber) != 9)
      {
        printf("invalid\n"); goto againID;
      }
    

    To this code,

     againID:
      int temp = 0,count=0;
      printf("\nEnter the student's ID (9 digits): ");
      scanf("%d", &st[*itemcount].stnumber);
      temp = st[*itemcount].stnumber;
      while (temp != 0)//Checks if it 9 digit number or not
      {
         temp = temp / 10;
         ++count;
      }
      if (count!= 9)
      {
         printf("Error!9 digit Number!\n");
         goto againID;
      }
    
  3. Changed this code scanf("%s",&confirm); to scanf("%c",&confirm);

  4. Finally, I added the following code to show that your data is saved.

    for (i = 0; i < itemcount; i++)
    {
      printf("%s\n", st[i].stname);
      printf("%d\n", st[i].stnumber);
      printf("%c\n", st[i].gender);
      printf("%f\n", st[i].quiz1);
      printf("%f\n", st[i].quiz2);
      printf("%f\n", st[i].assigment);
      printf("%f\n", st[i].midterm);
      printf("%f\n", st[i].final);
      printf("%f\n", st[i].total);
      printf("\n");
    }
    
Mutex202
  • 31
  • 4
1

The first two call to scanf in add_rec are wrong you have to remove the &:

those are the lines corrected:

scanf("%s", st[*count].stnumber);

scanf("%s", st[*count].stname);

Moreover in the main this:

scanf("%s",&confirm);

should be

scanf(" %c",&confirm);

see also What does space in scanf mean?

Community
  • 1
  • 1
terence hill
  • 3,354
  • 18
  • 31
0

It might have been easier to see cleaned up a bit. Good style is important because it let's you see the logics bugs more easily. Don't go past 80 chars per line. Simplify where possible. Don't use goto's. You used negative if's instead of positive if's. add_rec had huge amounts of needless replication.

I cleaned up Mutex202's solution for your existing style [credit to Mutex202]:

void
add_rec_score(const char *prompt,float *score)
{

    while (1) {
        printf("Enter the student's %s score: ",prompt);
        scanf("%f", score);
        if (*score >= 0 && *score <= 100)
            break;
        printf("invalid\n");
    }
}

void
add_rec(student *st, int *itemcount)
{
    int temp;
    int count;

    st += *itemcount;

    while (1) {
        printf("\nEnter the student's ID (9 digits): ");
        scanf("%d", &st->stnumber);
        temp = st->stnumber;

        // Checks if it 9 digit number or not
        count = 0;
        while (temp != 0) {
            temp = temp / 10;
            ++count;
        }
        if (count == 9)
            break;

        printf("Error!9 digit Number!\n");
    }

    printf("Enter the student's Name: ");
    scanf("%s", &st->stname);

    while (1) {
        printf("Enter the student's Gender(F or M): ");
        scanf("%s", &st->gender);
        if (st->gender == 'm' || st->gender == 'M' ||
            st->gender == 'f' || st->gender == 'F')
            break;
        printf("invalid\n");
    }

    add_rec_score("1st quiz",&st->quiz1);
    add_rec_score("2nd quiz",&st->quiz2);
    add_rec_score("assignment",&st->assignment);
    add_rec_score("mid-term",&st->midterm);
    add_rec_score("final",&st->final);

    st->total = st->quiz1 + st->quiz2 + st->assigment + st->midterm + st->final;

    *itemcount += 1;
}

Notice that I replaced the st[*itemcount].whatever with st->whatever because it was invariant throughout add_rec.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48