在数组、fstream和字符比较中定位 我目前正试图在C++类中完成一个作业。我想我已经尽我所能了

在数组、fstream和字符比较中定位 我目前正试图在C++类中完成一个作业。我想我已经尽我所能了,c++,arrays,char,fstream,string-comparison,C++,Arrays,Char,Fstream,String Comparison,作业要求我从文本文件中读取名称,将名称按顺序排列,然后将其写入新文件 给定文本文件中的名称格式如下: 杰基山姆汤姆比尔玛丽保罗泽夫芭比约翰 我的作业代码: #include <iostream> #include <fstream> #include <string> using namespace std; string nameArray[26] = {}; int main() { // Defined variables int y = 65,

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

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

杰基山姆汤姆比尔玛丽保罗泽夫芭比约翰

我的作业代码:

#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; 
}
#包括
#包括
#包括
使用名称空间std;
字符串名称数组[26]={};
int main(){
//定义变量
int y=65,行号=0;
字符串readName;
//打开文本文件列表
ifstream读取文件;
打开(“LineUp.txt”);
//按顺序打开文本文件
出流孔的直径;
outfile.open(“inoorder.txt”);
//从文件中读取名称
while(readfile>>readName){
//如果名称的第一个字符!=字符(y),
//运行循环
while(readName[0]!=char(y)){
y++;
lineNumber++;
//如果名称的第一个字符=字符(y),
//将名称添加到行号在数组中的位置
if(readName[0]==char(y)){
nameArray[lineNumber]=readName;
}
}
//重置lineNumber和y的值
y=65;
行号=0;
}
//将数组中的名称写入文件

对于(int x=0;x首先不要在
for循环中使用
名称数组[26]
,因为它超出范围

现在你肯定已经意识到这个问题了。你的逻辑为每个Alpababe保留了一个名字的位置,如果有两个名字从同一个字母开始,它会产生问题。空白区也是因为数组中的地方没有任何名字从字母表开始。文件的开头会有一个空格,类似地,没有名字以C开头,所以名字Barb后面会有一个空格

根据您的逻辑,解决方案应如下所示:

#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; 
}
#包括
#包括
#包括
使用名称空间std;
//可以有很多名字
字符串名称数组[100]={};
int main(){
//定义变量
int y=65,行号=0;
字符串readName;
//打开文本文件列表
ifstream读取文件;
打开(“LineUp.txt”);
//按顺序打开文本文件
出流孔的直径;
outfile.open(“inoorder.txt”);
//在未检查所有字母的名称时运行
while(y>readName){
//如果名称的第一个字符!=字符(y),
//运行循环
if(readName[0]==char(y)){
//如果名称的第一个字符=字符(y),
//将名称添加到行号在数组中的位置
nameArray[lineNumber]=readName;
lineNumber++;
}
}
}
//检查文件中的下一个字符
y++;
}
//将数组中的名称写入文件
对于(int x=0;xoutfile首先不要在
for循环中使用
名称数组[26]
,因为它超出范围

现在你肯定已经意识到这个问题了。你的逻辑为每个Alpababe保留了一个名字的位置,如果有两个名字从同一个字母开始,它会产生问题。空白区也是因为数组中的地方没有任何名字从字母表开始。文件的开头会有一个空格,类似地,没有名字以C开头,所以名字Barb后面会有一个空格

根据您的逻辑,解决方案应如下所示:

#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; 
}
#包括
#包括
#包括
使用名称空间std;
//可以有很多名字
字符串名称数组[100]={};
int main(){
//定义变量
int y=65,行号=0;
字符串readName;
//打开文本文件列表
ifstream读取文件;
打开(“LineUp.txt”);
//按顺序打开文本文件
出流孔的直径;
outfile.open(“inoorder.txt”);
//在未检查所有字母的名称时运行
while(y>readName){
//如果名称的第一个字符!=字符(y),
//运行循环
if(readName[0]==char(y)){
//如果名称的第一个字符=字符(y),
//将名称添加到行号在数组中的位置
nameArray[lineNumber]=readName;
lineNumber++;
}
}
}
//检查文件中的下一个字符
y++;
}
//将数组中的名称写入文件
对于(int x=0;xoutfile一个问题是字符串数组的长度。在for循环中只保留26个元素

for (int x = 0; x <= 26; x++)
    outfile << nameArray[x] << endl;
最后将所有内容写入输出文件:

for(int x = 0; x < nameVector.size(); ++x)
    outfile << nameVector[x] << std::endl;
for(int x=0;xoutfile一个问题是字符串数组的长度。在for循环中只保留26个元素

for (int x = 0; x <= 26; x++)
    outfile << nameArray[x] << endl;
最后将所有内容写入输出文件:

for(int x = 0; x < nameVector.size(); ++x)
    outfile << nameVector[x] << std::endl;
for(int x=0;x您访问的输出文件超出范围
nameArray[26]
for
循环中,这很糟糕。我不确定你说的超出范围是什么意思,但当我在函数中有数组时,它会使调试器崩溃。使用向量代替字符串数组…它会自动解决你的超出范围问题。你的声明是
字符串名称数组[26]={};
,所以只有
nameArray[0]
nameArray[25]
是可用的,你不能使用
nameArray[26]
@Tejendra我不太懂术语。你能举个例子说明你的意思吗?你访问了超出范围的
nameArray[26]
for
循环中,这很糟糕。我不确定你说的超出范围是什么意思,但当我在函数中有数组时,它会使调试器崩溃。使用向量代替字符串数组…它会自动解决你的超出范围问题。你的声明是
字符串名称数组[26]={};
,因此只有
nameArray[0]
nameA