The conclusions I can draw from the process of debugging this program are:
- Don't forget to always declare a function before defining it.
- The contents of a variable can be emptied using spaces enclosed in double single quotes (' '). Example: int select; is the same value as int select = ' ';
- Use the getch() function to display or print strings or characters on the screen, while also providing a pause.
- Don't forget to add break; to each switch-case branching option.
Output:
Implementation of 4 Return Value Functions to Calculate the Area of a Flat Plane
Source Code:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>
#include <stdio.h>
/*
*bundet.com
*Wawan Beneran
*Implementasi multiple fungsi
*Untuk menghitung luas bidang datar
*/
//pertama, definisikan fungsi dulu, setelah itu baru dideklarasikan.
float hitung_luas_persegi(float p, float l, float L);
float hitung_luas_lingkaran(float r, float L);
float hitung_luas_segitiga(float a, float t, float L);
void say_something(char word[]);
void tampil(int pilih);
int menu(){
int pilih = 4;
cout<<"1.hitung_luas_persegi?\n";
cout<<"2.hitung_luas_lingkaran?\n";
cout<<"3.hitung_luas_segitiga?\n";
cout<<"4.Keluar\n";
cout<<"pilihan anda (1-4)?";
cin>>pilih;
return pilih;
}
void main()
{
char word[50]="bye";
int pilih;
float a, b, c;
do {
pilih = menu();
clrscr();
tampil(pilih);
cout << endl;
pilih = ' ';
switch(pilih)
{
case 1:hitung_luas_persegi(a, b, c);
break;
case 2:hitung_luas_lingkaran(a, b);
break;
case 3:hitung_luas_segitiga(a, b, c);
break;
case 4:say_something(word);
break;
default:
cout<<"Monggo input lagi!"<< endl<< endl;
}
}while(pilih != 4);
getch();
}
float hitung_luas_persegi(float p, float l, float L) {
cout << "luas persegi"<<endl;
cout << "masukan nilai p : ";cin >> p;
cout << "masukan nilai l : ";cin >> l;
L = p*l;
return L;
}
float hitung_luas_segitiga(float a, float t, float L) {
cout << "luas segitiga" << endl;
cout << "masukan nilai alas : "; cin >> a;
cout << "masukan nilai tinggi : "; cin >> t;
L = 0.5 * a * t;
return L;
}
float hitung_luas_lingkaran(float r, float L) {
cout << "luas lingkaran" << endl;
cout << "masukan nilai r : ";cin >> r;
L = 3.14 * r * r;
return L;
}
void say_something(char word[])
{
cout<<word;
getch();
}
void tampil(int pilih){
float a, b, c;
char word[50];
if(pilih==1){
cout<<"Hasil :"<<hitung_luas_persegi(a, b, c)<<endl;
}
else if(pilih==2){
cout<<"Hasil :"<<hitung_luas_lingkaran(a, b)<<endl;
}
else if(pilih==3){
cout<<"Hasil :"<<hitung_luas_segitiga(a, b, c)<<endl;
}
else{
say_something(word);cout<<endl;
exit(4);
}
}
Hope this is useful & happy learning!