我正在编写一个简单的c ++测量程序,要求用户输入选择他们要从哪个单元测量单元的单位?但我不知道结构是否正确(我的C ++真的很糟糕)。这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int storeFROM, storeTO;
char mm, cm, m, kk;
cout << "Enter the initial unit (mm, cm, m, or km): ";
cin >> storeFROM;
cout << endl;
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;
}
// Calculate the selected units
system("pause");
}
我希望有人可以帮我询问用户输入,询问他们想要哪个单位,这正是它应该如何显示:
答案 0 :(得分:2)
这是正确的代码:
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);
}
答案 1 :(得分:1)
在您的计划中,storeFROM
和storeTO
都是string
,因此您应将其声明为string
s:
string storeFROM, storeTO;
在将它们与"cm"
,"mm"
等值进行比较时,您应该在""
中引用它们,而不是''
,因为您绝对不希望它们是multicharacter literals。它应该是这样的:
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;
}
答案 2 :(得分:0)
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
这里...
答案 3 :(得分:0)
对于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" );
}