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 2393
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 using the
Debug-Version of the program, but it fails (or leads to false
results) if we are using the Release-Version. After a long
debugging...
|
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
--------------------------------------------------------------------------------
Hello,
I have a very simple problem but cannot seem to figure it out. I have a
very simple php script that sends a test email to myself. When I...
|
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 and see what happens if you then keep entering .01 until you
have added the full .85. The program still asks the user to deposit
$0.00. It is...
|
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 `'}''
'else if (choice == 27) exit(0) } }'
Here is my program (Simple loop to display currency equivalencies based
|
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 the EPSILON? I know in float.h a FLT_EPSILON is defined to be
10^-5. Does this mean that the computer cannot distinguish between 2
numbers that...
| |
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 describing here.
========================================================
#include<stdio.h>
#include<stdlib.h>
|
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 integer, and a character. Each of these inputs should be preceded by a prompt and stored using individual variable names. Have your program call a...
|
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;
printf("Please enter a number: ");
scanf("%d", &num); // yes I know this function may throw a
|
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 thought I
would go a bit above and beyond the scope of the class and use
strings. But I ran into a snag with my getgrades function. The
compiler gives me...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |