数据未保存在数组中

时间:2015-11-04 21:04:51

标签: c

出于某种原因,当我向student结构插入数据时,我插入的数据不会被保存。

如果我在每次运行后打印st[],我在scanf()中插入的数据都不会被保存。

我该怎么办才能add_rec()保存我的数据?

我的代码是:

#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;
}

感谢所有帮助者:)

3 个答案:

答案 0 :(得分:1)

我更正了你的守则。这是更正后的代码,

#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;
 }

我更改/添加到您的代码的事情是,

  1. 我将char stnumber[10];更改为int stnumber

  2. 因此,我更改了此代码

     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;
      }
    

    到此代码,

     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. 将此代码scanf("%s",&confirm);更改为scanf("%c",&confirm);

  4. 最后,我添加了以下代码,以表明您的数据已保存。

    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");
    }
    

答案 1 :(得分:1)

在add_rec中对scanf的前两次调用是错误的,你必须删除&amp;:

这些是纠正的行:

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

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

此外主要是:

scanf("%s",&confirm);

应该是

scanf(" %c",&confirm);

另见What does space in scanf mean?

答案 2 :(得分:0)

可能更容易看到清理了一下。好的风格很重要,因为它让你更容易看到逻辑错误。不要超过每行80个字符。尽可能简化。不要使用goto。如果是,则使用否定if而不是positive。 add_rec有大量不必要的复制。

我为您现有的风格清理了Mutex202的解决方案[信用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;
}

请注意,我将st[*itemcount].whatever替换为st->whatever,因为它在整个add_rec中都是不变的。