#include <iostream>
#include <string>
usingnamespace std;
class convert {
public:
float k_to_m(float km){
float m;
m = km*1000;
return m;
}
float m_to_km(float m){
float km;
km = m/1000;
return km;
}
void log(){
string decision;
cout <<"choose by write (km to m) or (m to km)"<<endl;
getline(cin,decision );
/*
it;s for the decision
*/
if(decision == "kmtom"){
// the ends
float a;
cout <<"enter the nos "<<endl;
cin >>a;
cout <<"the nos is "<<k_to_m(a)<<endl;
cout<<"-------------------------------------------"<<endl;
}
elseif(decision == "m to km"){
float a;
cout <<"enter the nos "<<endl;
cin >>a;
cout <<"the nos is "<<m_to_km(a)<<endl;
cout<<"-------------------------------------------"<<endl;}
else {
cout <<"wrong"<<endl<<"-------------------------------------------"<<endl;
}
system ("pause");
}
};
int main(){
for ( ; ;) {convert enter;
enter .log();}
}
Did you type it right?
> if (decision == "kmtom") {
> else if (decision == "m to km") {
If you don't get the spacing right, it won't work.
You also need to be wary of mixing getline() and >> operators on the same stream. The stream extractors typically leave the '\n' untouched on the stream, and this will cause getline() to apparently return earlier than expected. For this, we use cin.ignore()
You got a main function with return type integer, but didn't give a return?
Uniquely, if control reaches the end of the global main function without executing an explicit return statement, the behavior is equivalent to return 0.
This is a "feature" inherited from C. I don't know why it was added in the first place.