1

私は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;
}
4

2 に答える 2

1

コードを次のように変更します。

int column_count = 5;
for (count = 0; count < array_size; count++) {
    cout << numbers[count] << " ";
    if ( count % column_count == column_count - 1 ) {
         cout << "\n";
    }
}
于 2012-11-13T00:43:45.433 に答える
0

あなたは機能を使うことができます

cout << setw(x);

次のアイテムを印刷するので、

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

あなたに与えるだろう

     hellobye now

左揃えにしたい場合は、

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

与えるために

hello     bye now
于 2012-11-13T01:29:12.427 に答える