简体   繁体   English

从文件中读取字符串并进行排序

[英]Reading strings from file and sorting

I need help with my assignment. 我的作业需要帮助。 I am not allowed to use any functions other than loops, if statements, cout, cin, basic operators and basic string. 除语句,cout,cin,基本运算符和基本字符串之外,不允许使用除循环以外的任何函数。 Arrays are not allowed. 不允许使用数组。 I need to sort a list of names from a file by alphabetical order and output which names would be at the front and back of the line. 我需要按字母顺序对文件中的名称列表进行排序,并输出该名称位于行的开头和结尾。 However when I try to run my code it stops at the part where it reads the file. 但是,当我尝试运行代码时,它将停止在读取文件的部分。 The file is in the correct location because I hae tested it with cout. 该文件位于正确的位置,因为我已经使用cout测试了它。 I cant figure out what I am doing wrong. 我不知道我在做什么错。 Any help would be appreciated! 任何帮助,将不胜感激! Heres the code: 这是代码:

#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;
}
         cin >> studentName;

This is why your program stops. 这就是程序停止的原因。 It is waiting for you to type something at the keyboard. 它正在等待您在键盘上键入内容。 (cin is the standard input in C++) (cin是C ++中的标准输入)

My sincere advise would be always use pen and paper and note down your requirement first. 我的真诚建议是始终使用笔和纸,并先记录下您的要求。 Base on that write down the algorithm and convert that algorithm into code. 在此基础上写下算法并将该算法转换为代码。 What are you doing in your codes are all wrong unfortunately. 不幸的是,您在代码中所做的一切都是错误的。 Just deeply think what are you doing in your code specially below codes:- 只需仔细考虑一下您在代码中的工作,特别是下面的代码:

   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
            }

If you consider all this very carefully you will understand all your mistakes. 如果您非常仔细地考虑所有这些,您将理解您的所有错误。

Hope this will help you.. 希望这个能对您有所帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM