Daniel
Daniel

Reputation: 47

How to display whole numbers without decimals

I created a program to display an average from an array of numbers the user have decided to input. The program asks the user the amount of numbers he / she will input, then they input all positive numbers. The output for the average is always a decimal, how can I only display the whole number without any decimal points. Ex. 12.34 = 12 / 8.98 = 8

#include <iostream>
#include <iomanip>
using namespace std;

void sortingTheScores(double *, int); 
void showsTheScoresNumber(double *, int);  
double averageForAllScores(double, int);  

int main()
{
    double *scores;                        
    double total = 0.0;                         
    double average;                            
    int numberOfTestScores;                      
                                       
    cout << "How many test scores do you have? ";
    cin >> numberOfTestScores;

    
    scores = new double[numberOfTestScores];
    if (scores == NULL)
        return 0;

    
    
    for (int count = 0; count < numberOfTestScores; )  
    {
        cout << "Test Score #" << (count + 1) << ": ";
        cin >> scores[count];
        while (scores[count] <= 0)
        {
            cout << "Value must be one or greater: " ;  
               
            cin >> scores[count];
        }
        
        count = count +1; 
    }

    


    for (int count = 0; count < numberOfTestScores; count++)  
    {
        total += scores[count]; 
        
        
    }
    
    
    

    
    sortingTheScores(scores, numberOfTestScores);

    cout << "The numbers in set are: \n";
    showsTheScoresNumber(scores, numberOfTestScores);
    
    
    averageForAllScores(total, numberOfTestScores);
    
    cout << fixed << showpoint << setprecision(2);
    cout << "Average Score: " << averageForAllScores(total,numberOfTestScores);


    return 0;

}



void sortingTheScores (double *array, int size)
{
    int sorting;
    int theIndex;
    double  theNumbers;
    
    
    for (sorting = 0; sorting < (size - 1); sorting++)  
    {
        theIndex = sorting;
        theNumbers = array[sorting];
        for (int index = sorting + 1; index < size; index++) 
        {
            if (array[index]  < theNumbers)
            {
                theNumbers = array[index];
                theIndex = index;
            }
        }
        array[theIndex] = array[sorting];
        array[sorting] = theNumbers;
    }
}



void showsTheScoresNumber (double *array, int size)
{
    for (int count = 0; count < size; count++) 
        cout << array[count] << " ";
    cout << endl;
    
    
}






double averageForAllScores(double total, int numberOfTestScores)
{   double average;
    
    average = total / numberOfTestScores;
    return average;

    
    
}  

Upvotes: 1

Views: 1316

Answers (3)

Rohan Bari
Rohan Bari

Reputation: 7726

You can do it without using iomanip library:

std::cout.precision(0);
std::cout << 1.231321 << std::endl;

Then you'll simply get:

1

Just you need to use std::cout.precision() which is equivalent to std::setprecision() from iomanip library.

Edit:

The aforementioned solution is okay for smaller floating point values, but if you try something like 1334.231321, the std::cout will result displaying some scientific notation, something like:

1e+03

which is actually odd to read and understand. To solve it, you need std::fixed flag, you may write something like:

std::cout.precision(0), std::cout << std::fixed;
std::cout << 1334.231321 << std::endl;

Then it'll show:

1334

Upvotes: 1

Dmitri Ivanov
Dmitri Ivanov

Reputation: 31

For numbers in a +/-2^31 range you can do:

cout << int(12.34) << " " << int(8.98) << endl;

which produces output

12 8

You may also want to consider rounding to the nearest integers. To do so add a line

#include <cmath>

then do

cout << int(rint(12.34)) << " " << int(rint(8.98)) << endl;

this gives

12 9

Upvotes: 0

kesarling
kesarling

Reputation: 2238

You can use I/O manipulators here:

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::setprecision(0) << 1.231321 << '\n';
}

Output:

1

Upvotes: 1

Related Questions