如何以5行显示分数?

时间:2012-11-13 00:39:21

标签: c++ arrays

我对c ++编程很新,我需要帮助我的程序。我应该从文本文件中显示一组分数,但我不知道如何以5行显示它们。

有什么建议吗?

到目前为止,这是我的代码:

//Create a Vector to hold a set of exam scores.Write a program to do the following tasks: 1. Read exam scores into a vector from Scores.txt 
//2. Display scores in rows of five(5) scores.
//3. Calculate average score and display.
//4. Find the median score and display.
//5. Compute the Standard Deviation and display

#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{   
    const int array_size = 36; // array size
    int numbers[array_size]; //array with 36 elements
    int count = 0;
    ifstream inputfile; //input file into stream object
    //open file
    inputfile.open("Scores.txt");
    //read file
    while (count < array_size && inputfile >> numbers[count])
        count++;
    //close file
    inputfile.close(); 
    //display numbers read
    cout << "Scores:\n";
    for (count = 0; count < array_size; count++)
        cout << numbers[count] << " ";
    cout << endl;

    system ("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:1)

将您的代码更改为:

int column_count = 5;
for (count = 0; count < array_size; count++) {
    cout << numbers[count] << " ";
    if ( count % column_count == column_count - 1 ) {
         cout << "\n";
    }
}

答案 1 :(得分:0)

你可以使用这个功能

cout << setw(x);

将生成您打印的下一个项目,所以

cout << setw(10) << "hello" << "bye now";

会给你

     hellobye now

如果你想左对齐,请使用

cout << left;
cout << setw(10) << "hello" << "bye now";

给予

hello     bye now