473,500 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with input to floating point values

23 New Member
I'm making a calculator that is Supposed to calculate floating point values, but somethign goes wrong along the way (can't be sure what). It calculates intagers just fine, but when I add a decimal with an extra intager on, something funky happens. It infinitly displays a rounded number. For instance, if I type in "3+1.3", instead of displaying "4.3" once like it should, it will just display "4" line by line infinatly over the time span that I don't hit the X at the top right. Can someone perhaps tell me a solution to this? If it will help, I have the code right here:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()    \\calculator function
  4. {
  5.    cout << "Press '*' to multiply two numbers (ex: 2*2 plus enter key)\n\n";      \\start of instructions
  6.    cout << "Press '/' to divide two numbers (ex: 2/2 plus enter key)\n\n";
  7.    cout << "Press '+' to add to two numbers (ex: 2+2 plus enter key)\n\n";
  8.    cout << "Press '-' to subtract a number from another(ex: 2-2 plus enter key)\n\n";    \\end of instructions
  9.    cout << "Press 'Q' or 'q' to exit\n\n";
  10.    char character;
  11.    while(character != 'Q')
  12.    {  long double value;
  13.       getline(cin, value, '*' || '/' || '+' || '-');
  14.       cin >> character;
  15.       long double value2;
  16.       getline(cin, value2)
  17.       if(character == '*')
  18.       {  cout << value * value2 << endl;}
  19.       if(character == '/')
  20.       {  cout << value / value2 << endl;}
  21.       if(character == '+')
  22.       {  cout << value + value2;}
  23.       if(character == '-')
  24.       {  cout << value - value2;}
  25.    }
  26.    return 0;
  27. }
Apr 10 '07 #1
6 2389
ilikepython
844 Recognized Expert Contributor
I'm making a calculator that is Supposed to calculate floating point values, but somethign goes wrong along the way (can't be sure what). It calculates intagers just fine, but when I add a decimal with an extra intager on, something funky happens. It infinitly displays a rounded number. For instance, if I type in "3+1.3", instead of displaying "4.3" once like it should, it will just display "4" line by line infinatly over the time span that I don't hit the X at the top right. Can someone perhaps tell me a solution to this? If it will help, I have the code right here:

#include <iostream>
using namespace std;
int main() \\calculator function
{
cout << "Press '*' to multiply two numbers (ex: 2*2 plus enter key)\n\n"; \\start of instructions
cout << "Press '/' to divide two numbers (ex: 2/2 plus enter key)\n\n";
cout << "Press '+' to add to two numbers (ex: 2+2 plus enter key)\n\n";
cout << "Press '-' to subtract a number from another(ex: 2-2 plus enter key)\n\n"; \\end of instructions
cout << "Press 'Q' or 'q' to exit\n\n";
char character;
while(character != 'Q')
{long double value;
getline(cin, value, '*' || '/' || '+' || '-');
cin >> character;
long double value2;
getline(cin, value2)
if(character == '*')
{cout << value * value2 << endl;}
if(character == '/')
{cout << value / value2 << endl;}
if(character == '+')
{cout << value + value2;}
if(character == '-')
{cout << value - value2;}
}
return 0;
}
I'm not sure but I believe it is because you are adding(or anything else) an integer with a float. The program adds 1 and 3 (4) and then doesn't know what to do with the 0.3. You should probably make both numbers floats. Again, I'm not sure but I think that is the problem.
Apr 10 '07 #2
massdeletion101
23 New Member
I'm not sure but I believe it is because you are adding(or anything else) an integer with a float. The program adds 1 and 3 (4) and then doesn't know what to do with the 0.3. You should probably make both numbers floats. Again, I'm not sure but I think that is the problem.
Actually, I'm adding two variables of the same type. Looking at the code, both values are of type long double. I tried what you said though, despite thinking you were wrong. It still doesn't work right sadly.:( However, there is a different outcome, it just displays nothing. (but that's because I put two numbers with deimal values in them instead of just one with a whole number, I typed 1.5+1.5 and I got nothing)
Apr 10 '07 #3
ilikepython
844 Recognized Expert Contributor
Actually, I'm adding two variables of the same type. Looking at the code, both values are of type long double. I tried what you said though, despite thinking you were wrong. It still doesn't work right sadly.:( However, there is a different outcome, it just displays nothing. (but that's because I put two numbers with deimal values in them instead of just one with a whole number, I typed 1.5+1.5 and I got nothing)
Ok, I studied it and I still can't figure something. I'm really sorry I couldn't help. Maybe somebody else can.
Apr 10 '07 #4
massdeletion101
23 New Member
Ok, I studied it and I still can't figure something. I'm really sorry I couldn't help. Maybe somebody else can.
That's okay. Trying is enough. I might contact someone else about the problem.
Apr 10 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Are you using some different getline() function than I am? I didn't know you could use getline to get double values, or even integer values. I would get the entire user input as a string and try to get the numbers and operator for that instead of what you are doing.
Apr 11 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
I also notice the line comments are \\ instead of // which meams this code has never compiled.
Apr 11 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3726
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
8
5448
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
1727
by: Jeffrey Barrett | last post by:
Can someone tell me why I'm having this problem: When I select drink 5 and deposit too little money, there's a problem with the 'difference' variable. Try to deposit $.80 instead of the full $.85...
8
5364
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
15
3893
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
7
2186
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am...
2
3943
by: karafire2003 | last post by:
I've been tasked to do 2 questions. I think i got the majority of it done, but i'm having trouble. Question #1: Write a C program that accepts as input from the keyboard a floating point number, an...
14
2380
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
5
1822
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I...
0
7183
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7235
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3108
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1431
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
317
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.