Menampilkan GMT menggunakan bahasa pemrograman C++ adalah topik yang akan dibahas. Contoh program C++ ini menampilkan beberapa operator aritmatika, seperti operator modulo dan pembagian. Selain itu, program ini juga menggunakan augmented assignment operator. Contoh program c++ sederhana ini diharapkan bisa diikuti oleh pemula seperti seperti #bayuyudhasaputra .
Masalah ini saya ambil dari buku Introduction to Programming with C++ 3rd Edition karya Liang yang bisa didapatkan melalui tautan:
https://www.pearson.com/en-us/subject-catalog/p/Liang-Companion-Website-for-Introduction-to-Programming-with-C-Access-to-Videonotes-3rd-Edition/P200000003422/978013338026
Baris kode ini juga bisa dilihat dan diunduh melalui tautan berikut:
https://github.com/bayuYudhaSaputra/introduction-programming-CPP-liang/blob/main/02.08.CurrentTime.cpp
2.8. Menampilkan GMT menggunakan bahasa pemrograman C++
1. *2.8. (Current time)
Listing 2.9, ShowCurrentTime.cpp, gives a program that displays the current time
in GMT. Revise the program so that it prompts the user to enter the time zone
offset to GMT and displays the time in the specified time zone.
Here is a sample run:
Enter the time zone offset to GMT: -5
The current time is 4:50:34
Soal 2.8 ini meminta kita untuk memodifikasi listing 2.9.ShowCurrentTime.cpp berikut:
1 #include <iostream>
2 #include <ctime>
3 using namespace std;
4
5 int main()
6 {
7 // Obtain the total seconds since midnight, Jan 1, 1970
8 int totalSeconds = time(0);
9
10 // Compute the current seconds in the minute in the hour
11 int currentSecond = totalSeconds % 60;
12
13 // Obtain the total minutes
14 int totalMinutes = totalSeconds / 60;
15
16 // Compute the current minute in the hour
17 int currentMinute = totalMinutes % 60;
18
19 // Obtain the total hours
20 int totalHours = totalMinutes / 60;
21
22 // Compute the current hour
23 int currentHour = totalHours % 24;
24
25 // Display results
2. 26 cout << "Current time is " << currentHour << ":"
27 << currentMinute << ":" << currentSecond << " GMT " << endl;
28
29 return 0;
30 }
Program listing 2.9 ini digunakan untuk menentukan pukul dalam GMT. Kemudian program
ini dimodifikasi agar pengguna bisa menginput offset zona waktu ke dalam GMT. Baris kode
yang ditambahkan adalah:
25 // menerima input zona waktu (Misal, input 5 jika berada pada zona
waktu GMT+5)
26 cout << endl
27 << "======================================================="
28 << endl
29 << "Input zona waktu anda (Misal, input 5 jika zona waktu anda
GMT+5) : ";
30
31 int zonaWaktu;
32 cin >> zonaWaktu;
33
34 cout << endl
35 << "-------------------------------------------------------"
36 << endl;
37
38 currentHour += zonaWaktu;
Pada baris ke-31, program mendefinisikan variabel dengan nama zonaWaktu dengan tipe data
integer. Variabel ini digunakan untuk menampung nilai yang diinput oleh pengguna. Kemudian
pada baris ke-38, nilai currentHour diperbarui dengan menambahkan nilai dalam variabel
zonaWaktu. Baris kode lengkap setelah dimodifikasi adalah:
1 #include <iostream>
2 #include <ctime>
3 using namespace std;
3. 4
5 int main()
6 {
7 // Obtain the total seconds since midnight, Jan 1, 1970
8 int totalSeconds = time(0);
9
10 // Compute the current seconds in the minute in the hour
11 int currentSecond = totalSeconds % 60;
12
13 // Obtain the total minutes
14 int totalMinutes = totalSeconds / 60;
15
16 // Compute the current minute in the hour
17 int currentMinute = totalMinutes % 60;
18
19 // Obtain the total hours
20 int totalHours = totalMinutes / 60;
21
22 // Compute the current hour
23 int currentHour = totalHours % 24;
24
25 // menerima input zona waktu (Misal, input 5 jika berada pada zona
waktu GMT+5)
26 cout << endl
27 << "======================================================="
28 << endl
29 << "Input zona waktu anda (Misal, input 5 jika zona waktu
anda GMT+5) : ";
30
31 int zonaWaktu;
32 cin >> zonaWaktu;
33
34 cout << endl
35 << "-------------------------------------------------------"
36 << endl;
37
38 currentHour += zonaWaktu;
39
40 // Display results
41 cout << "Current time" << " (GMT" << zonaWaktu << ") is "
42 << currentHour << ":" << currentMinute << ":" << currentSecond
43 << endl
4. 44 << "======================================================="
45 << endl;
46
47 return 0;
Contoh output yang ditampilkan program ketika pengguna menginput nilai 5 adalah
sebagai berikut:
Contoh output yang ditampilkan program ketika pengguna menginput -6 adalah sebagai
berikut:
5. Sumber Rujukan:
Liang. 2014. Introduction to Programming with C++ 3rd Edition. London: Pearson Education
yang bisa diakses pada tautan berikut:
https://www.pearson.com/en-us/subject-catalog/p/Liang-Companion-Website-for-
Introduction-to-Programming-with-C-Access-to-Videonotes-3rd-
Edition/P200000003422/978013338026
Baris kode ini bisa diakses pada tautan berikut:
https://github.com/bayuYudhaSaputra/introduction-programming-CPP-
liang/blob/main/02.08.CurrentTime.cpp