I am having problem when trying to do pointer loop in C++. What I am trying to do is, user can keep enter message and the message on the new line will be appended. It will only stop prompting when "." is detected at the beginning of a new line. Here is my main method:
vector <Message*> message_list;
Message* message1 = new Message("Student1", "Student2");
cout << "Enter message text line, enter . on new line to finish: " << endl;
while(getline(cin, message1->get_text_input()))
{
if(message1->get_text_input() == ("."))
{
break;
}
else
{
message1->append(message1->get_text_input());
}
}
}
And this is my .cpp file:
Message::Message(string recipient, string sender)
{
this->recipient = recipient;
this->sender = sender;
}
string Message::get_text_input()
{
return text_input;
}
void Message::append(string text)
{
message += text + "\n";
}
string Message::to_string() const
{
return ("From: " + sender + "\n" + "To: " + recipient + "\n");
}
void Message::print() const
{
cout << message;
}
My header class:
class Message
{
public:
Message(std::string recipient, std::string sender);
std::string get_text_input();
void append(std::string text);
std::string to_string() const;
void print() const;
private:
std::string recipient;
std::string sender;
std::string message;
std::string text_input;
char* timestamp;
};
Does anybody know why is it so? Even ".' is detected, it still wont stop.
Thanks in advance.
getline(cin, message1->get_text_input())compiles? It should not.