0

Im new into C programming. So i want to build a program that whenever if the user give input "111" will display the 'total' variable and whenever if the user give input "999" the input/scanf will be stopped. I have succeed to solve this problem but without array. Now i want to solve it with array but i can't figure it how. (Ignore some unused variable cause im praticing on this).

 int i,j,k,o,n,nilai[100],nilaimnilai2,total=0;
    char nama[100][100],nim[100][100],nimif[100][2]={"111","999"},nim1[100],nim2[100];
    float rata2;
    printf("Berapa banyak data mahasiswa yang ingin diinput?: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\nMahasiswa ke-%d\n",i);
        fflush(stdin);
        printf("Masukkan nama  : ");
        scanf("%[^\n]",&nama[i]);
        fflush(stdin);
        printf("Masukkan NIM   : ");
        scanf("%[^\n]",&nim[i]);
        fflush(stdin);
        printf("Masukkan Nilai : ");
        scanf("%d",&nilai[i]);
        fflush(stdin);
        printf("\n");
        o=strcmp(nim[1],"111");
        k=strcmp(nim[2],"999");

        if(o==0)
        {
            total+=nilai[i];
            printf("Total nilai : %d\n",total);
            printf("Berikut adalah data nilai dari %d mahasiswa:\n",j);
        }
        else if(k==0);
        {
            //fclose(stdin);
            //total+=nilai[i];
            printf("salah");
        }
    }

The problem that i faced is how to use strcmp with string array? Here is the example if i run the program:

Berapa banyak data mahasiswa yang ingin diinput?: 2

Mahasiswa ke-1
Masukkan nama  : test a
Masukkan NIM   : 89
Masukkan Nilai : 98

salah
Mahasiswa ke-2
Masukkan nama  : test b
Masukkan NIM   : 111
Masukkan Nilai : 90

salah

It gives me very wrong output and unexpected. My knowledge for array is very short, but i want to figure it out.

The right and expected output for "111" input:

Berapa banyak data mahasiswa yang ingin diinput?: 2

Mahasiswa ke-1
Masukkan nama  : test a
Masukkan NIM   : 89
Masukkan Nilai : 98

Mahasiswa ke-2
Masukkan nama  : test b
Masukkan NIM   : 111
Masukkan Nilai : 90

Total nilai : 179
Berikut adalah data nilai dari 2 mahasiswa: 

The right and expected output for "999" input:

Berapa banyak data mahasiswa yang ingin diinput?: 2

Mahasiswa ke-1
Masukkan nama  : test a
Masukkan NIM   : 999
Masukkan Nilai : 98

salah
(scan stop and exit)
Process returned 0 (0x0)   execution time : 16.813 s
Press any key to continue.

How do i solve this? Im sorry if my question not too much detailed, because im having some difficulties to explain the problem, so i just give an example.

FinnnIT
  • 27
  • 4
  • 3
    To start with, arrays are indexed from 0 to n-1, so `for(i=1;i<=n;i++)` is wrong – stark Mar 01 '22 at 16:04
  • 1
    Two things: First passing an input-only stream (like `stdin`) to `fflush` is explicitly mentioned in the C specification as *undefined behavior*; And the `%[]` format expects an argument of type `char *`, which would be e.g. `nama[i]` (which is what `&nama[i][0]` decays to). The type of `&nama[i]` is `char (*)[100]`, and mismatching format specifier and argument type also leads to *undefined behavior*. – Some programmer dude Mar 01 '22 at 16:05
  • 1
    Furthermore, you use `nim[2]` even in the first iteration of the loop, which means that `nim[2]` haven't been initialized yet, and the array contents will be *indeterminate*. It might not have a string terminator inside it, which means `strcmp` will go out of bounds and again lead to undefined behavior. – Some programmer dude Mar 01 '22 at 16:10
  • `scanf("%[^\n]",&nama[i]);` is worse than [`gets()`](https://stackoverflow.com/q/1694036/2410359). research `fgets()`. – chux - Reinstate Monica Mar 01 '22 at 17:57
  • FinnnIT, Who or what text suggested using `fflush(stdin);`? – chux - Reinstate Monica Mar 01 '22 at 17:59

0 Answers0