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;
-
}
6 2389
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.
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)
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.
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.
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.
I also notice the line comments are \\ instead of // which meams this code has never compiled.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
--------------------------------------------------------------------------------
...
|
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...
|
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 `'}''...
|
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...
| |
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...
|
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...
|
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;...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |