Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void printnumber(int x){
  5. cout << "I am printing an integer " <<x << endl;
  6. }
  7.  
  8. void printnumber(float x){
  9. cout << "I am printing an float: " << x << endl;
  10. }
  11.  
  12.  
  13. int main(){
  14.  
  15. int a = 54;
  16. float b= 32.4896;
  17.  
  18. printnumber(a);
  19. printnumber(b);
  20.  
  21. }
  22.  
  23. #include <iostream>
  24. using namespace std;
  25. int a;
  26. float b;
  27.  
  28. void printnumber(int x){
  29. cout << "I am printing an integer " <<x << endl;
  30. }
  31.  
  32. void printnumber(float x){
  33. cout << "I am printing an float: " << x << endl;
  34. }
  35.  
  36.  
  37. int main(){
  38.  
  39. cout << "enter a whole number or a number with decimal: ";
  40. cin >> ; //what will i put here?
  41.  
  42. printnumber(a);
  43. printnumber(b);
  44.  
  45. }
  46.  
  47. int a;
  48. std::cout << "Enter an integer value: ";
  49. std::cin >> a;
  50.  
  51. float b;
  52. std::cout << "Enter a floating point value: ";
  53. std::cin >> b;
  54.  
  55. std::string user_input;
  56.  
  57. cout << "enter a whole number or a number with decimal: ";
  58. cin >> user_input;
  59.  
  60. if(user_input.find('.') != std::string::npos){
  61. // we found a float (probably)
  62. // uses boost library to make stuff easier
  63. printNumber(boost::lexical_cast<float>(user_input));
  64. }else{
  65. printNumber(boost::lexical_cast<int>(user_input));
  66. }
  67.  
  68. std::stringstream ss;
  69. ss << user_input;
  70. if(user_input.find('.') != std::string::npos){
  71. // we found a float (probably)
  72. float f;
  73. ss >> f;
  74. printNumber(f);
  75. }else{
  76. int i;
  77. ss >> i;
  78. printNumber(i);
  79. }
  80.  
  81. int main() {
  82. std::string value;
  83. std::cin >> value;
  84.  
  85. std::size_t end;
  86. int intval = std::stoi(value, &end);
  87. if (end == value.size())
  88. printnumber(intval);
  89. else
  90. printnumber(std::stof(value));
  91. }
  92.  
  93. struct printer : boost::static_visitor<void> {
  94. template <typename T>
  95. void operator ()(T const& value) const { printnumber(value); }
  96. };
  97.  
  98. int main() {
  99. std::string value;
  100. std::cin >> value;
  101.  
  102. namespace qi = boost::spirit::qi;
  103.  
  104. auto begin = std::begin(value);
  105. auto const end = std::end(value);
  106. boost::variant<int, float> result;
  107. bool success = qi::parse(begin, end, (qi::int_ >> !qi::lit('.')) | qi::float_, result);
  108. if (not success or begin != end)
  109. std::cerr << "Something went wrongn";
  110. else
  111. apply_visitor(printer{}, result);
  112. }
  113.  
  114. int main(){
  115.  
  116. string a;
  117. cout << "enter a whole number or a number with decimal: ";
  118. cin >> a;
  119.  
  120. printnumber((int)a);
  121. printnumber((float)a);
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement