다음은 올바른 코드입니다.
bool isValidUnit(const std::string &unit);
int convertValue(double value, const std::string &unitFrom, const std::string &unitTo);
int main() {
...
std::string storeFROM, storeTO;
cout << "Enter the initial unit (mm, cm, m, or km): ";
cin >> storeFROM;
if (!isValidUnit(storeFROM)) {
cout << "--> Sorry unit to convert FROM is invalid" << endl;
...
}
cout << "Enter the initial unit (mm, cm, m, or km): ";
cin >> storeTO;
if (!isValidUnit(storeTO)) {
cout << "--> Sorry unit to convert TO is invalid" << endl;
...
}
double value;
cout << "Enter the value in (" << storeFROM << "): ";
cin >> value;
double valueConverted = convertValue(value, storeFROM, storeTO);
cout << "Value in (" << storeTO << "): " << valueConverted << endl;
return 0;
}
bool isValidUnit(const std::string &unit) {
return unit == "mm" || unit == "cm" || unit == "m" || unit == "km";
}
double unitMultiplier(const std::string &unit) {
if (unit == "mm") return 0.001;
if (unit == "cm") return 0.01;
if (unit == "m") return 1;
if (unit == "km") return 1000;
return 0.;
}
double convertValue(double value, const std::string &unitFrom, const std::string &unitTo) {
if (unitFrom == unitTo) return value;
if (value <= 0.) return 0.; // or value :)
// Get it in meters
int valueInMetes = value * unitMultiplier(unitFrom);
return valuesInMeter / unitMultiplier(unitTo);
}
-------------------프로그램에서 storeFROM
이고 storeTO
둘 다 string
이므로 string
s 로 선언해야합니다 .
string storeFROM, storeTO;
같은 값을 비교할 때 "cm"
, "mm"
등, 당신은 그들을 인용해야 ""
하지, ''
당신은 확실히 그들이되고 싶어하지 않는 위해 다중 리터럴 . 다음과 같이 표시되어야합니다.
if ((storeFROM != "mm") || storeFROM != "cm"
|| storeFROM != "m" || storeFROM != "km")
{
cout << "--> Sorry unit to convert FROM is invalid" << endl;
}
else if ((storeFROM == "mm") || storeFROM == "cm"
|| storeFROM == "m" || storeFROM == "km")
{
cout << "Enter the initial unit(mm, cm, m, or kk) :";
cin >> storeTO;
}
-------------------int 유형의 객체에 대해 'm'문자와 같이 허용되지 않는 기호를 입력 할 수 없습니다.
이 코드를 프로그램의 템플릿으로 사용할 수 있습니다.
#include <iostream>
#include <string>
#include <initializer_list>
#include <algorithm>
#include <cstdlib>
std::ostream & display_meazures( const std::initializer_list<const char *> &l,
std::ostream &os = std::cout )
{
auto it = l.begin();
for ( size_t i = 0; i < l.size(); i++ )
{
if ( i == l.size() - 1 ) os << "or ";
os << *it;
if ( i != l.size() - 1 ) os << ", ";
}
return os;
}
int main()
{
std::string storeFROM, storeTO;
std::initializer_list<const char *> meazure = { "mm", "cm", "m", "km" };
while ( true )
{
std::cout << "Enter the initial unit (" << display_meazures( meazure )
<< "): ";
std::cin >> storeFROM;
if ( std::find( meazure.begin(), meazure.end(), storeFROM ) !=
meazure.end() )
{
break;
}
std::cout << "--> Sorry unit to convert FROM is invalid" << std::endl;
std::cout << std::endl;
}
// Calculate the selected units
std::system( "pause" );
}
-------------------string storeFROM, storeTO; // note string thingie
float value1, value2;
cin>>storeFROM;
if ((storeFROM != "mm") || storeFROM != "cm" || storeFROM != "m" || storeFROM != "km") )
{
cout << "--> Sorry unit to convert FROM is invalid" << endl;
}
else
{
cin>>value1;
cout << "Enter the initial unit(mm, cm, m, or kk) :";
cin >> storeTO;
if((storeTO == "mm") || storeTO == "cm" || storeTO == "m" || storeTO == "km"))
{
cin>>value2;
//convert here...
//hint: use switch-case
}
}
//end programme
여기...
출처
https://stackoverflow.com/questions/22050253