如何在 C++ 中将双精度数组中的值"force convert"到字符串输出中?
How to "force convert" a value from an array of doubles into a string output in C++?
我目前正在研究这个程序。在输入国家名称后,程序将一个国家的粗粮总产量输出为双倍值。我正在使用字符串数组和双精度数组。该程序完美运行和执行:
int main ()
{
string countries[18] = {"World", "Total foreign", "United States", "Canada", "Mexico"};
double grainsProduction[18] = {1361.4, 977.0, 384.4, 26.2, 33.2}
string countryChoice = " ";
int sub = 0;
cout << "Enter country name: ";
getline (cin, countryChoice);
while(sub < 5 && countries[sub] != countryChoice)
{
sub += 1;
}
if (sub < 5)
{
cout << "" << countries[sub] << "'s coarse grain production for 2017/2018 was: "
<< grainsProduction[sub] << " million metric tons." << endl;
}
else
{
cout << "Invalid country name." << endl;
}
}
但是,我想在国家/地区数组中包括另一个国家/地区。输入此国家/地区名称时,应输出"未报告"语句。
我想在双精度数组中添加一个虚拟值,如果输入特定国家/地区,它将强制转换为字符串消息"未报告"。如何做到这一点?
也许当您输入国家/地区名称时,您可以使用此代码检查数组中是否有具有该名称的国家/地区,以及是否不显示您的自定义消息。 下面是一个代码示例:
for(int x = 0; x < size; x++){
if (countries[x].find(countryChoice, 0) != std::string::npos){
cout << data[x] << endl;
}
else {
cout << "No data for this Country" << endl;
}
相关文章:
- C++Brute Force攻击函数不会返回结果
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 如何在 C++ 中将双精度数组中的值"force convert"到字符串输出中?
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 我一直收到错误"cannot convert 'float*' to 'float' in return"
- 如何在断言中'force' const 访问器的使用?
- 地图'Cannot convert from _Ty2 to T*'错误
- 将 2D 数组插入 std::vector 时"cannot convert from 'const GLfloat [12]' to '_Objty'"错误消息
- 如何修复C++中的'cannot convert char to char*'错误?
- 返回"Cannot convert from 'std::ofstream {aka std::basic_ofstream<char>}' to bool"错误
- 列表插入"Cannot convert argument from Class<T>* to Class<T> &&"
- "Cannot convert parameter 1 from double to float"虽然没有更改参数
- C++ 中的错误" cannot convert node to node* "
- "cannot convert argument 1 from 'Node<T>' to 'std::nullptr_t"编译器错误
- 撤消仪表convert依行动
- 'Convert' 5 个字符到整数
- 如何修复C++编译器错误"cannot convert 'Type' to 'const Type*'"?
- 我可以"force"线程唤醒吗?
- "could not convert template argument"到自己的类型
- 如何修复错误"could not convert <brace enclose initilizer list> to Node"