Page 88 - MODUL ALGORTIMA DAN PEMROGRAMAN
P. 88

printf("Alamat: %s\n", dosen1.alamat);

                    printf("\n");

                    printf("Data Dosen Kedua\n");
                    printf("---------------\n");
                    printf("NIP: %ld\n", dosen2.nip);
                    printf("Nama: %s", dosen2.nama);
                    strcspn(dosen2.nama, "\n");  // Hapus newline dari nama
                    printf("Alamat: %s\n", dosen2.alamat);

                    printf("\n");

                    return 0;
                  }
                    printf("\n");

                    printf("Data Dosen Kedua\n");
                    printf("---------------\n");
                    printf("NIP: %ld\n", dosen2.nip);
                    printf("Nama: %s", dosen2.nama);
                    strcspn(dosen2.nama, "\n");  // Remove newline from nama
                    printf("Alamat: %s\n", dosen2.alamat);

                    printf("\n");

                    return 0;
                  }

                                               KODE PROGRAM BAHASA C++
                  #include <cstdlib>
                  #include <iostream>

                  using namespace std;

                  //deklarasi struct
                  typedef struct
                  {
                         long int nip;
                         char nama[31];
                         char alamat[51];
                  }data_dosen;

                  //deklarasi variabel struct
                  data_dosen dosen1, dosen2;

                  int main(int argc, char *argv)
                  {
                         //input
                         cout<<"Data Dosen Pertama\n";
                         cout<<"---------------\n";
                         cout<<"NIP  :";cin>>dosen1.nip;
                         cout<<"Nama : ";fflush(stdin);
                         cin.get(dosen1.nama,30);
                         cout<<"Alamat         :";fflush(stdin);
                         cin.get(dosen1.alamat,50);
                         cout<<endl<<endl;

                         cout<<"Data Dosen Kedua";
                         cout<<"---------------\n";
                         cout<<"NIP  :";cin>>dosen2.nip;
                         cout<<"Nama : ";fflush(stdin);
                         cin.get(dosen2.nama,30);                                                         65
                         cout<<"Alamat         : ";fflush(stdin);
                         cin.get(dosen2.alamat,50);
                         cout<<endl<<endl;

                         //output
   83   84   85   86   87   88   89   90   91   92   93