0

TaskI am new to C++ and I have small problem. I want to make program where I scan two numbers and get their sum: e.g. Input: 1 2 Output: 3

Input. 1.1 2.2 Output: 3.3

I struggle with switching between float or int. If I scan for int(%d) I can't get float variable, but if I scanf float then as 1st output I get 3.00 instead of 3. By this I want to achieve that decimal number will be in decimal form and number like 3 in its form without decimal zeros.

Thanks

Renato Rak
  • 13
  • 1
  • 6
  • 6
    First of all, what books or resources to learn C++ are you using? No [decent book](https://stackoverflow.com/a/388282/440558) should tell you to use `scanf`. Secondly, why not read only floating point numbers? Integers are just floating-point values without decimals. – Some programmer dude Oct 12 '21 at 16:15
  • 2
    "If I scan for int(%d) I can't get float variable, but if I scanf float then as 1st output I get 3.00 instead of 3." Please show a [mre] which demonstrates that. – Yunnosch Oct 12 '21 at 16:17
  • I uploaded a picture. The thing is that if I input 1 and 2 I get 3.00 but I want to get 3 without decimal. But, if I type 1.1 and 2.2 I want to get 3.3. I want to combine this. – Renato Rak Oct 12 '21 at 16:27
  • Don't post images of text, least of all code. Copy-paste text *as text* into your questions. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 12 '21 at 16:31
  • Also, there's nothing C++ specific in the code, it could be all plain C. – Some programmer dude Oct 12 '21 at 16:32
  • @RenatoRak What if the input is 1.1 and 3 , then what should be the output? Should it be 3.1 or 3 ? That is how will you decide whether the output should be float or int? – Jason Oct 12 '21 at 16:33
  • I'll guess you live in Brazil, and therefore have to type "1,1" instead of "1.1" – Hans Passant Oct 12 '21 at 16:53

2 Answers2

1

You can read the two numbers with std::cin, then output the sum with std::cout.

You can use std::fixed and std::setprecision to control the format of the output. Demo

#include <iomanip>  // setprecision
#include <ios>  // fixed
#include <iostream>  // cin, cout

int main()
{
    double d1{0.0};
    double d2{0.0};
    std::cin >> d1 >> d2;
    std::cout << std::fixed << std::setprecision(5) << d1 << " + " << d2 << " = " << d1 + d2 << "\n";
}

If you want to output decimals only when needed, just don't use fixed and precision. Demo

#include <iostream>  // cin, cout

int main()
{
    double d1{0.0};
    double d2{0.0};
    std::cin >> d1 >> d2;
    std::cout << d1 << " + " << d2 << " = " << d1 + d2 << "\n";

    double d3{0.0};
    double d4{0.0};
    std::cin >> d3 >> d4;
    std::cout << d3 << " + " << d4 << " = " << d3 + d4 << "\n";
}
rturrado
  • 7,699
  • 6
  • 42
  • 62
0

You can scan a std::string, check whether there is a dot in it using std::find and converting to an int (std::stoi) or a float (std::stof) accordingly. By the way, in C++, you should use std::cin instead of scanf.

TUI lover
  • 542
  • 4
  • 16