有一个cin函数的无限循环

时间:2014-05-24 15:50:33

标签: c++

所以我正在做一个小项目的主要想法是有图书馆的项目(书籍,杂志)和学生和程序的主菜单添加学生,图书馆项目删除学生图书馆项目.. .etc,所以当我试图添加一本书时,它工作得很完美,但当我试图添加杂志时,它进入无限循环并且不让我输入值来输入其他选项而且我的第一个程序我可能有使用dev c ++时出现了很多错误,而且还没有完成代码::

#include<iostream>
#include<string>
using namespace std;
char enter;
string none,none1,none2;
int is,iff;
int B_num=0;//books number 
int M_num=0;//magazine number
int S_num=0;//student number

class Clibrary_items{//the clibrary class is a class that got the common elemt of the             magazine and the book
protected:
string name,publisher;
string av="availabil";//the availability of a book or a magazine 
 };
class Cbook: public Clibrary_items{
private:
string author_name;
public:
virtual void set(string n,string p,string a)
{
name=n;
publisher=p;
author_name=a;
}
virtual void print()//the the printer function that print the pointed book 
{
    cout<<"the name is::"<<name<<endl;
    cout<<"the publisher is::"<<publisher<<endl;
    cout<<"the author name is::"<<author_name<<endl;
}
 };
class Cmagazine: public Clibrary_items{
private:
int isbn;
public:
virtual void set(string na,string pu,int i)
{
    name=na;
    publisher=pu;
    isbn=i;
}
virtual void print()//the the printer function that print the pointed magazine 
{
    cout<<"the name is::"<<name<<endl;
    cout<<"the publisher is::"<<publisher<<endl;
    cout<<"the issue number is::"<<isbn<<endl;
}
  };
  class Cstudent{
friend class Clibrary;
private:
string name="";
int ID;
public:
void set(string sn,int number)
{ID=number; 
name=sn;}
void print_student()//the the printer function that print the pointed student 
{    cout<<"ID:"<<ID<<"  ";
    cout<<"student name is::"<<name<<endl;}
  };
  class Clibrary//the main class 
  {
public:
Cbook book[100];//dynamic array for books
Cmagazine magazine[100];//dynamic array for magazine
Cstudent student[100];//dynamic array for student
void print()//the main function that print the main menu and get you to everthing
{
while(enter!='q')
{
cout << "[a] Add student" << endl;
cout << "[b] Remove student" << endl;
cout << "[c] Add Item to the Library" << endl;
cout << "[d] Remove Item from the Library" << endl;
cout << "[e] Borrow Item from the Library" << endl;
cout << "[f] Return Item to the Library" << endl;
cout << "[g] Show all Library Items" << endl;
cout << "[i] Show all reserved Library items" << endl;
cout << "[j] Show all free Library items" << endl;
cout << "[k] Show all students" << endl;
cout << "[l] Show all students who borrowed items" << endl;
cout << "[m] Find the student who borrowed a specific item" << endl;
cout << "[n] Show all Items borrowed by student" << endl;

cout << "q) Quit to Windows" << endl;
cin>>enter;
switch (enter)
{
case 'a':
        cout<<"please enter the name::";
        cin>>none;
        student[S_num].set(none,S_num);
        S_num++;
        break;
case 'k':
        for(int i=0;i<S_num;i++)
        {
        student[i].print_student();
        }    
        break;
case 'b':
        cout<<"please enter the name of the student you want to remove:";
        cin>>none;
        for(int i=0;i<S_num;i++)
        if(none==student[i].name)
        student[i].name="";
        break;
case 'c':
        cout<<"[1].add book"<<endl;
        cout<<"[2].add magazine"<<endl;
        cin>>iff;
        if(iff==1)
        {
        cout<<"please enter the name of the book:"<<endl;
        cin>>none;
        cout<<"please enter the publisher:"<<endl;
        cin>>none1;
        cout<<"please enter the author name:"<<endl;
        cin>>none2;
        book[B_num].set(none,none1,none2);
        }   
        if(iff==2)
        {
        cout<<"please enter the name of the magazine:"<<endl;
        cin>>none;
        cout<<"please enter the publisher:"<<endl;
        cin>>none1;
        cout<<"please enter the issue number:"<<endl;
        cin>>is;
        magazine[M_num].set(none,none1,is);
        }
        break;  
}
   }
   }
  };
    main()
  {

cout <<"******  ******  ******  ******  ******  ******" << endl;  
cout <<"*                                            *" << endl;  
cout <<"******    Library Management System     ******" << endl;  
cout <<"*                                            *" << endl;  
cout <<"*         BY:  <M.Saed Ramadan>              *" << endl;  
cout <<"*                                            *" << endl;  
cout <<"******  ******  ******  ******  ******  ******" << endl; 
Clibrary librarby;
librarby.print();

 }

1 个答案:

答案 0 :(得分:1)

为了使您的代码更易于管理,我建议您在switch语句中使用函数调用:

  switch (selection)  // "enter" is such a bad name.
  {
    case 'a':
      Add_Student();
      break;
    case 'b':  
      Remove_Student();
    //...
    default:
      cout << "Invalid menu selection, try again.\n";
      break;
  }

这种简化(为了便于阅读)导致了下表驱动的菜单系统:

// Declare a function pointer syntax
typedef void (*Function_Pointer)(void);

struct Menu_Entry
{
  char             selection_char;
  Function_Pointer menu_function;
};

// The table
const Menu_Entry First_Menu[] =
{
  {'a', Add_Student}, // A selection entry
  {'b', Remove_Student},
  //...
};
const unsigned int Number_Of_Selections =
  sizeof(First_Menu) / sizeof(First_Menu[0]);

// The lookup engine
char selection;  // This you input from user.
for (unsigned int i = 0; i < Number_Of_Selections; ++i)
{
  if (selection == First_Menu[i].selection_char)
  {
    // Execute the function associated with the selection
    First_Menu[i].menu_function();
    break;
  }
}

表格系统的一个优点是您可以通过以下方式轻松添加菜单选择项目:

  1. 创建执行选择的功能。
  2. 在表格中添加Menu_Item条目。
  3. 那就是它。非常简单。

    这使您可以让菜单选择过程适用于一个选择,然后在另一个选择上工作;一块一块。

    通过将函数放在单独的文件中,您只需编译查找表源和新的选择源文件。所有其他代码保持不变。更快的构建时间。