从文件和排序中读取字符串

时间:2018-03-07 22:37:37

标签: c++ string file sorting ifstream

我的作业需要帮助。我不允许使用除循环之外的任何函数,if语句,cout,cin,基本运算符和基本字符串。不允许使用数组。我需要按字母顺序对文件中的名称列表进行排序,并输出哪些名称将位于行的前面和后面。但是,当我尝试运行我的代码时,它会在它读取文件的部分停止。该文件位于正确的位置,因为我已经用cout测试了它。我无法弄清楚我做错了什么。任何帮助,将不胜感激! 下面是代码:

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

int main()
{

    //Intialize variables
    string studentName;
    string firstEntry;
    string secondEntry;
    string first;
    string last;
    ifstream inputFile;
    string filename;
    int students;

    //Ask for amount of students
    cout << "Please enter the number of students in the class.\n(The number must be a whole number between 1 and 25.)\n";
    cin >> students;

    //Input validation
    while (students < 1 || students > 25)
    {
        cout << "\nInvalid value. Please enter a value between 1 and 25.\n";
        cin >> students;
    }

    //Get file name from user
    cout << "Please enter the name of the file with the list of students\n";
    cin >> filename;

    //Open the file
    inputFile.open(filename);
    if (inputFile)
    {
        while (inputFile >> studentName)
        {
            cin >> studentName;
            studentName = firstEntry;
            cin >> studentName;
            studentName = secondEntry;
            do
            {
                if (firstEntry < secondEntry)
                {
                    firstEntry = first;
                    secondEntry = last;
                }
                else
                {
                    firstEntry = last;
                    secondEntry = first;
                }
            } while (students = 30);

            if (firstEntry < first)
                firstEntry = first;

            if (secondEntry < first)
                secondEntry = first;

            if (firstEntry > last)
                firstEntry = last;

            if (secondEntry > last)
                secondEntry = last;

        }
        cout << first << " is the first student in line.";
        cout << last << " is the last student in line.";
    }
    else
    {
        cout << "Error opening the file.\nPlease restart the program and try again.";
        return 1;
    }

    inputFile.close();
    return 0;
}

2 个答案:

答案 0 :(得分:1)

         cin >> studentName;

这就是你的程序停止的原因。它等着你在键盘上输入一些东西。 (cin是C ++中的标准输入)

答案 1 :(得分:0)

我真诚的建议总是使用笔和纸,并首先记下你的要求。在此基础上写下算法并将该算法转换为代码。不幸的是,你在你的代码中做了什么都是错的。只是深入思考你在代码中做了什么,特别是在代码下面: -

   while (inputFile >> studentName)//Think what are you doing here?
    {
        cin >> studentName;//???? reading into the same variable
        studentName = firstEntry;//???? what are your doing here no value in firstEntry
        cin >> studentName;//??here
        studentName = secondEntry;//?? here no value in secondEntry
        do
        {
            if (firstEntry < secondEntry)// this is not correct string comparison  
            {
                firstEntry = first;//?? no value in first
                secondEntry = last;//?? no value in last
            }

如果你仔细考虑这一切,你就会理解你的所有错误。

希望这会对你有帮助..