-5

I am self teaching my self teaching myself C++

but I am having a trouble make a simple input/output calculator.

Here is what I have come up with:

#include <iostream>

using namespace std;

int main()
{
    cout << "THIS IS TEST CALCULATOR!" << endl;

    int fn,sn,dvi;                                        //fn- FIRST NUMBER, sn - SECOND NUMBER
    cout << "Please type in the first number: " << endl;
    cin >> fn;


    cout << "Please type in the second number: " << endl;
    cin >> sn;


    cout << "Please choose type ( type in one: division, multiplication, subtraction, addition or modulus): "<< endl;   //Asks for the type on calculation; needs to type in one.
    std::string tp;
    cin>>tp;


    dvi = (fn/sn); // division

    if (tp == "division"){
        cout<<fn<<" divide by "<<sn<<" is "<<dvi<< endl;
    }
}

result: enter image description here

8/6 = 1.333333... not 1

Music Watt
  • 23
  • 2
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Feb 11 '17 at 18:57
  • 2
    `float dvi;` & `dvi = (fn/sn);` -> `dvi = (float)fn/sn;` – George Feb 11 '17 at 18:58
  • 2
    A debugger will not help here. You need to learn the difference between int and float. – koalo Feb 11 '17 at 19:00
  • This isn't the problem, but don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. And since `std::cout` and `std::cin` are synchronized, there's no need for a flush or even a newline when you prompt for input. `std::cout << "Please type in the first number: "; std::cin >> fn;` works just fine. – Pete Becker Feb 11 '17 at 19:00
  • If you're "self teaching" yourself C++, then hopefully you've found a good book or educational resource to follow. And hopefully that would explain integer division as well as all the other gotchas you're going to encounter in C++. But if you don't have such a resource, then you really need to find one. – TheUndeadFish Feb 11 '17 at 20:08

5 Answers5

2

You are performing integer division, and therefore 8/6 is 1. If you want floating point arithmetic, you could change fn,sn and dvi to float instead of int, or you could simply cast one or both of the arguments to a float (or double).

See this question for more in-depth answers.

uint128_t
  • 335
  • 2
  • 10
  • Wow, mad downvoter. Upvoted just to offset that silliness. – Pete Becker Feb 11 '17 at 19:01
  • Edit your question. I was the downvoter by mistake. On iPad it is sometimes a challenge to hit the proper arrow. I can't undo the vote until you edited your question. Write me when you edited it and I correct my vote – Peter VARGA Feb 11 '17 at 19:48
1

Since the decalred variable type is int you will always get the floor of number.

All three should be of type float or double. Else dvi can be of this type and typecast the fn and sn to same type (float/double) during division.

Shravan40
  • 8,922
  • 6
  • 28
  • 48
  • @PeteBecker : all three should be of type `float` or `double`. Else `dvi` can be of this type and typecast the `fn` and `sn` during division. – Shravan40 Feb 11 '17 at 19:03
0

When dividing integers you get integer values not decimal ones. To solve your problem you either declare them float or double or cast the operation.

double dvi = ((double)fn / (double)sn); // division
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
0

Instead of declaring fn,sn,dvi as ints, you should declare them as doubles:

double fn,sn,dvi; Otherwise, dividing integers by integers in C++ will truncate the numbers to a smaller whole number. For example: 5/3 would equal 1, even though in reality it equals 1.6667. C++ will not round up to 2.0, it will round down to 1.

Another workaround for this issue if you're not using variables is to add the decimal to whole numbers so that the compiler recognizes them as doubles. For example: 5.0/2.0 = 2.5

Josh Simani
  • 107
  • 10
0

The problem in your code is that you are dividing 2 variables that are of type int, and storing it in a variable that is also of type int. All three variables:

fn

sn

dvi

are of type int in your code. An int variable only stores a whole integer number, ranging from positive infinity to negative infinity. This means that when you divide 2 numbers that do not produce an integer, the floating point value is rounded off so that it can be stored in the int variable, which in your case is dvi. In addition, even if dvi alone is of data type float, your division will still round to a whole number as you have divided two variables of data type int, namely fn and sn, and in c++, when two integers are divided, the end result is always rounded off regardless of the data type of the variable it will be stored in.

The easiest way to get around the problem is to change the declaration of these variables to:

double fn, sn, dvi;

NOTE: I use double here, as it can store a more precise value since it has 8 bytes allocated to it in contrast to 4 for a float.

This is the easiest way to get around your problem. Alternatively, we can use casting in the mathematical step of your code; in that case we would declare the variables as:

int fn, sn;
double dvi;

Then in the step where you divide the numbers, you can do the following:

dvi = (double)(fn/sn);

This is called casting; the compiler here is "forced" to treat the end result of this division as a double. In your case it will work, as you are dividing numbers, however, as a side note, it cannot be done in all cases (you cannot cast a string as a float for example). Here is a link if you want to learn more about casting:

Typecasting in C and C++

A third way, simpler than casting would be to do the following:

dvi = (fn/sn)*1.0;

This can be done only if dvi is of type float or double. Here, the mathematical operation involves a float, so all values are not of type int. As a result, the precision of the value is preserved with a decimal point, in your case, it will store 1.33333333333... instead of 1.

Sorry for the late reply, I couldn't get time to answer your question yesterday.

Community
  • 1
  • 1
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31