473,396 Members | 1,968 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,396 software developers and data experts.

typeof

How do i get a cout to tell what type of answer i get in my calculations? ie., long, short, double etc.

I am using a division in my computation, but i need to know what type of integer it returns.
Sep 29 '08 #1
6 2460
Banfa
9,065 Expert Mod 8TB
The type of the result of a calculation can be determined from the types of the variables and constants used in the calculation. As such it is completely determinable by the program at the time they write the code.
Sep 29 '08 #2
whodgson
542 512MB
I think that you can find the size of all the primative types on your machine by
using the sizeof function. e.g.
Expand|Select|Wrap|Line Numbers
  1. cout<<"The size of a long type is "<<sizeof(long)<<endl;//usually 4?
Knowing this you can find the size of the output and therefore the type.
Sep 30 '08 #3
donbock
2,426 Expert 2GB
I think that you can find the size of all the primative types on your machine by
using the sizeof function. e.g.
Expand|Select|Wrap|Line Numbers
  1. cout<<"The size of a long type is "<<sizeof(long)<<endl;//usually 4?
Knowing this you can find the size of the output and therefore the type.
sizeof is ambiguous: for instance, it is not uncommon for sizeof(int) and sizeof(long) to both be 4.

Banfa is right. You can tell simply by inspecting the source code what the type of an expression will be. The rules for expressions are well-defined in the Standard. The type of an expression is established when you write the expression, not by the values that happen to be used when the expression is evaluated at run-time. ... At least, that's the case for C and C++.

Perhaps alsoneedhelp2 could post an example of what he's trying to do.
Sep 30 '08 #4
for example,

i wish to input 2 numbers, int1 and int2
then i wish to divide the 2 numbers to get a result. such as 5, 2. the computation normally would result in 2 and does not list the remainder. however, i need the remainder value to also be calculated and in addition to giving me the result, i need to have the program tell me if the result is long, or short, or double, or whatever.

#include <iostream>





int main()
{
int number1;
int number2;


std:cout << "Type your first number " ;
std::cin >> number1;

std:cout << "Type your second number " ;
std::cin >> number2;

std::cout <<number1 << " / " <<number2 << " = " << div << std::endl;

return 0;
}
Oct 1 '08 #5
manontheedge
175 100+
you can't get a different type by dividing two integers ... it's not possible

I was thinking what you are being asked to do, is find out what the remainder is and decide what data type ( float, double, long, ... ) would be needed to hold that number ... but if you can only use integers and not do some more math that doesn't make any sense

you can get the remainder by using the modulus operator, that --> %

but, basically, you're question doesn't make sense, is there more to it?
Oct 1 '08 #6
Banfa
9,065 Expert Mod 8TB
I need to have the program tell me if the result is long, or short, or double
But the result of the calculation will be determined by the types of the operands and since you, as the programmer, have control of the types of the operands you have control of the type of the result.

There is nothing for the program to determine, it is all down to you.

If you mean you need the program to determine if the result would be an integer or a number with a decimal fraction and having made that determination what the smallest type required to contain the result would be then you are going to have to work on it yourself because I can think of no practical reason for doing this so it must be home work.
Oct 1 '08 #7

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

Similar topics

4
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
4
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' '...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool...
4
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
20
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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
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...
0
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
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,...

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.