0

How to add if-else statement for "kodeprodi"?

Everytime I add if-else statement, the message "Lvalue required" always appears.

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct {
    char bp[13];
    char nama[15];
    int kodeprodi;
    char namaprodi[10];
    float ipk;
} mahasiswa;

int main()
{
    char pil;

    do {
        mahasiswa mhs[10];
        int i, n;

        {
            printf("Data Nilai Mahasiswa\n");
            printf("Berapa banyak data = ");
            scanf("%d", &n);

            for(i = 0; i < n; i++) {
                printf("Data mahasiswa ke-%d\n", i+1);
                printf("Nomor BP: ");   scanf("%s", &mhs[i].bp);
                printf("Nama: ");       scanf("%s", &mhs[i].nama);
                printf("Kode Prodi: "); scanf("%d", &mhs[i].kodeprodi);
                printf("IPK: ");        scanf("%f", &mhs[i].ipk);

                if      (mhs[i].kodeprodi == 260) {mhs[i].namaprodi = "SI";}
                else if (mhs[i].kodeprodi == 261) {mhs[i].namaprodi = "TI";}
            }

            //output
            printf("No.     BP     Nama      Kode Prodi                 Nama Prodi      IPK    \n");

            for(i = 0; i < n; i++) {
                printf("\n%2d %-10s %-9s %3d %3s %3.f\n",
                       i+1, mhs[i].bp, mhs[i].nama, mhs[i].nama,
                       mhs[i].kodeprodi, mhs[i].namaprodi, mhs[i].ipk);
            }
        }

        printf("Repeat again? Y/N");
        scanf("%s", &pil);
        printf("\n\n");

    } while ((pil == 'Y') || (pil == 'y'));
}

Even if in the statement if-else, I type like this

if(mhs[i].kodeprodi==260){namaprodi = "SI");

The error message is "Undefined symbol 'namaprodi'

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
Raka Doank
  • 21
  • 2

2 Answers2

0

For a quick fix, use:

if(mhs[i].kodeprodi==260){strncpy(mhs[i].namaprodi, "SI", 9);

strncpy() is needed to copy the contents into namaprodi.

namaprodi is a member of struct mahasiswa, so you can't access it directly.

But better use std::string instead.

Also, as @BoPersson mentioned, char kodeprodi; can't hold 260, so you'll better to convert that to an int.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
0

I tweaked your code a bit. Got rid of unused conio.h, changed kodeprodi type to int (because char can only handle numbers -127..127), removed & from some scanf calls (because you should pass pointer to first character for %s formatter), deleted extra mhs[i].nama argument for printf.

Sorry, I completely didn't understood your code :-) My tweaks were semi-automatic! You should learn C programming better.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char bp[13];
    char nama[15];
    int kodeprodi;
    char namaprodi[10];
    float ipk;
} mahasiswa;

int main() {
    char pil;

    do {
        mahasiswa mhs[10];
        int i, n;

        {
            printf("Data Nilai Mahasiswa\n");
            printf("Berapa banyak data = ");
            scanf("%d", &n);

            for(i=0;i<n;i++) {
            printf("Data mahasiswa ke-%d\n", i+1);
            printf("Nomor BP: "); scanf("%s", mhs[i].bp);
            printf("Nama: "); scanf("%s", mhs[i].nama);
            printf("Kode Prodi: "); scanf("%d", &mhs[i].kodeprodi);
            printf("IPK: "); scanf("%f", &mhs[i].ipk);

            if(mhs[i].kodeprodi==260)
                strcpy(mhs[i].namaprodi, "SI");
            else if(mhs[i].kodeprodi==261)
                strcpy(mhs[i].namaprodi, "TI");
        }

        //output
        printf("No.     BP     Nama      Kode Prodi                 Nama Prodi      IPK    \n");

        for(i=0;i<n;i++) {
            printf("\n%2d %-10s %-9s %3d %3s %3.f\n", i+1, mhs[i].bp, mhs[i].nama, mhs[i].kodeprodi, mhs[i].namaprodi, mhs[i].ipk);
        }

        }

        printf("Repeat again? Y/N");
        scanf("%s", &pil);
        printf("\n\n");

    } while ((pil == 'Y') || (pil == 'y'));

    return 0;
}
Kyrylo Polezhaiev
  • 1,626
  • 11
  • 18