没有做任何事情的迭代器

时间:2013-08-22 19:16:09

标签: c++ list stl iterator

该计划旨在向学生列出课程。下面列出的问题:

#include <iostream>
using namespace std;
#include "app.h"
#include <conio.h>
#pragma once
App::App()
{
    // Students menu init
    appMenu[0].setNumOfOptions(5);
    appMenu[0].set( 0, "Print all the information for all the courses \n");
    appMenu[0].set( 1, "Print all the information for all the tests  \n");
    appMenu[0].set( 2, "Register to course \n");
    appMenu[0].set( 3, "Get a pdf name of a test \n");
    appMenu[0].set( 4, "Exit \n");

    // Teachers menu init
    appMenu[1].setNumOfOptions(4);
    appMenu[1].set( 0, "Moving test to the database \n");
    appMenu[1].set( 1, "Updating students grades  \n");
    appMenu[1].set( 2, "Print student info \n");
    appMenu[1].set( 3, "Exit \n");

    Student zachi("zachi","kfar monash");
    Teacher ori("ori","beit halevi");
    studentsList.push_back(zachi);
    teachersList.push_back(ori);
    Courses Hedva("Hedva");
    Courses HedvaB("Hedva B",list<Courses*>(Hedva));
    coursesList.push_back(Hedva);
    coursesList.push_back(HedvaB);
    list<Student>::iterator itr=find(studentsList.begin(),studentsList.end(),zachi);
    itr->getSignedCourse().push_back(&Hedva);
}

void App::Run()
{
    int index=-1,id;
    bool exist = false;

    while (!exist)
    {
        cout << "For student press 0 , for teacher press 1"<<endl;
        cin >> index;
        while ((index != 1) && (index != 0))
        {
            cout << "please enter a valid choice 0/1"<<endl;
            cin >> index;
        }
        cout << "Please enter your id: ";
        cin >> id;
        if (index == 0)
        {
            list<Student>::iterator itr=studentsList.begin(),found;     
            for(;itr != studentsList.end();++itr)
                if (itr->getId() == id)
                {
                    found = itr;
                    break;
                }
            if (found != studentsList.end())
            {
                exist = true;
                App::studentsMenu(id);
            }
            else cout<<"The id does not exist";
        }
        else
        {
            list<Teacher>::iterator itr=teachersList.begin(),found;
            for(;itr != teachersList.end();++itr)
                if (itr->getId() == id)
                {
                    found = itr;
                    break;
                }
            if (found != teachersList.end())
            {
                exist = true;
                App::teachersMenu(id);
            }
            else cout<<"The id does not exist";
        }
    }
}

//struct printAllCourses
//{
//  template<typename T>
//  void operator()(const T& printablePointer)
//  {
//      cout << *printablePointer << endl;
//  }
//};

struct printAllCourses
{
    template<typename T>
    void operator()(const T& printablePointer)
    {
        cout << *printablePointer << endl;
    }
};

struct printAllTests
{
    template<typename T>
    void operator()(const T& printablePointer)
    {
        cout << *printablePointer << endl;
    }
};

void App::studentsMenu(int id)
{
    unsigned int l_option = 0;
    int courseId,count;
    while ( l_option != 4)
    {
        l_option = appMenu[0].choose();
        list<Student>::iterator itr3=studentsList.begin(),found;

        for(;itr3 != studentsList.end();++itr3)
                if (itr3->getId() == id)
                {
                    found = itr3;
                    break;
                }

        switch( l_option)
        {
            case 0:      // printing all the courses data
                {
                list<Courses*>::iterator itr2=found->getSignedCourse().begin();
                list<Courses*>::iterator itrEnd=found->getSignedCourse().end();
                for(; itr2 != itrEnd ; itr2++)
                    cout << (**itr2);
                //for_each(found->getSignedCourse().begin(),found->getSignedCourse().end(),printAllCourses());
                system("cls");
                break;
                }
            case 1:     //printing all the test
                for_each(found->getSignedCourse().begin(),found->getSignedCourse().end(),printAllTests());
                system("cls");
                break;
            case 2:     //registering to course
                {
                    system("cls");
                    cout << "Please enter the course number: ";
                    cin >> courseId;
                    list<Courses>::iterator foundCourse=coursesList.begin();
                    for (; foundCourse != coursesList.end() ; ++foundCourse)
                        if (foundCourse->getCourseId() == courseId)
                            break;
                    if (foundCourse == coursesList.end())
                    {
                        cout << "The course number does not exist";
                        break;
                    }
                    for (list<Courses*>::iterator preCoursesItr=foundCourse->getPreCourses().begin(); preCoursesItr != foundCourse->getPreCourses().end() ; preCoursesItr++ )
                    {
                        count = 0;
                        for(list<Courses*>::iterator signedCoursesItr=found->getSignedCourse().begin() ; signedCoursesItr != found->getSignedCourse().end() ; signedCoursesItr ++ )
                            if (preCoursesItr == signedCoursesItr)
                                count ++;
                        if (count < 1)
                        {
                            cout << "You cant register to this course , you do not meet the requierments";
                            break;
                        }
                        else
                        {
                            found->getSignedCourse().push_back(&(*foundCourse));
                            cout << "You have been add to "<<foundCourse->getCourseName()<<endl;
                        }
                    }
                break;
                }
            case 3:
                system("cls");
                return;
        }
    }
}

void App::teachersMenu(int id)
{

}

我在做itr->getSignedCourse().push_back(&Hedva)时遇到问题。 当我要去App::Run时,迭代器没有做任何事......

请帮助我!

0 个答案:

没有答案