C ++我的文件在尝试将其移动到源文件之前一直在工作,现在没有错误,也没有任何显示

时间:2018-08-31 20:59:30

标签: c++

我很抱歉对此主题中的问题含糊不清,但我确实不确定。我正在尝试创建一个course_directory作为学校的实验室项目,到目前为止,一切进展都很好。在尝试将类函数移入.hpp文件之前,我已经在main.cpp中编写并测试了每个函数。我的目标是什么都没有,除了调用“运行”将打开一个文件,然后调用“ displayMenu”,并允许用户根据自己的选择与信息进行交互,但现在一切都已完成编译正确,没有任何显示。

Course_directory.hpp

#include "Course_Directory.h"

using namespace std;

Course_Directory::Course_Directory(){};
Course_Directory courses[1001];

void Course_Directory::displayMenu(){
cout << "1.Print all courses" << endl;
cout << "2.Print all courses for a department" << endl;
cout << "3.Print roster for a course" << endl;
cout << "4.Print the largest class" << endl;
cout << "5.Swap two classes" << endl;
cout << "6.Print schedule for a student" << endl;
cout << "7.Exit" << endl;

char dept[50], dept2[50];
int courseNo, courseNo2, i, choice;

while (choice != 7) {
    cout << "\nEnter your choice: ";
    cin >> choice;
    if (choice == 1)
        printAllCourses(i);
    else if (choice == 2) {
        cout << "\nEnter department name:";
        cin >> dept;
    for(int j = 0; j < sizeof(dept); j++)
    {
        dept[j] = (toupper(dept[j]));
    }
        coursesInDept(dept, i);
    }
    else if (choice == 3) {
        cout << "\nEnter course number:";
        cin >> courseNo;
        studentsInCourse(courseNo, i);
    }
    else if (choice == 4) {
        largestClass(i);
    }
    else if (choice == 5) {
        cout << "\nEnter first department name :";
        cin >> dept;
        for(int j = 0; j < sizeof(dept); j++)
        {
            dept[j] = (toupper(dept[j]));
        }
        cout << "\nEnter first course number: ";
        cin >> courseNo;
        cout << "\nEnter second department name :";
        cin >> dept2;
        for(int k = 0; k < sizeof(dept2); k++)
        {
            dept2[k] = (toupper(dept2[k]));
        }
        cout << "\nEnter second course number: ";
        cin >> courseNo2;
        swap2(dept, courseNo, dept2, courseNo2, i);
    }
    else if (choice == 6) {
        int id;
        cout << "\nEnter a student Id:";
        cin >> id;
        schedule(id, i);
    }
}
cout << "Goodbye!\n";
}
void Course_Directory::printAllCourses(int len){
for (int i = 0; i < len; i++)
    cout << "Course name: " << courses[i].courseName << ", Course 
number: " << courses[i].courseNum << endl;
    cout << len;
}
void Course_Directory::coursesInDept(char *dept, int len) {
for (int i = 0; i < len; i++)
    if (strcmp(dept, courses[i].courseName) == 0)
        cout << "Course Name: " << courses[i].courseName << ", 
Course number: " << courses[i].courseNum << endl;
}

void Course_Directory::studentsInCourse(int courseNo, int len) {
for (int i = 0; i < len; i++)
    if (courseNo == courses[i].courseNum) {
        for (int m = 0; m < courses[i].numStudents - 1; m++)
            cout << courses[i].IDs[m] << ",";
            cout << courses[i].IDs[courses[i].numStudents - 1] << 
endl;
    }
}

void Course_Directory::largestClass(int len) {
int max = -999;
for (int i = 0; i < len; i++) {
    if (courses[i].numStudents > max)
        max = courses[i].numStudents;
}
for (int i = 0; i < len; i++) {
    if (courses[i].numStudents == max){
        cout << "\nThe largest class is in department: " << 
courses[i].courseName
        << ", and the course number is: " << courses[i].courseNum 
<< "\n";
        cout << "The class currently has " << max << " students 
enrolled.\n";
    }
}
}

void Course_Directory::swap2(char *firstDep, int firstNo, char 
*secondDep, int secondNo, int len) {
Course_Directory temp;
int firstIndex, secondIndex;
for (int i = 0; i < len; i++) {
    if (strcmp(firstDep, courses[i].courseName) == 0 && 
courses[i].courseNum == firstNo)
        firstIndex = i;
    if (strcmp(secondDep, courses[i].courseName) == 0 && 
courses[i].courseNum == secondNo)
        secondIndex = i;
}
temp = courses[firstIndex];
courses[firstIndex] = courses[secondIndex];
courses[secondIndex] = temp;
}

void Course_Directory::schedule(int id, int len) {
cout << "Courses student " << id << " is enrolled in: " << endl;
for (int i = 0; i < len; i++) {
    for (int j = 0; j < courses[i].numStudents; j++)
        if (courses[i].IDs[j] == id)
            cout << courses[i].courseNum << " \n";
}
}

Course_Directory.h

#ifndef COURSE_DIRECTORY_H
#define COURSE_DIRECTORY_H

#include <iostream>
using namespace std;

class Course_Directory{

private:
int* deptSize;
string filename;

public:
char courseName[1001];
int courseNum;
int numStudents;
int IDs[1001];
int* choice;
void displayMenu();
Course_Directory();
//Course_Directory(const Course_Directory& original);
//~Course_Directory();
//void run(string);
void coursesInDept(char*, int);
void studentsInCourse(int, int);
void largestClass(int);
void swap2(char*, int, char*, int, int);
void schedule(int, int);
void printAllCourses(int);


};
#include "Course_Directory.hpp"
#endif //COURSE_DIRECTORY_H

main.cpp

#include "Course_Directory.h"

using namespace std;
class Course_Directory;

int main() {

ifstream file;
file.open("input.txt");

char *token;
int i = 0, j;
Course_Directory courses[100];
if (!file.fail()) {
    std::string line;
    while (getline(file, line)) {
        j = 0;
        char *str = const_cast<char *>(line.c_str());
        token = strtok(str, " ");
        while (token != NULL)
        {
            if (j == 0)
                strcpy(courses[i].courseName, token);
            else if (j == 1)
                courses[i].courseNum = atoi(token);
            else if (j == 2)
                courses[i].numStudents = atoi(token);
            else
                courses[i].IDs[j - 3] = atoi(token);
            j++;
            token = strtok(NULL, " ");
            cout << courses[i].courseName << endl;
        }
        i++;
    }
    file.close();
}
else{
        cout << "File not found." << endl;
}
//menu
Course_Directory displayMenu();

return 0;
}

如果我将Course_Directory保留在displayMenu()之外;然后我得到“ displayMenu”没有在这个范围内声明。我不确定是什么问题,或者为什么菜单不会显示。任何帮助将不胜感激!

0 个答案:

没有答案