Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Creating a program that roundup (http://www.go4expert.com/forums/creating-program-roundup-t7939/)

AssaultM16 20Dec2007 05:51

Creating a program that roundup
 
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

Vromoth 20Dec2007 07:11

Re: Creating a program that roundup
 
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.

AssaultM16 20Dec2007 10:21

Re: Creating a program that roundup
 
Thank you very much. Tell me if I am wrong but when I say .5 it means whatever number.5

AssaultM16 20Dec2007 10:25

Re: Creating a program that roundup
 
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


All times are GMT +5.5. The time now is 16:23.