c++ - 如何显示不带小数的整数

标签 c++ arrays visual-c++ c++17

我创建了一个程序来显示用户决定输入的一组数字的平均值。程序会询问用户他/她将输入的数字量,然后他们输入所有正数。平均值的输出始终是小数,我如何只显示整数而没有小数点。例如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;

    
    
}  

最佳答案

您可以在此处使用I/O manipulators:

#include <iostream>
#include <iomanip>

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

关于c++ - 如何显示不带小数的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62887755/

相关文章:

python - c malloc数组指针在cython中返回

java - 如何在 Java 中使用现有的 JButton 创建 JButton 数组?

c++ - 如何构建自定义 libcurl 以仅支持 HTTP/HTTPS 协议(protocol)

c++ - std::_USE 未定义

c++ - 编译错误Visual Studio 2013

c++ - 为什么这个程序有这种行为(push_back)?

ios - 如何在标签上显示数组

windows - 从 C++ 源文件创建 Windows DLL

c++ - 无法通过 QModelIndex 从 QTreeView 获取项目

c++ - 函数模板重载解析,依赖和非依赖参数