Struct卡在输入循环中的功能

时间:2019-03-31 16:57:31

标签: c++ function struct

我正在编写一个用于上课的基本程序来管理事件列表。我有一个带结构的void函数,用于输入陷入循环的新数据。创建该结构,并在完成数据输入部分后循环返回并重复。

我尝试将其包含在do{}while loop中,并尝试通过一系列if语句进行分解。是否有命令或对话框退出或终止该结构?

根据要求,最低要求:

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std;

void menu();
void AddANewEvent();

struct Event{    //struct to hold event data, name, location, description, date, start time, and duration

     string EventName;
     string EventLoc;    // location for the event
     string EventDesc;   //describe the event
     string Date;        //date in DD/MM/YYYY format
     string StartTime;    //time to start in 24H format
     float Duration;       //time in whole hours
};

int main()
{
menu();
//AddANewEvent();  removed this and it works...
return 0;
}

void menu(){
     int action = 0;
     cout << "Event List Management Menu" << endl;
     cout << "1.  Add new event" << endl;                                  //  add a new event, almost feeling positive about this part but it may be delirium.
     cout << "Please select an action (1): " ;                                     //prompt user for input
     cin >> action;                                                                                //storing input

     switch(action)
     {
          case 1: AddANewEvent(); break;
     }}

void AddANewEvent(){
          Event evn;
                    cout << "Enter event name: " << endl;
                    cin >> evn.EventName;
                    cout << "Enter event location: " << endl;
                    cin >> evn.EventLoc;
                    cout << "Enter event description: " << endl;
                    cin >> evn.EventDesc;
                    cout << "Enter date: " << endl;
                    cin >> evn.Date;
                    cout << "Enter start time: " << endl;
                    cin >> evn.StartTime;
                    cout << "Enter duration in hours: " << endl;
                    cin >> evn.Duration;
} //end of the function

这是我的问题出处:

void AddANewEvent(string &fileName){
     Event evn;
     cout << "Enter event name: " << endl;
     cin >> evn.EventName;
     cout << "Enter event location: " << endl;
     cin >> evn.EventLoc;
     cout << "Enter event description: " << endl;
     cin >> evn.EventDesc;
     cout << "Enter date: " << endl;
     cin >> evn.Date;
     cout << "Enter start time: " << endl;
     cin >> evn.StartTime;
     cout << "Enter duration in hours: " << endl;
     cin >> evn.Duration;
} //end of the function

我认为我的结构还可以:

struct Event{    //struct to hold event data, name, location, description, date, start time, and duration

     string EventName;
     string EventLoc;    // location for the event
     string EventDesc;   //describe the event
     string Date;        //date in DD/MM/YYYY format
     string StartTime;    //time to start in 24H format
     float Duration;       //time in whole hours
};

在该程序的其他部分中,我成功地实现了类似的段,其中的功能可以继续或退出,是或否的问题。但是,在此段中它将不起作用。理想情况下,在处理完此部分代码后,它将提示重复,否则返回主菜单。到目前为止,我所有的尝试都无法返回,只能无意地重复对话。

1 个答案:

答案 0 :(得分:0)

我首先要在Event类中添加一个用户定义的构造函数,以便您可以使用emplace_back将事件存储在std::vector中。请记住,如果您决定使用addNewEvent(),则emplace_back将触发一个副本,而不是使用move构造函数。

使用简单的while循环并在用户不想继续添加更多事件时中断它。

示例:https://rextester.com/IAO89219