大佬教程收集整理的这篇文章主要介绍了c – 将价格(例如89.95美元)读入双倍,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何让它忽略’$’?
Introduction to progrAMMing languages 1 $89.99 100
到目前为止,这是我的代码:
#include <iostream> #include <fstream> #include <String> #include <cstdlib> using namespace std; int main() { char title[50]; char strBookid[10]; char strPrice[10]; char stramount[10]; int bookId; double price; int amount; ifstream filein("bookfile.txt"); filein.getline(title,50); cout << "title : " << title << endl; filein.getline(strBookid,10); cout << "BookId as a String : " << strBookid << endl; filein.getline(strPrice,10); cout << "Price as a String : " << strPrice << endl; filein.getline(stramount,10); cout << "Qty as a String: " << stramount << endl; bookId = std::atoi(strBookid); cout << "The Book ID as an int : " << bookId << endl; price = std::atof(strPricE); cout << "The price as a double : " << price << endl; return 0; }
你可以这样做:
#include <iostream> #include <fstream> #include <String> #include <cstdlib> #include <locale> //add this using namespace std; int main() { char title[50]; char strBookid[10]; char strPrice[10]; char stramount[10]; int bookId; long double price; //changed! get_money only supports long double int amount; ifstream filein("bookfile.txt"); filein.getline(title,10); cout << "BookId as a String : " << strBookid << endl; filein.imbue(std::locale("en_US.UTF-8")); /// added filein >> std::get_money(pricE); ///changed price /= 100; //get_money uses the loWest denomination,in this case cents,so we convert it $by dividing the value by 100 cout << "Price as a String : $" << price << endl; ///changed filein.getline(stramount,10); cout << "Qty as a String: " << stramount << endl; bookId = std::atoi(strBookid); cout << "The Book ID as an int : " << bookId << endl; price = std::atof(strPricE); cout << "The price as a double : " << price << endl; return 0; }
作为第二种选择,您可以修改原始代码以手动测试$sign …(请参阅下面的代码段
......many lines skipped ........... bookId = std::atoi(strBookid); cout << "The Book ID as an int : " << bookId << endl; price = std::atof(strPrice[0] == '$' ? strPrice+1 : strPrice ); //modified cout << "The price as a double : " << price << endl; return 0; }
以上是大佬教程为你收集整理的c – 将价格(例如89.95美元)读入双倍全部内容,希望文章能够帮你解决c – 将价格(例如89.95美元)读入双倍所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。