令人困惑的C ++分配

时间:2015-10-09 05:00:00

标签: c++

我是大学新生CS学生,我似乎无法解决这个问题。它基本上要我询问用户特定班级的学生数量,然后不断循环询问学生姓名。完成所有这些后,我必须按字母顺序找出输入的名称,按字母顺序排列,以及最后输入的名称。

到目前为止,我的代码完成所有操作,直到我必须对名称进行排序,以及错误地显示名称。它只显示输入的最新名称,而不是一次显示两个名称。

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

int main () {

    int studentCount;
    string studentName;

    cout << "Hello fellow educator!" << endl;
    cout << "I will be helping you with your line-up for today." << endl;
    cout << "Please enter the number of students in your class: ";
    cin >> studentCount;
    cout << endl;
    cout << endl;

    if (studentCount < 1 || studentCount > 25) {
        cout << "Please try again." << endl;
        cout << "Try putting in a number between 1 and 25." << endl;
        cout << endl;
    }

    for (int count = 1; count <= studentCount; count++) {

        cout << "Please enter a student's name: ";
        cin >> studentName;
    }
    cout << "the names are " << studentName; // Just testing the string

    return 0;
}

3 个答案:

答案 0 :(得分:1)

  1. 首先,您需要创建一个string数组而不是单个字符串以避免覆盖。
  2. 之后,排序他们lexicographically并打印出来。
  3. 编辑:

    看看下面给出的代码:

    #include <iostream>
    #include <string>
    #include <algorithm>      /* Used to sort the string lexicographically */
    #define LIMIT 50          /* I'm considering that you are not providing more than 50
                                 names at once to a program*/
    using namespace std;
    
    int main () {
        int studentCount;              /* Count number of the students */
        string studentName[LIMIT];     /* Array of string */
    
        cin >> studentCount;           /* Input number of student */
    
        /* Keep on asking until the correct value of studentCount is not provided */
        while (studentCount < 1 || studentCount > 25) {  
            cout << "Please try again." << endl;  
            cout << "Try putting in a number between 1 and 25." << endl;
            cin >> studentCount;
        }
    
        cout <<"Please enter a student's name: ";
        for (int count = 0; count < studentCount; count++) {  
            cin >> studentName[count];        /* I started the count value from 0 to n-1 
                                              because the index of an array starts from 0 */ 
        }
    
        /* Now, time to sort the array of string lexicographically */
        sort(studentName, studentName+studentCount);
    
        /* Print the names of the student */
        cout << "Names of the students : " << endl;
        for(int i=0; i< studentCount; i++) {
            cout << studentName[i] << endl;
        }
    
        return 0;
    }
    

    此代码会一直询问,直到studentCount的正确值 收到。您甚至可以通过将LIMIT的值从50更改为您可能需要的其他内容来更改程序的上限。

答案 1 :(得分:1)

在收集名称时,将名称存储在数据结构中。如果继续将所有名称添加到单个字符串中,则可以将其存储为一个连接字符串,但我们需要不同的名称(字符串)。 所以,让我们把矢量作为我们的数据结构。

int main () {

int studentCount;
string studentName;
vector<string> attendanceBook;

for (int count = 1; count <= studentCount; count++) {

    cout << "Please enter a student's name: ";
    studentName.clear();
    cin >> studentName;
    attendanceBook.push_back(studentName);
}

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

cout<<"First: "<<name.front<<endl<<"Last: "<<name.back();

答案 2 :(得分:1)

所以这是完成的代码,经过数小时的工作,它运行并正确地完成所有事情。我知道,这很简单,但对于刚刚接触过Java表面的大学新生来说,这对我来说很复杂。 :)我知道有一种方法可以做到这一点没有矢量或数组..感谢所有尽力帮助我的人。我一定会在将来回来。

 #include <iostream>
 #include <string>

    using namespace std;

    int main () {

        int studentCount;
        string studentNames;
        string first;
        string last;

        cout << "Hello fellow educator!" << endl;
        cout << "I will be helping you with your line-up for today." << endl;
        cout << "Please enter the number of students in your class: ";
        cin >> studentCount;
        cout << endl;
        cout << endl;

        while (studentCount < 1 || studentCount > 25) {
            cout << "Please try again." << endl;
            cout << "Try putting in a number between 1 and 25: ";
            cin >> studentCount;
        }

        for (int count = 0; count < studentCount; count++) {

            cout << "Please enter the name for student number " << count + 1 << ":";
            cin >> studentNames;

            if (count == 1) {

                first = studentNames;
                last = studentNames;

            }
            else {
                if (studentNames < first) {
                first = studentNames;
            }
                else if (studentNames > last) {
                    last = studentNames;
                }
            }
        }

            cout << "The first student in line is " << first << "." << endl;
            cout << "The last student in line is  " << last << "." << endl;

        return 0;
    }