C++:切换语句实现
C++: Switch Statement Implementation
我正在尝试在程序的主要部分使用 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_;
};
您的问题是您正在调用类的成员(函数),并且您没有该类的对象。
像这样更改代码:
main()
int userOption;
EmployeeHandler e;
// ....
switch (userOption)
{
case 1:
e.AddEmployee();
break;
//...
似乎有很多
错误,但也许它们只是错别字(当然你应该说出你的错误是什么,"它不起作用"永远不够好)。但一个错误是这样的
int userOption;
while (userOption!=0)
{
...
cout<<"Please enter an option:";
cin>>userOption;
switch (userOption)
{
看到问题了吗?while 循环会在您给出值之前测试 userOption
的值。
简单的解决方法是将您的while循环更改为do ...而循环。
int userOption;
do
{
...
cout<<"Please enter an option:";
cin>>userOption;
switch (userOption)
{
...
}
}
while (userOption != 0);
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 使用简单类型列表实现的指数编译时间.为什么
- c++ 中 if 语句中的多个条件(通过链表实现堆栈)
- golang select语句是如何实现的
- 如果在if语句中实现,函数将存储值
- 如何实现 throw 语句来执行整数值
- 实现多个返回语句 c++ 时出现奇怪的结果
- 无法理解堆栈实现的isEmpty函数中的返回语句
- C++:切换语句实现
- 实现此switch语句的更智能的方式
- 使用语句实现C#的C++等价物
- 在C++中实现无操作语句的便携式方法是什么?
- 使用C++中的跳转表实现switch语句
- 在C++中实现sql语句绑定的最佳方式
- Switch case语句如何在内部实现或工作