如何如果用户输入匹配特定值?

我正在写一个简单的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");

}

我希望有人能帮助我,要求用户填写他们想哪个单位来问,这正是它应该如何显示:

回答:

下面是正确的代码:

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);

}

回答:

在程序,storeFROMstoreTO都是string S,所以你应该把他们定义为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;

}

回答:

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

这里...

回答:

您不能输入非接受的符号,例如字母'm'表示int类型的对象。

您可以将此代码用作程序的模板。

#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");

}

以上是 如何如果用户输入匹配特定值? 的全部内容, 来源链接: utcz.com/qa/259381.html

回到顶部