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.