I am having some problem when trying to do classes for C++. This is my header file.h:
#include <iostream>
#include <string>
#ifndef MESSAGES__H__
#define MESSAGES__H__
class Message
{
public:
Message(std::string recipient, std::string sender);
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;
};
#endif
And when I run the main method, the getline(cin,) is giving me some error message:
int main()
{
vector <Message*> message_list;
Message* message1 = new Message("Student1", "Gabriel");
cout << "Enter message text line, enter . on new line to finish: " << endl;
while(getline(cin, text_input))
{
}
}
The getline method is giving me no instance of overloaded function. Also, from the same line, the text_input is showing identifier is undefined. I thought I declared in .h class already?
Thanks in advance.
Updated portion
Now all the error has been fixed:
vector <Message*> message_list;
Message* message1 = new Message("Saiful", "Gabriel");
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());
}
}
Inside the while loop, once "." is detected at the beginning of the new line, it supposingly will stop. However, no matter how many times I entered "." at the new line, it just keep prompting. Anybody know why?
See Question&Answers more detail:os