C++ rounds up my decimals

E

eli m

Hi I made this code:

#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
int getIntx()
unsigned int ans;
double mfirstnum, msecondnum;
cout << "Type in your first number:";
mfirstnum = getIntx();
cout << "Multiply your first number by:";
msecondnum = getIntx();
ans = mfirstnum * msecondnum;
cout << ans << "\n"; }
//Get int x
int getIntx() {
double x = 0;

while(!(cin >> x)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid Input. Try again:";
}
return x;
}

So, whenever I type in a decimal it rounds it up. Why is this happening?
 
I

Ian Collins

eli said:
Hi I made this code:

#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
int getIntx()
unsigned int ans;
double mfirstnum, msecondnum;
cout << "Type in your first number:";
mfirstnum = getIntx();
cout << "Multiply your first number by:";
msecondnum = getIntx();
ans = mfirstnum * msecondnum;
cout << ans << "\n"; }
//Get int x

White-space comes at no extra cost.
int getIntx() {
double x = 0;

while(!(cin >> x)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid Input. Try again:";
}
return x;
}

So, whenever I type in a decimal it rounds it up. Why is this happening?

Because your input function returns an int?
 
P

Paul N

White-space comes at no extra cost.




Because your input function returns an int?

I thought C++ tended to round things down rather than up?

Still, I'm more interested in how he got

to compile.
 
Ö

Öö Tiib

I thought C++ tended to round things down rather than up?

No. Conversion from double to int will truncate toward zero.
Rounding down is done with floor().

OP was either testing with negative values so saw round up
effect or was also puzzled with terminology.
Still, I'm more interested in how he got


to compile.

Hmm. But that is elementary, Watson.
 
B

Bill Gill

No. Conversion from double to int will truncate toward zero.
Rounding down is done with floor().
That came as a bit of surprise to me. I was always taught that
it truncated by dropping everything after the decimal point. Or
at least that was what I understood. If I had tried to convert
a negative double to int I would have had problems just like the
OP. I will have to remember that in the future.

Bill
 
B

Bill Gill

And the difference with "truncates towards zero" is what, exactly?

--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---
Ok, I see. Sometimes I get carried away with my own smarts.

I think I got confused because I never heard (or saw) it stated that
way before.

Bill
 
S

Stuart

Hmm. But that is elementary, Watson.

Err, would you mind giving an explanation to the not so enlightened, Mr.
Holmes? My compiler says "Expected ';' at end of declaration."

I'm pretty positive that this is either some funky compiler extension or
some left-over from C. Hopefully the first.

Thanks,
Stuart
 
Ö

Öö Tiib

Err, would you mind giving an explanation to the not so enlightened, Mr.
Holmes? My compiler says "Expected ';' at end of declaration."

Excellent! So what does a developer do when compiler tells him that ';' is
missing?
I'm pretty positive that this is either some funky compiler extension or
some left-over from C. Hopefully the first.

I think that he never compiled posted code. He pasted compiling code into
post and then edited it so a semicolon got accidentally removed.
 
S

Stefan Ram

Öö Tiib said:
Excellent! So what does a developer do when compiler tells him that ';' is
missing?

That does not explain how he got

int getIntx()
unsigned int ans;

(without that semicolon!) to compile. He claimed

»whenever I type in a decimal it rounds it up.«,

which implied that that code did compile as posted.
 
V

Victor Bazarov

That does not explain how he got

int getIntx()
unsigned int ans;

(without that semicolon!) to compile. He claimed

»whenever I type in a decimal it rounds it up.«,

which implied that that code did compile as posted.

It could be a severe case of "I type my code into newsreader instead of
copy-pasting" disorder...

V
 
S

Stuart

Excellent! So what does a developer do when compiler tells him that ';' is
missing?


I think that he never compiled posted code. He pasted compiling code into
post and then edited it so a semicolon got accidentally removed.

Oh dear me. I was hoping that this would turn out to be the perfect ...
err ... crime? Well, posting non-workable examples should certainly be
considered a crime.

Back to Baker Street, then!

Thanks,
Stuart (a.k.a. Watson in this thread ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top