0

(文字列を指す) ポインターの配列を並べ替えてから、並べ替えられていないリストと並べ替えられたリストを表示する方法を見つけようとしましたが、2 番目に印刷されたリストは元の並べ替えられていないリストと常に同じです。リスト。あなたが提供できるどんな助けも大歓迎です(そして、私のコードが混乱していたらごめんなさい、私は新入生です)

これは私のメイン(lab5.cpp)です

#include <cstdlib>
#include <iostream>
#include "student.h"
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    student stu;
    stu.list();
    system("PAUSE");
    return EXIT_SUCCESS;
}

これは私のヘッダーです(student.h)

#include <string>
class student
{
public:
    student( );
    void setnameage();
    int getage(int);
    std::string getname(int);
    void sort();
    void list();

 private:
     std::string name[50];
     std::string nameL[50];
     int age[50];
     std::string * Pname ; 
     int * Page; 
     int amount;   
 };

これは私のオブジェクトです (student.cpp)

#include <iostream>
#include <iomanip>
#include "student.h"
#include <string>

using namespace std;
//constructor
student::student()
{
    int i = 0;
    amount = 0;
    Pname = name;
    Page = age;
    while (i != 50)
    {
        age[i] = 0;
        name[i] = "A";
        i = i +1 ;
    }
    std::cout << "Enter number of students(max 50) \n" << ">";
    std::cin >> amount;
}

//sets the neame and the age
void student::setnameage()
{
    int i = 0;
    while (i != amount)
    {
        std::cout << "Enter name " << i+1 <<" (last, first):";
        std::cin >> name[i] >> nameL[i];
        std::cout << "enter age";
        std::cin >> age[i];
        i++;
    }
}

//get age
int student::getage(int i)
{
    return age[i];
}

//get name   
std::string student::getname(int i)
{
    return name[i];
}

//sorts the aray of pointers
void student::sort()
{
    std::string tempL;
    int tempN;
    i = 0
    for (int i = 1; i <= amount-1; i++)
    {
        for(int j=i+1; j <= amount; j++)
        {
            if(Pname[i].compare(Pname[j]) > 0)
            {
                tempN = Page[i];
                Page[i] = Page[j];
                Page[j] = tempN;
                // tempL = Pname[i];
                Pname[i].swap(Pname[j]);
                //Pname[j] = tempL;
            }
        }
    }
}

//displayes the final results         
void student::list()
{
    setnameage();
    int i = 0;
    std::cout << "original list\n-------------";
    while(i != amount)
    {
        std::cout<< "\n" << getname(i) << ">" << getage(i);
        i++;
    }
    sort();
    i = 0;
    std::cout << "\nAlphabetized list\n-------------";
    while(i != amount)
    {
        std::cout<< "\n" << Pname[i] << ">" << Page[i];
        i++;
    }
}
4

1 に答える 1