Merhabalar öncelikle iyi forumlar dilerim. C++ ile yaptığım kod hata veriyor. Aslında hata değil olmaması gereken döngüye giriyor. Çözümünü bilen biri var ise yardımcı olabilir mi?
Kod:
(
#Include <iOStream>
Using namespace std;
Void btutocal(double BTU );
Void btutojoule(double BTU );
Void btutokwh(double BTU);
Void caltobtu(double BTU);
Void caltojoule(double BTU);
Void caltokhw(double BTU);
İnt main()
{
Double BTU, cal, joule, final;
Bool check=0;
Int option;
Int num1;
cout<<"\t\tWelcome to converter machine\n**********************************************************************"<<endl;
Cout<<"\tenter the desired converter option"<<endl;
Cout<<"1-BTU to Calories\n**********\n2-BTU to Joules\n**********\n3-BTU to kWh\n**********\n4-Calories to BTU\n**********\n5-Calories to joules\n**********\n6-Calories to kHw\n**********\n7-Exit"<<endl;
Cout<<"please enter the option\n*******"<<endl;
Cin>>option;
If(option==1)
{
Cout<<"please enter BTU"<<endl;
Cin>>BTU;
Void btutocal(double BTU );
}
Else if(option==2)
{
Cout<<"please enter BTU"<<endl;
Cin>>BTU;
Void btutojoule(double BTU );
}
Else if(option==3)
{
Cout<<"please enter BTU"<<endl;
Cin>>BTU;
Void btutokwh(double BTU );
}
Else if(option==4)
{
Cout<<"please enter calories"<<endl;
Cin>>BTU;
Void caltobtu(double BTU );
}
Else if(option==5)
{
Cout<<"please enter calories"<<endl;
Cin>>BTU;
Void caltojoule(double BTU );
}
Else if(option==6)
{
Cout<<"please enter calories"<<endl;
Cin>>BTU;
Void caltokwh(double BTU );
}
Else.
{
Cout<<"you have succesfully checked out\n******************************"<<endl;
}
Void btutocal(double BTU);
{
Cout<<BTU<<" BTU is -----> "<<BTU*252<<" calories"<<endl;
}
Void btutojoule(double BTU );
{
Cout<<BTU<<" BTU is -----> "<<BTU*1054<<" joules"<<endl;
}
Void btutokwh(double BTU );
{
Cout<<BTU<<" BTU is -----> "<<BTU*0.0002<<" kwh"<<endl;
}
Void caltojoule(double BTU);
{
Cout<<BTU<< " cal is -----> "<<BTU*4.18<<" joules"<<endl;
}
Void caltobtu(double BTU);
{
Cout<<BTU<< " cal is -----> "<<BTU*0.00397<<" BTU"<<endl;
}
Void caltokwh(double BTU);
{
Cout<<BTU<< " cal is -----> "<<BTU*0.00116<<" kwh"<<endl;
}
}
)
Hocam koddaki hata sizin ifblokları içerisindeki fonksiyon çağırımlarınız.
Sizin yazdığınız kodda fonksiyonlar aslında hiç çağırılmıyor. Kod satır satır okunduktan sonra kullanıcının hangi seçeneği seçtiği fark etmeksizin alttaki tüm fonksiyonlara BTU değeri gönderilip çalıştırılıyor.
Şurada da bir mantık hatası var. if kalıbındaki son koyduğunuz "you have succesfully checked out" mantıksız olmuş. Çünkü eğer kullanıcı "1,2,3,4,5,6" haricinde bir değer girerse bu çalışacak. Hatalı giriş yapmasına rağmen başarılı kontrol ettiniz yazıyor.
Ek olarak üstteki fonksiyon tanımlamalarında da hata vardı. "caltokwh" olması gereken fonksiyon, "caltokhw" olarak yazılmıştı. Bu da mantık hatasına sürüklüyordu programı.
@LayThese 'in dediği gibi; bu tip programlarda if-else kalıbı yerine switch-case kalıbı kullanmak daha doğru. Hem verimlilik hem okunabilirlik hem de kolaylık bakımından.
Fonksiyonları ve kodu düzenledim. Şimdi dediğiniz gibi ve sıkıntısız çalışıyor.
C++:
#include <iostream>
using namespace std;
void btutocal(double btu);
void btutojoule(double btu);
void btutokwh(double btu);
void caltobtu(double btu);
void caltojoule(double btu);
void caltokwh(double btu);
int main()
{
double btu, cal, joule, final;
bool check = 0;
int option;
int num1;
cout << "\t\tWelcome to converter machine\n**********************************************************************" << endl;
cout << "\tEnter the desired converter option" << endl;
cout << "1-BTU to Calories\n**********\n2-BTU to Joules\n**********\n3-BTU to kWh\n**********\n4-Calories to BTU\n**********\n5-Calories to joules\n**********\n6-Calories to kHw\n**********\n7-Exit" << endl;
cout << "Please enter the option\n*******" << endl;
cin >> option;
switch (option)
{
case 1:
{
cout << "Please enter BTU-->";
cin >> btu;
btutocal(btu);
break;
}
case 2:
{
cout << "Please enter BTU -->";
cin >> btu;
btutojoule(btu);
break;
}
case 3:
{
cout << "Please enter BTU-->";
cin >> btu;
btutokwh(btu);
break;
}
case 4:
{
cout << "Please enter calories-->";
cin >> btu;
caltobtu(btu);
break;
}
case 5:
{
cout << "Please enter calories-->";
cin >> btu;
caltojoule(btu);
break;
}
case 6:
{
cout << "please enter calories-->";
cin >> btu;
caltokwh(btu);
break;
}
default:
cout << "You have succesfully checked out\n******************************" << endl;
}
}
void btutocal(double btu)
{
cout << btu << " BTU is -----> " << btu * 252 << " calories" << endl;
}
void btutojoule(double btu)
{
cout << btu << " BTU is -----> " << btu * 1054 << " joules" << endl;
}
void btutokwh(double btu)
{
cout << btu << " BTU is -----> " << btu * 0.0002 << " kwh" << endl;
}
void caltojoule(double btu)
{
cout << btu << " cal is -----> " << btu * 4.18 << " joules" << endl;
}
void caltobtu(double btu)
{
cout << btu << " cal is -----> " << btu * 0.00397 << " BTU" << endl;
}
void caltokwh(double btu)
{
cout << btu << " cal is -----> " << btu * 0.00116 << " kwh" << endl;
}
Kodda birçok yazım hatası mevcut. "cout, void, cin, bool, int, double, if, else vb." terimlerin baş harfleri büyük yazıldığı için VS kodu tanımıyordu. Kodu düzenledim, şu an çalışıyor.
Kodda birçok yazım hatası mevcut. "cout, void, cin, bool, int, double, if, else vb." terimlerin baş harfleri büyük yazıldığı için VS kodu tanımıyordu. Kodu düzenledim, şu an çalışıyor.
Hocam sanırım editör düzeltme yapmış. Evet kodlar çalışıyor fakat ben 1 tanesinin hesaplanmasını istiyorum yani altta tekrar hepsi hesaplanıp çıkıyor onu düzeltmem mümkün müdür?
Resimde Cal veya seçeneğe göre BTU istiyor bizden. Fakat hesaplamayı en altta gördüğümüz gibi her şey ile yapıyor. 5 seçeneği tek Cal to joule olmasına rağmen BTU olanları da tekrar hesaplamış.
Hocam koddaki hata sizin ifblokları içerisindeki fonksiyon çağırımlarınız.
Sizin yazdığınız kodda fonksiyonlar aslında hiç çağırılmıyor. Kod satır satır okunduktan sonra kullanıcının hangi seçeneği seçtiği fark etmeksizin alttaki tüm fonksiyonlara BTU değeri gönderilip çalıştırılıyor.
Şurada da bir mantık hatası var. if kalıbındaki son koyduğunuz "you have succesfully checked out" mantıksız olmuş. Çünkü eğer kullanıcı "1,2,3,4,5,6" haricinde bir değer girerse bu çalışacak. Hatalı giriş yapmasına rağmen başarılı kontrol ettiniz yazıyor.
Ek olarak üstteki fonksiyon tanımlamalarında da hata vardı. "caltokwh" olması gereken fonksiyon, "caltokhw" olarak yazılmıştı. Bu da mantık hatasına sürüklüyordu programı.
@LayThese 'in dediği gibi; bu tip programlarda if-else kalıbı yerine switch-case kalıbı kullanmak daha doğru. Hem verimlilik hem okunabilirlik hem de kolaylık bakımından.
Fonksiyonları ve kodu düzenledim. Şimdi dediğiniz gibi ve sıkıntısız çalışıyor.
C++:
#include <iostream>
using namespace std;
void btutocal(double btu);
void btutojoule(double btu);
void btutokwh(double btu);
void caltobtu(double btu);
void caltojoule(double btu);
void caltokwh(double btu);
int main()
{
double btu, cal, joule, final;
bool check = 0;
int option;
int num1;
cout << "\t\tWelcome to converter machine\n**********************************************************************" << endl;
cout << "\tEnter the desired converter option" << endl;
cout << "1-BTU to Calories\n**********\n2-BTU to Joules\n**********\n3-BTU to kWh\n**********\n4-Calories to BTU\n**********\n5-Calories to joules\n**********\n6-Calories to kHw\n**********\n7-Exit" << endl;
cout << "Please enter the option\n*******" << endl;
cin >> option;
switch (option)
{
case 1:
{
cout << "Please enter BTU-->";
cin >> btu;
btutocal(btu);
break;
}
case 2:
{
cout << "Please enter BTU -->";
cin >> btu;
btutojoule(btu);
break;
}
case 3:
{
cout << "Please enter BTU-->";
cin >> btu;
btutokwh(btu);
break;
}
case 4:
{
cout << "Please enter calories-->";
cin >> btu;
caltobtu(btu);
break;
}
case 5:
{
cout << "Please enter calories-->";
cin >> btu;
caltojoule(btu);
break;
}
case 6:
{
cout << "please enter calories-->";
cin >> btu;
caltokwh(btu);
break;
}
default:
cout << "You have succesfully checked out\n******************************" << endl;
}
}
void btutocal(double btu)
{
cout << btu << " BTU is -----> " << btu * 252 << " calories" << endl;
}
void btutojoule(double btu)
{
cout << btu << " BTU is -----> " << btu * 1054 << " joules" << endl;
}
void btutokwh(double btu)
{
cout << btu << " BTU is -----> " << btu * 0.0002 << " kwh" << endl;
}
void caltojoule(double btu)
{
cout << btu << " cal is -----> " << btu * 4.18 << " joules" << endl;
}
void caltobtu(double btu)
{
cout << btu << " cal is -----> " << btu * 0.00397 << " BTU" << endl;
}
void caltokwh(double btu)
{
cout << btu << " cal is -----> " << btu * 0.00116 << " kwh" << endl;
}
Hocam koddaki hata sizin ifblokları içerisindeki fonksiyon çağırımlarınız.
Sizin yazdığınız kodda fonksiyonlar aslında hiç çağırılmıyor. Kod satır satır okunduktan sonra kullanıcının hangi seçeneği seçtiği fark etmeksizin alttaki tüm fonksiyonlara BTU değeri gönderilip çalıştırılıyor.
Şurada da bir mantık hatası var. if kalıbındaki son koyduğunuz "you have succesfully checked out" mantıksız olmuş. Çünkü eğer kullanıcı "1,2,3,4,5,6" haricinde bir değer girerse bu çalışacak. Hatalı giriş yapmasına rağmen başarılı kontrol ettiniz yazıyor.
Ek olarak üstteki fonksiyon tanımlamalarında da hata vardı. "caltokwh" olması gereken fonksiyon, "caltokhw" olarak yazılmıştı. Bu da mantık hatasına sürüklüyordu programı.
@LayThese 'in dediği gibi; bu tip programlarda if-else kalıbı yerine switch-case kalıbı kullanmak daha doğru. Hem verimlilik hem okunabilirlik hem de kolaylık bakımından.
Fonksiyonları ve kodu düzenledim. Şimdi dediğiniz gibi ve sıkıntısız çalışıyor.
C++:
#include <iostream>
using namespace std;
void btutocal(double btu);
void btutojoule(double btu);
void btutokwh(double btu);
void caltobtu(double btu);
void caltojoule(double btu);
void caltokwh(double btu);
int main()
{
double btu, cal, joule, final;
bool check = 0;
int option;
int num1;
cout << "\t\tWelcome to converter machine\n**********************************************************************" << endl;
cout << "\tEnter the desired converter option" << endl;
cout << "1-BTU to Calories\n**********\n2-BTU to Joules\n**********\n3-BTU to kWh\n**********\n4-Calories to BTU\n**********\n5-Calories to joules\n**********\n6-Calories to kHw\n**********\n7-Exit" << endl;
cout << "Please enter the option\n*******" << endl;
cin >> option;
switch (option)
{
case 1:
{
cout << "Please enter BTU-->";
cin >> btu;
btutocal(btu);
break;
}
case 2:
{
cout << "Please enter BTU -->";
cin >> btu;
btutojoule(btu);
break;
}
case 3:
{
cout << "Please enter BTU-->";
cin >> btu;
btutokwh(btu);
break;
}
case 4:
{
cout << "Please enter calories-->";
cin >> btu;
caltobtu(btu);
break;
}
case 5:
{
cout << "Please enter calories-->";
cin >> btu;
caltojoule(btu);
break;
}
case 6:
{
cout << "please enter calories-->";
cin >> btu;
caltokwh(btu);
break;
}
default:
cout << "You have succesfully checked out\n******************************" << endl;
}
}
void btutocal(double btu)
{
cout << btu << " BTU is -----> " << btu * 252 << " calories" << endl;
}
void btutojoule(double btu)
{
cout << btu << " BTU is -----> " << btu * 1054 << " joules" << endl;
}
void btutokwh(double btu)
{
cout << btu << " BTU is -----> " << btu * 0.0002 << " kwh" << endl;
}
void caltojoule(double btu)
{
cout << btu << " cal is -----> " << btu * 4.18 << " joules" << endl;
}
void caltobtu(double btu)
{
cout << btu << " cal is -----> " << btu * 0.00397 << " BTU" << endl;
}
void caltokwh(double btu)
{
cout << btu << " cal is -----> " << btu * 0.00116 << " kwh" << endl;
}