Need helping determining a return statement

Hey everyone!

I am almost done with this program, but I am unable to determine what one of the functions returns outside of the 'for' loop.

The specific place in the code I am having trouble at is at line 74.

Here's the code:

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

#include <iostream>
#include <string>
#include <vector>
using namespace std;


class Tweet
{
public:
    Tweet(); //constructor
    Tweet(string s, string m); //constructor
    string getmessage(); //accessor
    string getsubject(); //accessor
    void newmessage(string newmessage);
private:
    string message;
    string subject;
};

Tweet::Tweet()
{
    message = "";
    subject = "";
}

Tweet::Tweet(string s, string m)
{
    subject = s;
    message = m;
}

string Tweet::getmessage()
{
    return message;
}

string Tweet::getsubject()
{
    return subject;
}

void Tweet::newmessage(string newmessage)
{
    message = newmessage;
}

class Twitter
{
public:
    Twitter(); //creates empty twitter
    void addtweet(Tweet newmessage);
    Tweet gettweet(string subject);
private:
    vector<Tweet> messages;
};

void Twitter::addtweet(Tweet newmessage)
{
    messages.push_back(newmessage);
}

Tweet Twitter::gettweet(string subject)
{
    for(int i=0;i<messages.size();i++)
    {
        if(messages[i].getsubject() == subject)
        {
            return messages[i];
        }
    }
//what can I put as a return statement here? Help!
    
}

int main()
{
    Twitter t;
    Tweet m("Finished program","Works perfectly!");
    t.addtweet(m);
}
Last edited on
Since you've that declared gettweet returns type Tweet, the correct thing to return is an object of type Tweet. In this case, since you didn't find a match on subject, you probably want to return an empty Tweet.

Another option would be to change the type of gettweet to return a pointer to a Tweet. If you don't find a tweet matching the subject, return NULL. Then where you call gettweet, test if the returned pointer is NULL and act accordingly.

I understand that I will be returning an object of type Tweet, but I am confused as to how to return the empty Tweet. Maybe I am over thinking it. O.o
Last edited on
Give Twitter it's own Tweet:
1
2
3
4
5
6
7
8
9
10
class Twitter
{
public:
    Twitter(); //creates empty twitter
    void addtweet(Tweet newmessage);
    Tweet gettweet(string subject);
private:
    vector<Tweet> messages;
    Tweet emptyTweet;  //Here
};


You may want this in your constructor:
Twitter() :emptyTweet("Error", "Could Not Find Subject") {/* other code? */}

Then your problem is solved:
1
2
3
4
5
6
7
8
9
10
11
Tweet Twitter::gettweet(string subject)
{
    for(int i=0;i<messages.size();i++)
    {
        if(messages[i].getsubject() == subject)
        {
            return messages[i];
        }
    }
    return emptyTweet;
}


Last edited on
Ah thank you much kind sir/ma'am! :)
Last edited on
Topic archived. No new replies allowed.