Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void printnumber(int x){
- cout << "I am printing an integer " <<x << endl;
- }
- void printnumber(float x){
- cout << "I am printing an float: " << x << endl;
- }
- int main(){
- int a = 54;
- float b= 32.4896;
- printnumber(a);
- printnumber(b);
- }
- #include <iostream>
- using namespace std;
- int a;
- float b;
- void printnumber(int x){
- cout << "I am printing an integer " <<x << endl;
- }
- void printnumber(float x){
- cout << "I am printing an float: " << x << endl;
- }
- int main(){
- cout << "enter a whole number or a number with decimal: ";
- cin >> ; //what will i put here?
- printnumber(a);
- printnumber(b);
- }
- int a;
- std::cout << "Enter an integer value: ";
- std::cin >> a;
- float b;
- std::cout << "Enter a floating point value: ";
- std::cin >> b;
- std::string user_input;
- cout << "enter a whole number or a number with decimal: ";
- cin >> user_input;
- if(user_input.find('.') != std::string::npos){
- // we found a float (probably)
- // uses boost library to make stuff easier
- printNumber(boost::lexical_cast<float>(user_input));
- }else{
- printNumber(boost::lexical_cast<int>(user_input));
- }
- std::stringstream ss;
- ss << user_input;
- if(user_input.find('.') != std::string::npos){
- // we found a float (probably)
- float f;
- ss >> f;
- printNumber(f);
- }else{
- int i;
- ss >> i;
- printNumber(i);
- }
- int main() {
- std::string value;
- std::cin >> value;
- std::size_t end;
- int intval = std::stoi(value, &end);
- if (end == value.size())
- printnumber(intval);
- else
- printnumber(std::stof(value));
- }
- struct printer : boost::static_visitor<void> {
- template <typename T>
- void operator ()(T const& value) const { printnumber(value); }
- };
- int main() {
- std::string value;
- std::cin >> value;
- namespace qi = boost::spirit::qi;
- auto begin = std::begin(value);
- auto const end = std::end(value);
- boost::variant<int, float> result;
- bool success = qi::parse(begin, end, (qi::int_ >> !qi::lit('.')) | qi::float_, result);
- if (not success or begin != end)
- std::cerr << "Something went wrongn";
- else
- apply_visitor(printer{}, result);
- }
- int main(){
- string a;
- cout << "enter a whole number or a number with decimal: ";
- cin >> a;
- printnumber((int)a);
- printnumber((float)a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement