2015-09-03 48 views
0

我有一個程序根據用戶輸入創建來自兩個不同類的對象。如果用戶是學生,則學生課程的一個對象將被創建,學生將進入他們正在接受的課程。我有一個while循環,詢問用戶他們每次進入課程後是否想要進入其他課程。如果此用戶n,這是應該結束循環的條件,程序與exit code 11停止: C++:遇到環境條件時程序崩潰

這不應該是這樣的。 while循環後有更多的代碼行,並且循環結束後程序不應該結束。這裏是有問題的,而循環功能:

void createStudent (char student_name[], int student_age) 
{ 
    Student student; 

    student.setName(student_name); 
    student.setAge(student_age); 

    char courses[8]; 
    char course_loop = ' '; 
    int count = 0; 

    cout << "What courses are you taking? " 
     "(Enter course prefix and number with no spaces):\n\n"; 

    while (tolower(course_loop) != 'n') 
    { 
     cout << "Course #" << count + 1 << ": "; 
     cin.ignore(); 
     cin.getline(courses, 9); 
     //student.sizeOfArray(); // Increment the array counter if addCourse reports that the array was not full 
     student.addCourse(courses, count); 

     cin.clear(); 

     if (student.addCourse(courses, count)) 
     { 
      cout << "\nHave another course to add? (Y/N): "; 

      cin.clear(); 

      cin.get(course_loop); 
     } 
     else 
     { 
      cout << "You have exceeded the number of courses you're allowed to enter. Press any ENTER to continue..."; 
      cin.ignore(); 
      course_loop = 'n'; 
     } 

     count++; 
    } 

    cout << student; 
    student.printCourseNames(); 
} 

這裏是程序的其餘部分:

// main.cpp 
//----------------------- 

#include <iostream> 
#include "Person.h" 
#include "Student.h" 
using namespace std; 

void createStudent(char [], int); 
void createPerson(char [], int); 

int main() 
{ 
    char name[128], student_check; 
    int age; 

    cout << "Please state your name and age: \n\n" 
     << "Name: "; 
    cin.getline(name, 128); 
    cout << "Age: "; 
    cin >> age; 

    cout << "\n\nThanks!\n\nSo are you a student? (Y/N):"; 
    cin.ignore(); 
    cin.get(student_check); 

    switch (student_check) 
    { 
     case 'y': 
     case 'Y': 
      createStudent(name, age); 
      break; 
     case 'n': 
     case 'N': 
      createPerson(name, age); 
      break; 
     default: 
      break; 
    } 
} 

// createStudent function with while-loop posted above comes after this in main.cpp 

// student.h 
// ------------------ 

#include "Person.h" 
#ifndef PA2_STUDENT_H 
#define PA2_STUDENT_H 


class Student : public Person 
{ 
    public: 
     Student(); 
     bool addCourse(const char*, int); 
     void printCourseNames(); 
     void sizeOfArray(); 

    private: 
     const char* m_CourseNames[10] = {0}; 
     int array_counter; 
}; 


#endif 

// student.cpp 
//------------------ 

#include <iostream> 
#include "Student.h" 

using namespace std; 

Student::Student() : array_counter(0) {} 

void Student::sizeOfArray() 
{ 
    array_counter++; 
} 

bool Student::addCourse(const char* course, int index) 
{ 
    if (index < 9) 
    { 
     m_CourseNames[index] = course; 
     return true; 
    } 
    else if (index == 9) 
     return false; 
} 

void Student::printCourseNames() 
{ 
    if (array_counter != 0) 
    { 
     cout << ", Courses: "; 

     for (int count = 0 ; count < 10 ; count++) 
      cout << m_CourseNames[count] << " "; 
    } 
} 

我使用的克利翁爲我的IDE,有沒有什麼幫助。

+0

你的'''重載是什麼樣的? – molbdnilo

回答

1

你需要添加可發送Student對象輸出流像cout的功能。這是實現的方式是通過定義這樣的功能:

ostream& operator<<(ostream& os, const Student& student) 
{ 
    os << "Name: " << student.name << ", Age: " << student.age << ", Courses: " << student.m_CourseNames; 
    return os; 
} 

此功能允許您使用<<運營商將數據發送到輸出流,像cout,從Student類。它將輸出流作爲一個參數,將學生作爲另一個參數。它打印學生的信息,然後返回輸出流。這樣就可以鏈接操作員以便一次打印多個信息。

+0

我實際上已經註釋掉了'cout << student'這一行,並且我得到了同樣的錯誤信息。顯示操作員超載不是原因。 – jshapy8

1

編輯:與該返回相關的錯誤與緩衝區有關。

您的輸入緩衝區char courses[8]不足以處理來自cin.getline(courses, 9)的輸入,並且導致cin緩衝區發生溢出。 cin.clear()黑客正在恢復輸入流以允許cin.get(course_loop)的輸入,但函數嘗試返回時異常仍未處理。

變化char courses[8]char courses[10](在Studentm_CourseNames陣列相匹配),它應該正常工作。

更新代碼:

void createStudent(char student_name[], int student_age) 
{ 
    Student student; 

    student.setName(student_name); 
    student.setAge(student_age); 

    char courses[10]; 
    char course_loop = ' '; 
    int count = 0; 

    cout << "What courses are you taking? " 
     "(Enter course prefix and number with no spaces):\n\n"; 

    while (tolower(course_loop) != 'n') 
    { 
     cout << "Course #" << count + 1 << ": "; 
     cin.ignore(); 
     cin.getline(courses, 9); 
     cin.clear(); 

     //Removed the duplicate addCourse() call 
     if (student.addCourse(courses, count)) 
     { 
      student.sizeOfArray(); //Needed for printCourseNames() functionality 
      cout << "\nHave another course to add? (Y/N): "; 

      cin.clear(); 

      cin.get(course_loop); 
     } 
     else 
     { 
      cout << "You have exceeded the number of courses you're allowed to enter. Press any ENTER to continue..."; 
      cin.ignore(); 
      course_loop = 'n'; 
     } 

     count++; 
    } 

    cout << student; 
    student.printCourseNames(); 

    return; 
}