C ++入门:将字符串与文件行中的数字分开并将这些数字相加

时间:2016-04-01 03:06:17

标签: c++ arrays file vector

我是C ++的新手并且编写了一本本质上是一本成绩簿的程序,在那里我给了一个学生档案和一份成绩列表。以下是文件外观的示例:

" John Smith 34 29 10 49

Hannah Jones 45 48 34 46"

所有成绩均为50分。我必须创建两个向量,一个用于名称,一个用于每个学生的成绩(他们的成绩百分比)。矢量按最高等级排序。我想我已经想出了这个部分,并为它创建了一个功能。

知道怎么做的是将得分的总和加在一起。我知道我需要将名称与文件中的数字分开,然后将这些数字一起添加到文件中的每一行。我只是不知道如何去做。我试过了一些事情,但它还没有奏效。到目前为止,这是我的代码:

#include <iostream>;
#include <vector>;
#include <fstream>;
#include <string>;
#include <sstream>;
using namespace std; 

void sort_vectors(vector<string>& n, vector<double>& p)
{
// selection sort algorithm 
// sorting the percentages 
for (int i = 0; i < p.size() - 1; i++)
{
    int min = i;
    for (int j = i + 1; j < p.size(); j++)
    {
        if (p[j] < p[min])
        {
            min = j; 
        }
    }
    int temp = p[i];
    string ntemp = n[i];
    p[i] = p[min];
    p[min] = temp; 
    n[i] = n[min];
    n[min] = ntemp; 
    }
}

char percent2grade(double p)
{
    if (p >= 90)
        return 'A';
    else if (90 > p >= 80)
        return 'B';
    else if (80 > p >= 70)
        return 'C';
    else if (70 > p >= 60)
        return 'D';
    else
        return 'E'; 
}

int main()
{
// Ask the user for a filename and open the file in read mode. 
    cout << "Please enter a file name + .txt: ";
    string filename;
    cin >> filename;
    ifstream in_file;
    in_file.open(filename);

//While file DNE
    while (in_file.fail())
    {
    //ask user to enter valid filename until user enters valid filename.
        in_file.clear();
        cout << "This file doesn't exist. Please enter a valid file name + .txt: ";
        cin >> filename;
        in_file.open(filename);
    }
    //Creating a new file
    ofstream out_file;
    out_file.open("output.txt");

    //Creating the vectors and string for reading the file 
    vector<string> students;
    vector<double> grades; 
    string line; 
    //reading the file one line at a time
    while (getline(in_file, line))
    {
        string name; 
        double score; 

    // Splitting the lines between names and grades
    //locate the first digit 
        int i = 0; 
        while (!isdigit(line[i]))
        {
            i++;
        }
        //Locate the end of the name
        int j = i - 1; 
        while (isspace(line[j]))
        {
            j--;
        }
        // Extract name
        name = line.substr(0, j + 1);
        students.push_back(name);

       // Extract grades
       istringstream stream;
       stream.str(line.substr(i));
       // As you can see here, I tried to make an array.
       // It didn't exactly work, but I'll leave it this way for now. 
       const int CAPACITY = 100; 
       double gradesum[CAPACITY];
       for (int k = 0; k < (line.substr(i)).size(); k++)
       {
            stream >> gradesum[k];
            score = gradesum[k]; 
       }
     }

     system("pause");
    return 0; 
 }

因此,正如您所看到的,我尝试创建一个数组并在名称进入数组后获取所有数字。我还没有尝试将它们加在一起,但我想知道当我看到阵列时,它是否可以。这没关系。它看起来真的很难看。我完全迷失了,所以如果有人可以帮助我,我真的很感激。

我意识到我的代码中可能会出现比预期更多的错误,但我现在正在努力做到最好!如果你看到其他任何可以改进的地方,请告诉我。

谢谢。

1 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std; 

void sort_vectors(vector<string>& n, vector<double>& p)
{
// selection sort algorithm 
// sorting the percentages 
for (int i = 0; i < p.size() - 1; i++)
{
    int min = i;
    for (int j = i + 1; j < p.size(); j++)
    {
        if (p[j] < p[min])
        {
            min = j; 
        }
    }
    int temp = p[i];
    string ntemp = n[i];
    p[i] = p[min];
    p[min] = temp; 
    n[i] = n[min];
    n[min] = ntemp; 
    }
}

char percent2grade(double p)
{
    if (p >= 90)
        return 'A';
    else if (90 > p >= 80)
        return 'B';
    else if (80 > p >= 70)
        return 'C';
    else if (70 > p >= 60)
        return 'D';
    else
        return 'E'; 
}

int main()
{
// Ask the user for a filename and open the file in read mode. 
    cout << "Please enter a file name + .txt: ";
    string filename;
    cin >> filename;
    ifstream in_file;
    in_file.open(filename.c_str());

//While file DNE
    while (in_file.fail())
    {
    //ask user to enter valid filename until user enters valid filename.
        in_file.clear();
        cout << "This file doesn't exist. Please enter a valid file name + .txt: ";
        cin >> filename;
        in_file.open(filename.c_str());
    }
    //Creating a new file
    ofstream out_file;
    out_file.open("output.txt");

    //Creating the vectors and string for reading the file 
    vector<string> students;
    vector<double> grades; 
    string line; 
    //reading the file one line at a time
    while (getline(in_file, line))
    {
        string name; 
        double score; 

    // Splitting the lines between names and grades
    //locate the first digit 
        int i = 0; 
        while (!isdigit(line[i]))
        {
            i++;
        }
        //Locate the end of the name
        int j = i - 1; 
        while (isspace(line[j]))
        {
            j--;
        }
        // Extract name
        name = line.substr(0, j + 1);
        students.push_back(name);

       // Extract grades
       istringstream stream;
       stream.str(line.substr(i));

       // This gives you the mean score in percent
       int total=0;
       j=0;
       while(stream>>i){
        total+=i;
        j++;
       }
       score=total*2/j;
     }

     //system("pause");
    return 0; 
 }