std::string str;
uint food;
fd.open("Costs.csv",std::fstream::in);
if (!(fd.is_open())){
return 0;
}
std::getline(fd, str);
std::getline(fd, str);
std::stringstream strStream(str);
std::cout << str << std::endl;
std::getline(strStream, food, ',');
So, on the last one i have these error:
**error: no matching function for call to 'getline(std::stringstream&, uint&, char)'
From https://en.cppreference.com/w/cpp/string/basic_string/getline
Parameters for getline(stream,string,char) and not
getline(stream,uint,char).
When you encounter such error means function exists but the arguments are of wrong type or/and in wrong order.
Related
Why does this program print out blank?
string str;
stringstream ss(str);
ss << "A string";
cout << str; // just blank
stringstream is not going to modify the argument you pass to its constructor.
Instead, you can get the current buffer from the stringstream by calling its str member function:
cout << ss.str();
Next time, consider reading the documentation.
Is it possible to do a function call with an argument of a string using cin.get() or cin.getline()?
If not, how could i get user input from the console using a function call like this?
void setString(string str);
string stringText;
void setString(string str) {
stringText = str;
}
int main() {
setString(cin.getline());
return 0;
}
There is a free function called getline that takes an input stream and a string as parameter, the second being an out parameter. It returns the stream object that was passed in, not the string.
You could however write your own function that returns a string
std::string getConsoleLine()
{
std::string str;
std::getline( std::cin, str );
return str;
}
Note that, just like any other input stream, cin can be in a failed state from a period read. You could make your function clear any flags and ignore anything left in it first.
std::string getConsoleLine()
{
// as a precaution add this lines
std::cin.clear();
std::cin.ignore( std::numeric_limits<streamsize>::max() );
std::string str;
std::getline( std::cin, str );
return str;
}
Then you could pass it to your function.
setString( getConsoleLine() );
cin.getline() does not return std::string so you cannot pass it directly as a parameter to a function.
You need to store the input in a variable
std::string str;
cin >> str;
Then pass that variable to the function:
setString(str);
Probably you want something like this:
#include <string>
#include <iostream>
int main()
{
std::string stringText;
// puts the input you type into stringText
std::getline(std::cin, stringText);
// Now do what you like with stringText
std::cout << "You typed: " << stringText << '\n';
return 0;
}
I tried to create a function to identify matched line from a string. My whole string is saved in strStart and strToMatch contains the search string. Following is my code
void ExpertContextUser::removeMatchedString() {
String line;
String strStart="Testing\nReturns\nrelated\nresources";
String strToMatch="Test";
istringstream streamAddtText(strStart);
while(std::getline(streamAddtText, line)) {
cout << line << "Function" << endl;
if(line.index(strToMatch) > 0) {
TraceMessage <<" Test Success" << endl;
}
}
}
when i am compiling my code, i am getting following error
"../user_model_impl.cxx", line 234: error #2289: no instance of constructor
"std::basic_istringstream<_CharT, _Traits,
_Allocator>::basic_istringstream [with _CharT=char,
_Traits=std::char_traits, _Allocator=std::allocator]"
matches the argument list
argument types are: (RWCString)
istringstream streamAddtText(strStart);
I am unable to find reasons for this error.
The error happens because the istringstream constructor takes a std::string, not a RWCString. You need to provide a conversion from RWCString to std::string if you want this to work.
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?
To fix "text_input is showing identifier is undefined"
You need to change
while(getline(cin, text_input))
to
while(getline(cin, message1->text_input))
Possibly this will fix the first error.
Try changing your loop like this:
while(getline(cin,&message1->text_input))
{
}
If I remember correctly, getline function looks like this:
getline(isstream& stream, string& string)
It feels as if you are over complicating things. Just use a temporary variable in getline. If the input is a "." then break, otherwise append the line to the Message object. As a consequence, you should be able to remove the text_input member variable from your Message header file.
std::vector<Message*> message_list;
Message* message1 = new Message("Saiful", "Gabriel");
std::cout << "Enter message text line, enter . on new line to finish: " << std::endl;
std::string input = "";
while(getline(std::cin, input))
{
if(input == ".")
{
break;
}
else
{
message1->append(input);
}
}
I don't understand why I got this error message at line "if (sentToGroup(client_fd, ss) == -1){"
While running the following code:
stringstream ss;
// ss.get();
ss << "test";
if (sentToGroup(client_fd, ss) == -1){
perror("Fail sending to group");
}
I got the error message below, why??
Initializing argument 2 of ‘int sentToGroup(int, std::stringstream)’
The sentToGroup function is as below:
int sentToGroup(int sender_fd, stringstream str){
char buffer[MAX];
stringstream sender;
sender << int(sender_fd) << "> " << str;
int bytes = recv(sender_fd, buffer, sizeof(buffer), 0);
for (int c = printerCnt; c < sizeof(printer); c++){
if (printer[c] != sender_fd){
if (send(printer[c], sender, bytes, 0) == -1){
return -1;
}
}
}
return 0;
}
It's not clear how that message is produced, but stringstream is not copyable. You should pass by reference and copy data out of it, if you do not wish to pass a stringstream but not modify it.
However, you typically should not pass a stringstream at all. If the purpose is to pass a string into the function, use string. If the purpose is to treat it as a stream, use istream & or ostream & or iostream &. You can still pass the same stringstream because of polymorphism.
I'm not really sure what you're doing here, but changing stringstream to iostream & should fix the immediate problem and possible later issues too.
You cannot copy stringstream, try to pass it by reference:
int sentToGroup(int sender_fd, stringstream& str)