Creating a program that roundup

Discussion in 'C++' started by AssaultM16, Dec 20, 2007.

  1. AssaultM16

    AssaultM16 New Member

    Joined:
    Dec 20, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I have created a program that should roundup a number:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        
        double numr;
        int numroundup = numr;
        int numroundup1= numr + 1;
        
        cout<<"Input a number \n";
        cin>>numr;
        
        if(numr >= .5){
                
        cout<<"\nThe number is: \n";
        cout<<numroundup1;
        cin.get();
        
        }
        
        else if(numr < .5){
             
        cout<<"The number is: ";
        cout<<numroundup;
        cin.get();
        
        }
        
        return 0;
        
    }
    When I put the number it always says that the number is 1
     
  2. Vromoth

    Vromoth New Member

    Joined:
    Nov 6, 2006
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://vromoth.4000webs.com/
    The answer will ether be the 0 or 1. So what i think is happening is in the first IF, when it says cout<<numroundup1; it is printing 1, and that is because its value is 1. Back at the start of your program when you declare numroundup1 and you've got "= numr + 1" more or less since that at that time numr's value is 0 it really looks like this "int numroundup1 = 0 + 1" which will result in you getting 1 as the answer. What I think would be better is this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        
        double numr;
        int numroundup = 1;
        int numroundup1;
        
        cout<<"Input a number \n";
        cin>>numr;
        
        if(numr >= .5){
        numroundup = numr + 1;        
        cout<<"\nThe number is: \n";
        cout<<numroundup1;
        cin.get();
        
        }
        
        else if(numr < .5){
             
        cout<<"The number is: ";
        cout<<numroundup;
        cin.get();
        
        }
        
        return 0;
        
    }
    
    I edit it a bit there but what I did will not make you program work how you want it to. Firstly I think what you trying to do is get the program to judge(on the IF's) the number not by its' hole but by numbers after the dot, this however will not work. What it really is doing is(lets say you put in the number 2) hay 2 is bigger than .5 so I'll enter the if.
     
  3. AssaultM16

    AssaultM16 New Member

    Joined:
    Dec 20, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thank you very much. Tell me if I am wrong but when I say .5 it means whatever number.5
     
  4. AssaultM16

    AssaultM16 New Member

    Joined:
    Dec 20, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Actually you are right. If its more than .5 it enters the if statement. Is there a way a could fix this?

    Thank You,

    AssaultM16
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice