Stopping the while loop

Hello, is there a way to stop the while loop without changing the functions. I was thinking of doing if (events.back(-1)) {tf = false}. But I can't make it work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include "Conference.h"
#include "Event.h"
#include "Person.h"
#include "Lesson.h"
#include <string>
#include <vector>
using namespace std;

int returnSelection()
{
    cout << "Enter event type \n(1 for Lesson, 2 for Conference, -1 to exit):";
    int selection;
    cin >> selection;
    return selection;
}

Person * createPerson(string title)
{

    cout << title << ":\nEnter the first name: ";
    string fn; cin >> fn;
    cout << "Enter the last name: ";
    string ln; cin >> ln;
    cout << "Enter the phone number: ";
    string pn; cin >> pn;

    Person *personptr = new Person(fn, ln, pn);
    return personptr;
}
Event * createLesson()
{
    cout << "LESSON CREATION:\n";
    cout << "Enter the starting data: ";
    string sd; cin >> sd;
    cout << "Enter the ending data: ";
    string ed; cin >> ed;
    cout << "Enter the location: ";
    string loc; cin >> loc;
    cout << "Enter the course name: ";
    string cn; cin >> cn;

    Lesson *lessonptr = new Lesson(sd, ed, loc, cn, *createPerson("Instructor"));
    return lessonptr;
  //  cout << lesson.toString();
}
Event * createConference()
{
    cout << "CONFERENCE CREATION:\n";
    cout << "Enter the starting data: ";
    string sd; cin >> sd;
    cout << "Enter the ending data: ";
    string ed; cin >> ed;
    cout << "Enter the location: ";
    string loc; cin >> loc;
    cout << "Enter the Conference title: ";
    string tl; cin >> tl;

    cout << "Enter the of panelist: ";
    int num; cin >> num;

    Conference *conferenceptr = new Conference(sd, ed, loc, tl, num, createPerson("Panelist"), *createPerson("Moderator"), *createPerson("Speaker"));
    return conferenceptr;
}
Event * createEvent(int selection)
{
    if(selection == 1)
        return createLesson();
    else if (selection == 2)
        return createConference();
    else return 0;
}
int main() {

    cout << "*******Welcome to Event Organizer!**********\n";
    vector <Event*> events;
    bool tf = true;
    while (tf = true)
    {
        events.push_back(createEvent(returnSelection()));
    }

    for (int i=0; i<events.size(); i++)
    cout << events.at(i)->toString();

    return 0;
}
Last edited on
you can add a quit event in return selection and check events[back] for it in the while loop after the push back.
or you can add a 'enter more events y/n' to the while loop and stop if they say no.
you have it already, the -1 in selection. That converts to a zero in the push back, so you just need to actually CHECK that.
Last edited on
is there a way to stop the while loop without changing the functions

As far as I can see, no, there isn’t.
Your function createEvent is supposed to return a Event *, so returning an integer is simply wrong. Therefore it must be changed.

If you really fancy the current structure, you could return nullptr and catch the following exception (but it sounds pretty awful even before trying it).
Anyway, I’d be surprise at discovering your code compiles, so I can’t see why not amending it.
Topic archived. No new replies allowed.