Help me complete this code

I wrote this code but It needs to read several input and print. How can I take multiple input and output it ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  // Header file for input output functions 
#include<iostream>  
#include<math.h>
  
using namespace std; 
  
// main function - 
// where the execution of program begins 
int main() 
{ 
    //Variable declaration
    double userInput = 0.0, originalVal = 0.0, roundedNum;
    
    //Taking input from user and storing it
    cout << "Enter a double value : ";
  
    cin >> userInput;
  
    originalVal = userInput;
    
    //Rounding number
    roundedNum = floor(userInput + .5);
    
    //Showing result
    cout << "The number " << fixed << originalVal << " was rounded to " << fixed << roundedNum;      
    
    return 0; 
} 
You're going to want to use some sort of loop.
See: https://cplusplus.com/doc/tutorial/control/ [Iteration statements section]

1
2
3
4
5
6
7
8
9
10
int main()
{
    double userInput;
    while (cin >> userInput)
    {
        // ... operate on userInput

        cout << "<output here>\n";
    }
}
Okey, I understand it. Thank you very much. I have one more question. How I can print it evey loop cout << "Enter a double value : ";?
My actual question is "rounds the number x to the nearest integer and assigns the result to y. Write a program that reads several numbers and uses the preceding statement to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number". Is my code true ?
Is my code true ?

Well, your code rounds correctly, although putting the result in an int would remove any floating-point precision problems. If you used an int then a careful assignment, taking care with positive and negative numbers, would bypass the need for floor(). There is a function round() if you need it, to avoid the +0.5 term.

You don't show what you have chosen to do in the way of a loop. What is your loop-termination criterion? Fixed number of entries? Sentinel input? Deliberately closing the program?

You could just do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <sstream>
using namespace std; 
  
int main() 
{ 
   string input;
   cout << "Enter all numbers in one line, separated by spaces\n";
   getline( cin, input );
   istringstream in( input );
   for ( double x; in >> x; ) cout << fixed << x << "   " << (int)( x + 0.5 - (x<0) ) << '\n';
}


Enter all numbers in one line, separated by spaces
0.0 -2.7 -3.1 -4.5 2.7 3.1 4.5
0.000000   0
-2.700000   -3
-3.100000   -3
-4.500000   -5
2.700000   3
3.100000   3
4.500000   5
Last edited on
The standard library has https://en.cppreference.com/w/cpp/numeric/math/round

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <limits>

int main()
{
    constexpr long long ll_min = std::numeric_limits<long long>::min() ;
    constexpr long long ll_max = std::numeric_limits<long long>::max() ;

    std::cout << std::fixed << std::showpos << "rounding away from zero:\n" ;
    for( double d : { -2.50, -2.49, -0.50, -0.49, 0.0, 0.49, 0.5, 2.49, 2.5,
                      -1.0e+20, 1.0e+20, /* out of range of integers */
                      -1.0e+30, 1.0e+30, /* huge value may not be exactly equal to integer -+ 10^30 */   
                      -1.0/0.0, 1.0/0.0, 0.0/0.0 /* -+ infinity, nan */ } )
    {
        // see: https://en.cppreference.com/w/cpp/numeric/math/round
        const double r = std::round(d) ;
        std::cout << "number: " << d << "  rounded: " << r << " (integer value is " ;
        if( d >= ll_min && d <= ll_max ) std::cout << std::llround(d) ; // if d is within the range of long long
        else std::cout << "out of range" ;
        std::cout << ")\n" ;
    }
}

rounding away from zero:
number: -2.500000  rounded: -3.000000 (integer value is -3)
number: -2.490000  rounded: -2.000000 (integer value is -2)
number: -0.500000  rounded: -1.000000 (integer value is -1)
number: -0.490000  rounded: -0.000000 (integer value is +0)
number: +0.000000  rounded: +0.000000 (integer value is +0)
number: +0.490000  rounded: +0.000000 (integer value is +0)
number: +0.500000  rounded: +1.000000 (integer value is +1)
number: +2.490000  rounded: +2.000000 (integer value is +2)
number: +2.500000  rounded: +3.000000 (integer value is +3)
number: -100000000000000000000.000000  rounded: -100000000000000000000.000000 (integer value is out of range)
number: +100000000000000000000.000000  rounded: +100000000000000000000.000000 (integer value is out of range)
number: -1000000000000000019884624838656.000000  rounded: -1000000000000000019884624838656.000000 (integer value is out of range)
number: +1000000000000000019884624838656.000000  rounded: +1000000000000000019884624838656.000000 (integer value is out of range)
number: -inf  rounded: -inf (integer value is out of range)
number: +inf  rounded: +inf (integer value is out of range)
number: -nan  rounded: -nan (integer value is out of range)

http://coliru.stacked-crooked.com/a/ed12eb3aa80a00f4
Thank you guys, It helped me a lot ^^
Topic archived. No new replies allowed.