数组定位、fstream、char比较

Positioning in arrays, fstream, and char comparison

我目前正尝试在我的 C++ 中做一个赋值 class。我想我已经尽我所能了。

作业要求我从文本文件中读取姓名,按顺序排列姓名,然后将它们写入新文件。

给定文本文件中的名称格式如下:

Jackie Sam Tom Bill Mary Paul Zev Barb John

我的作业代码:

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

string nameArray[26] = {};

int main() {

// Defined variables
int y = 65,  lineNumber = 0;
string readName;

// Opens the text file LineUp
ifstream readfile;
readfile.open("LineUp.txt");

// Opens the text file InOrder
ofstream outfile;
outfile.open("InOrder.txt");

// Read name from file
while (readfile >> readName) {

    // If first character of name != char(y),
    // run loop
    while (readName[0] != char(y)) {

        y++;
        lineNumber++;

        // If first character of name = char(y),
        // add name to lineNumber's position in array
        if (readName[0] == char(y)) {
            nameArray[lineNumber] = readName;

        }
    }

    // Reset values of lineNumber and y
    y = 65;
    lineNumber = 0;
}

// Writes names in array to file
for (int x = 0; x <= 25; x++)
    outfile << nameArray[x] << endl;

// Close files
readfile.close();
outfile.close();

// Print statement so you can see at least
// something happens
cout << "KIDS ORGANIZED. BEEP. BOOP. BEEP.\n";
cin.get();
return 0; 
}

保存有序名称的程序中生成的文件的输出:

Barb

John

Mary

Paul

Sam

Tom

Zev

(它包含比堆栈溢出显示更多的空行。)

我的问题如下:

1: 当我想将名称写入文本文件时,如何去掉数组中的空白区域InOrder.txt?

2:名字 Jackie 和 Bill 没有显示,因为数组中的位置被其他两个名字覆盖。如何查看这些职位是否已填写以添加这两个名称?

3: 我可以在我的程序中做些什么来提高它的效率或可读性?或者只是总体上做得更好,我想。

非常感谢任何愿意尝试解决这些问题的人!

首先不要在for-loop中使用nameArray[26],因为它超出了范围。

其次,你肯定已经意识到了这个问题。您逻辑上为每个 alhpabet 的名称保留一个位置,如果有两个名称以相同的字母开头,则会产生问题。还有空白 space 是因为数组中的位置没有任何以相应字母开头的名称。例如。没有名称以 A 开头,因此文件开头将出现空白 space

按照你的逻辑,解决方案应该是这样的:

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

//Can be many names
string nameArray[100] = {};

int main() {

    // Defined variables
    int y = 65,  lineNumber = 0;
    string readName;

    // Opens the text file LineUp
    ifstream readfile;
    readfile.open("LineUp.txt");

    // Opens the text file InOrder
    ofstream outfile;
    outfile.open("InOrder.txt");

    //Run while the names are not checked for all alphabets
    while (y <= 90) {
        // Read names from file till the end is reached 
        while (readfile >> readName) {

            // If first character of name != char(y),
            // run loop
            if(readName[0] == char(y)) {
                // If first character of name = char(y),
                // add name to lineNumber's position in array
                nameArray[lineNumber] = readName;
                lineNumber++;
            }
            }
        }
        //Check File for next character
        y++;
    }

    // Writes names in array to file
    for (int x = 0; x < lineNumber; x++)
        outfile << nameArray[x] << endl;

    // Close files
    readfile.close();
    outfile.close();

    // Print statement so you can see at least
    // something happens
    cout << "KIDS ORGANIZED. BEEP. BOOP. BEEP.\n";
    cin.get();
    return 0; 
}

请注意,此代码仅适用于大写字母,并且仅根据第一个字母表进行排序。

一个问题是字符串数组的长度。您只保留 26 个元素。在你的 for 循环中

for (int x = 0; x <= 26; x++)
    outfile << nameArray[x] << endl;

您尝试访问元素 0 到 26(总共 27 个元素),这比您分配的多。您必须将此循环更改为

for (int x = 0; x < 26; x++)
    outfile << nameArray[x] << endl;

所以它不会崩溃。 但是您程序中的主要问题是排序系统。正如您已经注意到的,只要两个孩子的名字首字母相同,一个名字就会被覆盖。 更好的方法是使用一些标准库。 首先我不会使用 std::string 数组,而是 std::vector。简而言之,向量是一种更好的动态数组,具有更多功能。如果您想了解更多关于向量的信息,请查看 here。 使用向量,您可以先读取每个名称,然后再对其进行排序:

std::vector<std::string> nameVector;
while(readfile >> readName) {
    //add name to the end of the vector
    nameVector.push_back(readName);
}

现在您可以使用std::sort()对向量进行排序。

std::sort(nameVector.begin(), nameVector.end());

最后将所有内容写入输出文件:

for(int x = 0; x < nameVector.size(); ++x)
    outfile << nameVector[x] << std::endl;