473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dividing two integers will return an integer value, what's wrong

So I have a simple calculator that does a few operations (+ - * / %) Pretty basic stuff

i declared int x, y for the numbers, char operation, and float result.

the code is based on switch(operation)

The program is running alright, but when I divide 8/7 it returns 1 as the result, I tried changing the x and y to float but that won't work because of the case '%'

I also tried making local float variables in the case'/' but it won't compile "E2126 Case bypasses initialization of a local variable"

How can I make the division work and return a float value?
Dec 2 '14 #1

✓ answered by weaknessforcats

8/7 is 1.

There is only one 7 in 8.

Essentially, if your numbers are char, short, int they are converted to int, the operation is done as int, and the result is int.

Having a float as a result does no good since the operation is already done before the int result is assigned to the float.

You can use 8.0/7.0 because here the numbers are double, the operation is done using double, and the result is double. Now you would see the decimal portion.

3 1617
weaknessforcats
9,208 Expert Mod 8TB
8/7 is 1.

There is only one 7 in 8.

Essentially, if your numbers are char, short, int they are converted to int, the operation is done as int, and the result is int.

Having a float as a result does no good since the operation is already done before the int result is assigned to the float.

You can use 8.0/7.0 because here the numbers are double, the operation is done using double, and the result is double. Now you would see the decimal portion.
Dec 2 '14 #2
Oh! I get it now, thank you! but how can I make them doubles while I have a case of doing the remaining of a division operation (%)? I tried to solve this problem by making local variables in the division case, it returns with an Error "E2126 Case bypasses initialization of a local variable" :\ Should I use if instead of switch?
Dec 2 '14 #3
Alright thank you, I got it done by putting { } for the body of case '/' Thanks alot! :)
Dec 2 '14 #4

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

Similar topics

62
by: TheShadow1 | last post by:
safetyTips - this array is in here.js ...
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
6
by: Simon Mansfield | last post by:
Im trying to make a C program that takes in a date (birthday) and tells the user how many days it has been since that date. So far I have got this... It compiles ok but then crashes, with no idea...
5
by: Robert Wilson | last post by:
Hi, I'm a little new to C#, so bear with me. I have a text box on a form, and I want to be able to get an integer value of whats in it. In VC++ you just set the text box property to be numerals...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
7
by: Goldie | last post by:
Can some please look at this function and maybe help me with why it's not doing as it's told... :) It's basicall meant to check to see if that field returned from database actually has any data...
0
by: Kim | last post by:
Im having a problem with one of my datagrids. Basically the datagrid has paging included in it and when a user clicks on page other than the first page and the user selects a row the value is...
3
by: mikerich135 | last post by:
Is it possible that any function without return type and returning value will return any value. If so, How?
0
by: mylife smile | last post by:
Hi all, I have to return an integer value from vc++ to c# wcf. In vc++ the function works fine but in c# the function returns wrong integer value. In vc++ typedef struct { BOOL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.