【发布时间】:2019-03-31 16:57:31
【问题描述】:
我正在为班级编写一个基本程序来管理事件列表。我有一个带有结构的 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
};
在这个程序的其他部分,我已经成功实现了类似的部分,在这些部分中,可以通过是或否问题继续或退出该功能。但是,在这个部分它不会起作用。理想情况下,在处理完这部分代码后,它会提示重复,否则返回主程序。到目前为止我所有的尝试都无法返回,只是无意中重复了对话。
【问题讨论】:
-
请在此处发布minimal reproducible example 以重现您的问题。另外请问您在逐行调试代码时观察到了什么?
-
您似乎遇到了循环问题,但您的问题中没有包含出现问题的代码。我建议你阅读how to make a minimal, verifiable example。
-
AddANewEvent创建一个新的Event,它永远不会返回,所以它只消耗输入而不产生任何东西。 -
@πάντα ῥεῖ 添加了最低限度。它没有给我任何警告或标志,它只是循环。 davidlowryduda 没有故意循环,但无论如何它都会循环... molbdnilo 我删除了在发布之前将其写入文件的部分。
-
我通过从
main中删除AddANewEvent函数来修复它。我不明白为什么它在循环,但这已经解决了问题......