Philip Hudson
Philip Hudson

Reputation: 45

Arrays in and out of Struct with user input

So I am working on a program, I'm a net admin and terrible at programming, and need some help with structures, arrays, and possibly pointers. I am trying to write a program using struct, I know I need arrays but I am not sure how to properly tie them in with the struct. All info needs to be user input and needs to break on command to either skip to end or Loop back to certain point to enter more info. I need the user to input the employee ID#, then be able input multiple review scores, like 100, 90, 80, then break that sequence and either go back and enter another employee# and keep going, or skip to the end and print out all info entered.

The employee ID number and score entry seems to work fine when entering but when I print it out it does not look right so I am obviously doing something wrong with how the data is being stored and then printed. code and results below.

#include <stdio.h>

struct help{
    int empID;
    int marks[100];
    };
int main(){
    struct help s[100];
    int i, input, empNUM;

    NEWENTRY: printf("Enter employee ID#: ");
    scanf("%d", &empNUM);

    for(i=0;i<10;++i){
        s[i].empID = empNUM;
        printf("\nFor employee ID# %d\n",s[i].empID);

            while (i <= 100) {
                printf("Enter score:");

                if (scanf("%d", &input) == 1) {
                    if (input >= 0 && input <= 100) {
                        s[i].marks[100] = input;
                        i++;
                        }
                    else if (input == 101) {
                        printf("\n\nExiting entry.\n");
                        i = i - 1;
                        goto NEWENTRY;
                        }
                    else if (input == 102) {
                        printf("\n\nExiting entry.\n");
                        i = i - 1;
                        goto EXIT;
                        }
                    }
                }
            }
        EXIT:

        for(i=0;i<10;++i) {
            printf("\nInformation for employee ID number %d:\n",s[i].empID);
            printf("Marks: %.1f",s[i].marks);
            }

        return 0;
    }

Apologies, I forgot to add the pic

I would like it to look remotely like this if possible.

info for emp id 12345:
  100
  90
  80

info for emp id 67890:
  80
  90
  60

Upvotes: 1

Views: 489

Answers (1)

Cherubim
Cherubim

Reputation: 5457

There are many problems in your code :

Problem 1 :

Here in your code,

you are using the same parameter i for the outer for loop and inner while loop :

for(i=0;i<10;++i) //you are using i here
{
    s[i].empID = empNUM;
    printf("\nFor employee ID# %d\n",s[i].empID);

        while (i <= 100) //and even here 
        {
            printf("Enter score:");

Problem 2 :

apart from that every time you goto NEWENTRY: and then again enter the for loop, i value is again set to 0,

so no matter how many entries you may enter, you will only populate the first element of struct array s i.e, s[0]


Problem 3:

you are using wrong arguments while printing your array here :

  printf("Marks: %.1f",s[i].marks);

here s[i].marks is of the type int*, you are using it to print a double data


Solution :

The simple solution I can give is that :

never usegoto (click to see why :) )

as it makes your code very complex to understand and more reasons can be known by clicking it.

you can instead achieve what you are trying to do without using goto this way:

#include <stdio.h>

struct help
{
    int empID;
    int marks[100];    
};
int main()
{
    struct help s[100];
    int i, j;   //useful for indices of array
    int val;    //useful for taking in user input
    int flag=0; //useful for exiting the program

    for(i=0;i<10;)
    {

        printf("Enter employee ID#: ");
        scanf("%d",&s[i].empID);

        printf("\nFor employee ID# %d\n",s[i].empID);

        for(j=0;j<100;j++)
        {
            printf("Enter score : ");
            scanf("%d",&val); //taking in score input

            if(val>=0 && val<=100)
            {
                s[i].marks[j]=val;
            }
            else if(val==101)
            {
                s[i].marks[j]=-1; //to mark the end of entries I used -1
                break;
            }
            else if(val==102)
            {
                s[i].marks[j]=-1;
                flag=1;            //to know whether user wants to exit
                break;
            }
            else
            {
                printf("\ninvalid entry\n");
                j--;
            }
        }

        printf("\n\n----------\n\n");

        i++;

        if(flag==1) //to exit
            break;
    }

    int num=i;

    for(i=0; i<num ; i++)
    {
        printf("\nInformation for employee ID number %d:\n",s[i].empID);
        for(j=0; s[i].marks[j]!=-1; j++)
            printf("Marks: %d\n",s[i].marks[j]);
    }

    printf("\nenter any key to exit!\n");
    scanf("%d",&i);

    return 0;
}

This logic is quite simple to understand :

  • the nested for loop I used is just like any other nested for loop used for populating a 2D array
  • in the inner for loop, val takes in the user's input and goes through an else if ladder

    • if val is between 0 and 100, it gets accepted and stored in marks array of s[i]
    • else-if val is 101, then -1 is inserted to mark the end of marks array of s[i] and you break out of inner for loop and i value is incremented
    • else-if val is 102, then similarly

      1. -1 is inserted to mark the end of marks array of s[i].
      2. additionally flag which has been 0 till now is assigned to 1 and this helps in exiting the outer for loop
    • else (that is for all other cases), the number is not accepted and j value is decremented to retake the value
  • and finally you print the scores of employees just as you print values 2D array and also using the fact that we assigned end of each marks array of s[i] with a -1.

Upvotes: 2

Related Questions