can't use double/float left of %?

This is a discussion on can't use double/float left of %? within the C++ Programming forums, part of the General Programming Boards category; Just tell me if I am correct that this doesn't work Code: #include <iostream> using namespace std; int main(void) { ...

  1. #1
    Shadow12345
    Guest

    can't use double/float left of %?

    Just tell me if I am correct that this doesn't work
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void) {
    	float diameter = 7.62;
    	float length = 39;
    	short int remainder = height % 100;
    	cout << "The remainder is " << remainder << endl;
    	
    	return 0;
    }
    Just tell me that this code doesn't work and that I am an idiot for trying. Seriously. I mean it.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    you're an idiot for trying.
    where the hell did you get height from?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    well of course it's not going to work, height is an undeclared identifier

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just tell me that this code doesn't work and that I am an idiot for trying.
    The code doesn't work, but you're certainly not an idiot for trying. The problem is that height doesn't exist as you haven't declared it, but there are a few other points worth making:

    >float diameter = 7.62;
    This should cause a warning on most compilers. Usually something about double being truncated to float. All floating-point literals are double by default, so to remove that warning you should tell the compiler that you want a float.

    float diameter = 7.62f;

    >float length = 39;
    Same thing, different warning. Only the most pernickety compilers will warn against this, but if you are placing the value in a float you should make it a float. As it is the literal is treated as an integer.

    float length = 39.0f;

    >short int remainder = height % 100;
    Be sure to remember that the % operator will only work on integral types, so you will get errors if you try and use it with floats or doubles. If the left operand is a floating-point value, you will probably need to cast it to int first.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Shadow12345
    Guest
    yes height wasn't declared, you knew what I meant

    Prelude should write a compiler, she is a programming goddess
    Last edited by Shadow12345; 07-31-2002 at 09:58 PM.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    sorry for being a little blunt there... i honestly thought you were joking.

    btw, fmod() should do the floating point equivalent of %.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Originally posted by Shadow12345
    Prelude should write a compiler, she is a programming goddess
    *smirk*

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Who do you think writes MS's Visual Studio?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Who do you think writes MS's Visual Studio?
    Ouch.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    dont but it untill your real into c++ and bielive that youll be programming c++ for a long time. newbies should look at freewware compilers
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 04:16 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21