1

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
12
  • 4
    Do not access (no read nor write) s[i].marks[100], which is out-of-range, or you will invoke undefined behavior. printf("Marks: %.1f",s[i].marks) will also invoke undefined behavior because int* is passed where double is expected. Commented Jun 24, 2016 at 15:00
  • 2
    Also do not access s[100], which is also out-of-range. Commented Jun 24, 2016 at 15:03
  • 1
    Can you show an example of input and result Commented Jun 24, 2016 at 15:07
  • 1
    Please show us an example of input and expected output. It's hard to guess what your program is actually supposed to do. Commented Jun 24, 2016 at 15:22
  • 3
    @PhilipHudson, it looks like you are getting confused by the fact that each element of your array of structs has a member that is also an array. You appear to want separate index variables for the two. If you want to access the jth mark given by the ith employee, that would be s[i].marks[j]. Commented Jun 24, 2016 at 15:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@PhilipHudson any doubts, feel free to ask :)
Just for my own information, where 101 and 102 do the breaks, is it possible to use a word in there? So instead of typing 101, you would type exit, and instead of 102, you would type quit. Or would that not work because they are char, or maybe strings? thank you
@PhilipHudson nope... because user's input is being taken into an integer val.. so an integer cannot store a word right :)
@PhilipHudson I understand what you want to achieve but that is a little complex... instead of taking in an integer as input you must use a string... hope you can work around with it.... but the best thing to do is just display at the starting of program using printf() that 101 is for new entry and 102 for printing results and exiting..
That is what I did, adding the 101 102 at the beginning. Thank you for all your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.