typeof

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alsoneedhelp2
    New Member
    • Sep 2008
    • 2

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I think that you can find the size of all the primative types on your machine by
      using the sizeof function. e.g.
      Code:
      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.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by whodgson
        I think that you can find the size of all the primative types on your machine by
        using the sizeof function. e.g.
        Code:
        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.

        Comment

        • alsoneedhelp2
          New Member
          • Sep 2008
          • 2

          #5
          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;
          }

          Comment

          • manontheedge
            New Member
            • Oct 2006
            • 175

            #6
            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?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by alsoneedhelp2
              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.

              Comment

              Working...