2013-09-27 128 views
-1

我試圖在我的程序的主要部分使用switch語句,這裏是我...C++:switch語句實現

main() 
int userOption; 
while (userOption=!0) 
{ 


cout<<"BUSINESS MANAGEMENT system <16102868>"<<endl; 
cout<<"-------------------------------------"<<endl; 
cout<<"EMPLOYEE OPTIONS"<<endl; 
cout<<"1. Add Employee"<<endl; 
cout<<"2. Edit Employee"<<endl; 
cout<<"3. Layoff Employee"<<endl; 
cout<<"4. View Employee List"<<endl; 
cout<<"----------------------------------"<<endl; 
cout<<"SCHEDULING OPTIONS"<<endl; 
cout<<"5. Update Schedule"<<endl; 
cout<<"6. Cancel Schedule"<<endl; 
cout<<"7. View Schedule"<<endl; 
cout<<"8. Export Schedule to CSV"<<endl; 
cout<<"9. Export Schedule to HTML"<<endl; 
cout<<"---------------------------------"<<endl; 
cout<<"0. Quit"<<endl; 
cout<<"----------------------------------"<<endl; 
cout<<"Please enter an option:" 
cin>>userOption 
switch (userOption) 
{ 
case 1: 
    AddEmployee() 
     break; 
case 2: 
    EditEmployee() 
     break; 
case 3: 
    LayoffEmployee() 
     break; 
case 4: 
    DisplayEmployeeList() 
     break; 
case 5: 
    AddSchedule() 
     break; 
case 6: 
    CancelSchedule() 
     break; 
case 7: 
    DisplaySchedule() 
     break; 
case 8: 
    ExportScheduleCSV() 
     break; 
case 9: 
    ExportScheduleHTML() 
     break; 
default: 
    cout<<"Enter a number between 1 and 9:"<<endl; 
} 

我不能完全弄清楚,我」這裏錯了。下面是有大部分是我打電話在它上面的函數的代碼...

class EmployeeHandler 
{ 
    public: 
    void AddEmployee() 
    { 
     std::string firstName; 
     std::string lastName; 
     float payRate; 

     std::cout<<"NEW EMPLOYEE"<<std::endl; 
     std::cout<<"First Name:"<<std::endl; 
     std::cin>>firstName; 
     std::cout<<"Last Name:"<<std::endl; 
     std::cin>>lastName; 
     std::cout<<"Pay Rate:"<<std::endl; 
     std::cin>>payRate; 
     employees_.push_back(Employee(firstName,lastName,payRate)); 
     std::cout<<"**Employee m_employeeCount added"<<std::endl; 
    } 

    void EditEmployee() 
    { 
     std::string newFirst; 
     std::string newLast; 
     float newPay; 
     std::cout<<"Which employee would you like to edit"<<std::endl; 
     int indexEdit = GetSelection(); 
     Employee& employee = employees_[indexEdit]; 
     std::cout << employee << std::endl; 
     std::cout<<"Employee new first name:"<<std::endl; 
     std::cin>>newFirst; 
     std::cout<<"Employee new last name:"<<std::endl; 
     std::cin>>newLast; 
     std::cout<<"Employee new pay rate:"<<std::endl; 
     std::cin>>newPay; 
     employee = Employee(newFirst, newLast, newPay); 
     std::cout<<"** Employee index updated"<<std::endl; 
    } 

    void LayoffEmployee() 
    { 
     int index = GetSelection(); 
     if(employees_[index].GetIsActive()) 
     { 
     std::cout << "Laying off employee:\n" << employees_[index] << std::endl; 
     employees_[index].LayOff(); 
     } 
     else 
     { 
     std::cerr << "Already layed off employee:" << employees_[index] << std::endl; 
     } 
    } 

    void DisplayEmployeeList() 
    { 
     std::copy(employees_.begin(), employees_.end(), std::ostream_iterator<Employee>(std::cout, "\n")); 
    } 

    int GetSelection() 
    { 
     std::size_t indexNumber; 
     std::cout << "Select an employee from the list below by specifying its number:" << std::endl; 
     DisplayEmployeeList(); 

     do{ 
      while(!std::cin >> indexNumber) 
      { 
      std::cerr << "Select a number..." << std::endl; 
      } 
      if(indexNumber >= employees_.size()) 
      { 
      std::cerr << "Select a number within range of list below:" << std::endl; 
      DisplayEmployeeList(); 
      } 
     } 
     while(indexNumber >= employees_.size()); 
     return indexNumber; 
    } 

    Employee& operator[](std::size_t index) 
    { 
     return employees_[index]; 
    } 

    const Employee& operator[](std::size_t index) const 
    { 
     return employees_[index]; 
    } 

    std::size_t EmployeeCount() const 
    { 
     return employees_.size(); 
    } 

    private: 
    std::vector<Employee> employees_; 
}; 
+2

註釋掉選項3肯定... – Bathsheba

+1

您是否收到錯誤?不編譯?有什麼問題?實際結果與預期結果是什麼? – nhgrif

+0

@Bathsheba - rimshot! – EkoostikMartin

回答

3

您的問題是要調用類的成員(功能),並且你沒有的對象那個班。

更改您這樣的代碼:

main() 
int userOption; 

EmployeeHandler e; 
// .... 

switch (userOption) 
{ 
case 1: 
    e.AddEmployee(); 
    break; 
//... 
2

有似乎是一個很多錯誤,但也許他們只是打字錯誤(當然你應該你的錯誤是什麼,「這是行不通的'從來都不夠好)。但有一個錯誤是這樣的

看到問題了嗎?在給定值之前,while循環會測試userOption的值。

簡單的修復就是將你的while循環變成do ... while循環。

int userOption; 
do 
{ 
    ... 

    cout<<"Please enter an option:"; 
    cin>>userOption; 
    switch (userOption) 
    { 
     ... 
    } 
} 
while (userOption != 0);