Thread: Need help with arrays.

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    4

    Question Need help with arrays.

    Hello.
    I´m doing a project in school, and I´ve reached a problem with my code.
    The projects goal is to make a Menu with 2 options, one for users to inputs 5 different kinds og data, in the form of:
    ID.
    Workplace.
    Workhours.
    Sickdays.
    Taken a course or not.
    then after that, the user is returned to the menu page where he can choose to either enter another entry or print it out.
    printing the info out is done when choosing the print part of the menu.
    all the inputs is in the form of an int.

    my problem is that i need to save the user indputs in some sort of array, that i then can print out later. but i cant seem to figure out how to save 5 different indputs in arrays for late use.

    My code untill now looks like below:

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main(int argc, char **argv)
    {
        int svar='0';
        int studieNummer[100];
        int institut[100];
        int antalTimer[100];
        int fravaersTimer[100];
        int DeltagelseAfKursus[100];
    
    
    
    
        printf("\n************   Menu *******\n");
        printf("\n1. Opret\n");
        printf("2. Udskriv\n");
        printf("Vaelg en mulighed (tast 3 for at afslutte): ");
        scanf("%d", &svar);
        while(svar!=3){
            switch(svar){
                case 1:
    
    
                        printf("\nIndtast studienummer:");
                        scanf("%d", &studieNummer);
                        printf("\nIndtast institut:\n1. Elektroteknologi\n2. Elektrisk energiteknologi\n3. IT-elektronik\n");
                        scanf("%d", &institut);
                        printf("\nIndtast antal timer arbejdet:");
                        scanf("%d", &antalTimer);
                        printf("\nIndtast fravaers timer:");
                        scanf("%d", &fravaersTimer);
                        printf("\nHar du deltaget i TA kursus (1=ja, 0=nej)?:");
                        scanf("%d", &DeltagelseAfKursus);
                        break;
                case 2:
                        printf("\nUdskriv");
                        break;
    
    
                default:
                        printf("\n\n\n Ugyldigt valg!\n");
                        break;
            }
            printf("\n************  Menu *******\n");
            printf("\n1. Opret\n");
            printf("2. Udskriv\n");
            printf("Vaelg en mulighed (tast 3 for at afslutte): ");
            scanf("%d", &svar);
        }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have a look at this tutorial...
    Arrays in C - Cprogramming.com

    Pay close attention to how they used a 'for' loop to fill an array.

    You'll need to fill enter info into all arrays each loop...

    You need to have a variable (say 'i') and change it for each input
    Code:
      ...
    
    for(i=0; i<100 && having_a_good_time; i++ )
    {
      printf("\nIndtast studienummer:");
      scanf("%d", &studieNummer[i]);
    
      ...
    
    }
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    4
    Thank you for the quick answer.
    I'm actually understanding that part. My problem is when I need to fill out more than one array, as in this project where I need to fill out 5 arrays I'm one go.
    Do I make 5 variables as in I and then 4 others?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So just expand what click_here said.
    Code:
                        printf("\nIndtast studienummer:");
                        scanf("%d", &studieNummer[i]);
                        printf("\nIndtast institut:\n1. Elektroteknologi\n2. Elektrisk energiteknologi\n3. IT-elektronik\n");
                        scanf("%d", &institut[i]);
                        printf("\nIndtast antal timer arbejdet:");
                        scanf("%d", &antalTimer[i]);
                        printf("\nIndtast fravaers timer:");
                        scanf("%d", &fravaersTimer[i]);
                        printf("\nHar du deltaget i TA kursus (1=ja, 0=nej)?:");
                        scanf("%d", &DeltagelseAfKursus[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    4
    But dosent it overwrite the previous data if i loop it 5 times? i mean does it store the data in the array as the first indput data in 0, second loop in 1 third loop in 2 and so on? so at the end I will end up with an 5 array, that has all the data stored from lets say USERID and one 5 array for workplace?
    Sorry if this is a stupid question, but im kinda new to programming, and trying my best to learn.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There is such a thing as actually trying it and finding out.

    Code:
    int a[3], b[3];
    for ( int i = 0 ; i < 3 ; i++ ) {
      a[i] = i * 10;  // takes the place of the first printf/scanf
      b[i] = i * 100;  // takes the place of the second printf/scanf
    }
    
    for ( int i = 0 ; i < 3 ; i++ ) {
      printf("%d %d\n", a[i], b[i] );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    4
    Disregard my last comment, it seems to be working now, and i´ve also made the printout part using the different places in the arrays. Actually i did it just as you guys disscribed when i tried yeasterday, my only flaw was that i made the curly bracket after the break, instead of before.
    The program isent as cleever as i would like it to be yet, as the ultimate goal was for it to read one list of indputs, then returd to menu, and be able to read another indputs, without overwriting the previous one. but i guess that is kinda tricky to make.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    if your arrays don't have variable sizes per element, you can write them in binary form with:

    Code:
    // a is the array pointer
    // f is pointer to FILE type.
    // sizeof(int) assuming you are saving an array of ints.
    fwrite( a, sizeof(int), 100, f );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  2. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread