在主函数中调用时,来自单独的.cpp文件的函数未运行

时间:2019-11-10 22:40:43

标签: function header

您好,我是这个网站的新手,还是一个相当新手的编码人员,我正在研究一个课程的作业。我试图使用链接列表创建代码,并试图从另一个.cpp中调用要运行的函数。我可以编译到目前为止的内容,并且可以运行它,但是它将仅运行我在main中放置的内容,而不会单独运行任何内容。我只需要一些帮助,以弄清楚为什么我无法获取运行我正在调用的函数的代码。下面列出的是我的主要


#include"prog9.h"
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>

void createList();
void menu();
const int STUDENTS = 15;
int main()
{
    student arr[STUDENTS];
    std::ifstream(info);
    info.open("prog9input.txt");

    if(!info.is_open())
    {
        std::cout << "prog9input.txt failed to open" << endl;

        system("PAUSE>NUL");

        return 1;
    }

    for(int i = 0; i < STUDENTS ;i++)
    {
        info >> arr[i].studentID >> arr[i].name >> arr[i].number >> arr[i].strtName >> arr[i].strEnd 
            >> arr[i].gpa;
    }

    info.close();

    student createList();
    student menu();

}

下面是我的头文件

#ifndef PROG9_H_INCLUDED
#define PROG9_H_INCLUDED

#include<string>

using namespace std;
class student
{
public:
    int studentID;
    string name;
    int number;
    string strtName;
    string strEnd;
    float gpa;

    protected:
    struct Node
    {
        int studentID;
        string name;
        int number;
        string strtName;
        string strEnd;
        float gpa;

        Node *next;
};
Node *head;

void createList();
void menu();
void insertNode();
void deleteNode();
void displayList();

};

#endif // PROG9_H_INCLUDED

这是我用于功能的辅助.cpp。

#include"prog9.h"
#include<iomanip>
#include<iostream>
using namespace std;
//3. Delete the kth record from the middle of the sorted list and display the resulting list. 
    (Delete function.)
void student::createList()
{

    int num = 15;

    student arr[num];

    Node *newNode;
    Node *nodePtr;
    Node *previousNode = nullptr;

    for(int k = 0;k < num;k++)
    {
    newNode = new Node;

    newNode->studentID = arr[k].studentID;
    newNode->name = arr[k].name;
    newNode->number = arr[k].number;
    newNode->strtName = arr[k].strtName;
    newNode->strEnd = arr[k].strEnd;
    newNode->gpa = arr[k].gpa;


    if(!head)
    {
        head = newNode;
        newNode->next = nullptr;
    }

    else
    {
        nodePtr = head;
        previousNode = nodePtr;

        while(nodePtr != nullptr && nodePtr->studentID < arr[k].studentID)
        {
             previousNode = nodePtr;
             nodePtr = nodePtr->next;
        }

        if(previousNode == nullptr)
        {
            head = newNode;
            newNode->next = nodePtr;
        }
    }
    }
}

void student::menu()
{
    std::cout << 
"================================================================================================\n";
    std::cout << "A)\tDisplay Sorted Student List\n";
    std::cout << "B)\tDelete Student from list\n";
    std::cout << "C)\tAdd new Student Record\n";
    std::cout << "D)\tShow unsorted Student list\n";
    std::cout << "E)\tEnd Program\n";
    std::cout << 
"================================================================================================\n";
}

void student::deleteNode()
{
    Node *nodePtr;
    Node *previousNode;
    int del;
    int mth = 0;
    int middle;
    int student = 15;

    std::cout << "what is the student ID of the student you wish to delete?: ";
    std::cin  >> del;
    std::cout << "\n";

    if (!head)
        return;

    if (head->studentID == del)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;

        middle = ( student / 2);
        student--;

        std::cout << "you have deleted a node ";
        std::cout << (middle - 1) << " from the middle.\n";

        student::displayList();
    }

    else
    {
        nodePtr = head;

        while (nodePtr != nullptr && nodePtr->studentID != del)
        {
            mth++;
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if (nodePtr)
        {
            previousNode->next = nodePtr->next;
            delete nodePtr;
            std::cout << "you have deleted a node ";
            std::cout << (middle - mth) << " from the middle.\n";

            student::displayList();
        }
    }
}

void student::displayList()
{
     Node *nodePtr;
    nodePtr = head;

    while (nodePtr)
    {
        std::cout << nodePtr->studentID << "\t";
        std::cout << nodePtr->name << "\t";
        std::cout << nodePtr->number << "\t";
        std::cout << nodePtr->strtName << "\t";
        std::cout << nodePtr->strEnd << "\t";
        std::cout << nodePtr->gpa << "\t";

        nodePtr = nodePtr->next;
    }
}

void student::insertNode()
{
    Node *newNode;
    Node *nodePtr;
    Node *previousNode = nullptr;

    int IDnum,
        hnum,
        cnt,
        student = 15,
        middle = 0;
    string name,
           strtName,
           strtend;
    float gpa;

    middle = (student / 2);

    std::cout << "what is the student ID of the student you wish to add?: ";                        //
    std::cin  >> IDnum;                                                                             //
    std::cout << "\n\n";                                                                            //
                                                                                                //
    std::cout << "what is the first name of the student you wish to add?: ";                        //
    std::cin  >> name;                                                                              //
    std::cout << "\n\n";                                                                            //
                                                                                                //
    std::cout << "what is the house number of the student you wish to add?: ";                      //
    std::cin  >> hnum;                                                                              //
    std::cout << "\n\n";                                                                            //
                                                                                                //
    std::cout << "what is the name of this students street in their street address?: ";             //
    std::cin  >> strtName;                                                                          //
    std::cout << "\n\n";                                                                            //
                                                                                                //
    std::cout << "what is the street end name of this student?(i.e. ave., st. ,blvrd. ect.): ";     // 
    std::cin  >> strtend;                                                                           //
    std::cout << "\n\n";                                                                                                                                                                 
    std::cout << "what is the students gpa?: ";                                                     
    std::cin  >> gpa;                                                                               
    std::cout << "\n\n";                                                                            

    newNode = new Node;

    newNode->studentID = IDnum;
    newNode->name = name;
    newNode->number = hnum;
    newNode->strtName = strtName;
    newNode->strEnd = strtend;
    newNode->gpa = gpa;

    if(!head)
    {
        head = newNode;
        newNode->next = nullptr;
        std::cout << "you added a new node at " << middle - 1 << " from the middle.\n";
    }
    else
    {
        nodePtr = head;
        previousNode = nodePtr;

        while(nodePtr != nullptr && nodePtr->studentID < IDnum)
        {
             cnt++;
             previousNode = nodePtr;
             nodePtr = nodePtr->next;
        }

        if(previousNode == nullptr)
        {
            head = newNode;
            newNode->next = nodePtr;
            std::cout << "you added a new node at " << middle - cnt << " from the middle.\n";
        }
    }
}

请记住,我的代码尚未完成,所以我知道它可能会丢失一些东西,我只是因为无法正确运行我的功能而已。任何建议都会有所帮助!我没有错误代码,将运行main和来自main的cout,但是第二个cpp文件将什么也没有。

0 个答案:

没有答案