Facebook
From Tengku Muhammad Fadlan Praditya, 2 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 74
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int panjangString(string str)
  7. {
  8.     return str.length();
  9. }
  10. bool panjangStringLebihMaksimal(string str, int maksimal)
  11. {
  12.     return (panjangString(str) > maksimal) ? true : false;
  13. }
  14.  
  15. int main()
  16. {
  17.     const int maksNama = 31, maksNIM = 15, maksMataKuliah = 50;
  18.     string inputNama, inputNIM, inputMataKuliah;
  19.  
  20.     try
  21.     {
  22.         cout << "Nama        : ";
  23.         getline(cin, inputNama);
  24.         if (panjangStringLebihMaksimal(inputNama, maksNama))
  25.         {
  26.             string maksNamaString = to_string(maksNama);
  27.             throw string("Panjang Nama melebihi maksimal huruf! (" + maksNamaString + " huruf)");
  28.         }
  29.  
  30.         cout << "NIM         : ";
  31.         getline(cin, inputNIM);
  32.         if (panjangStringLebihMaksimal(inputNIM, maksNIM))
  33.         {
  34.             string maksNIMString = to_string(maksNIM);
  35.             throw string("Panjang NIM melebihi maksimal huruf! (" + maksNIMString + " huruf)");
  36.         }
  37.  
  38.         cout << "Mata Kuliah : ";
  39.         getline(cin, inputMataKuliah);
  40.         if (panjangStringLebihMaksimal(inputMataKuliah, maksMataKuliah))
  41.         {
  42.             string maksMataKuliahString = to_string(maksMataKuliah);
  43.             throw string("Panjang Mata Kuliah melebihi maksimal huruf! (" + maksMataKuliahString + " huruf)");
  44.         }
  45.  
  46.         cout << endl;
  47.  
  48.         cout << "Banyak karakter Nama adalah: " << panjangString(inputNama) << " huruf" << endl;
  49.         cout << "Banyak karakter NIM adalah: " << panjangString(inputNIM) << " huruf" << endl;
  50.         cout << "Banyak karakter Mata Kuliah adalah: " << panjangString(inputMataKuliah) << " huruf" << endl;
  51.  
  52.         return 0;
  53.     }
  54.     catch (string pesanError)
  55.     {
  56.         cout << endl;
  57.         cout << "Error Muncul Karena: " << pesanError << endl;
  58.         cout << endl;
  59.         cout << "Banyak karakter Nama adalah: 0 huruf" << endl;
  60.         cout << "Banyak karakter NIM adalah: 0 huruf" << endl;
  61.         cout << "Banyak karakter Mata Kuliah adalah: 0 huruf" << endl;
  62.  
  63.         return 1;
  64.     }
  65. }