⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 数据结构ssd5 re3
💻 CPP
字号:
/*
 * 063420 魏政
 */



// For Visual C++ users:
// Visual C++ does not implement checked exceptions, and the compiler will
// show warning messages when functions throws exceptions. The following
// warning pragma ignore those compiler messages.
//
// GCC users can remove this #pragma warning.
#pragma warning(disable: 4290)

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

#include "course.h"
#include "safearray.h"

void init_courses(safearray<course> &);
void display_menu(safearray<course> &);

const int NUM_COURSES = 10;
const int QUIT = 99;

int main(int argc, char* argv[]) {

    safearray<course> courses(NUM_COURSES);
	init_courses(courses);
    int choice = 0;
    while (choice != QUIT) {

        display_menu(courses);
        cout << "Enter number of course to see more information on\n";
        cin >> choice;
		//保证cin输入的为数字
		if(!cin)
		{
			cout << "请输入数字" << endl;
			cin.clear();   
			cin.ignore(); //清空cin缓冲 
			continue;
		}
        cout << "\n";
		//输入不等于99,进行输出
		if(choice != QUIT)
		{
			try
			{
				cout << courses[choice - 1] << "\n\n";
			}
			catch (out_of_range& e) //数组越界抛出异常
			{
				cerr << e.what() << endl;
			}
		}		
    }

    return EXIT_SUCCESS;
}

void display_menu(safearray<course> & courses) {

    for (int i = 1; i <= NUM_COURSES; i++) {
        cout << i << ". " << courses[i - 1].name << "\n";
    }
    cout << "99. Quit\n";
}

void init_courses(safearray<course> & courses) {

    ifstream inf("courses.txt");
    if (! inf) {
        cerr << "Could not open courses.txt" << endl;
        exit(EXIT_FAILURE);
    }

    for (int i = 0; i < NUM_COURSES; i++) {
        inf >> courses[i];
    }

    inf.close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -