How does stringstream work with <<? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to use << as a means of moving integers into a stringstream. There must be something fundamental and basic I am overlooking. The simplest of code does not even compile:
std::stringstream ss;
ss << "simple test ";
produces this error:
error C2297: '<<' : illegal, right operand has type 'const char [13]'

That is not a valid C++ program.
First, you need to include sstream. Then, you need to put that expression with << into a function.
Like this:
#include <sstream>
int main()
{
std::stringstream ss;
ss << "simple test ";
}

This worked:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
stringstream ss;
string s;
ss << "simple test ";
s = ss.str();
cout << s;
return 0;
}

Related

Getting input in a String in cpp [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Hello I want to get some input in a string in cpp and I am getting and error. Here is the code:
#include <iostream>
int main() {
using namespace std;
string name;
cout << "Type your name:";
cin >> name;
cout << "Your name is: " << name;
return 0;
}
I am building the project and I get this error:
Test1.cpp:10:6: error: invalid operands to binary expression
It is this line: cout << "Type your name:";
What am I missing here ? It is the first time when I am using c++
You need to include header <string>
#include <string>
<iostream> does not include <string>. Hence, you also need to include <string> in order to use std::string.
#include <iostream> // for std::cout, std::cin
#include <string> // for std::string

C plus plus expected unqualified id [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
On line five of my code, the compiler raises an error. "error: expected unqualified-id
std::string send(message); {"
What error is this? How do I fix it?
Here's my code:
#include <iostream>
#include <fstream>
std::string message = "blank";
std::string send(message); { // here it raises the error
std::ofstream MyFile("messagepy.txt");
std::ofstream MyFile; << "Files can be tricky, but it is fun enough!";
}
// // // // // // // // // // // // // //
int main() {
std::cout << "defining is done.";
}
You have some syntax errors in there. Not sure what your code is meant to do, but here is my guess:
#include <iostream>
#include <fstream>
#include <string>
void send(std::string message)
{
std::ofstream MyFile("messagepy.txt");
MyFile << message;
MyFile << "Files can be tricky, but it is fun enough!";
}
int main()
{
std::string message = "blank";
send(message);
std::cout << "defining is done." << std::endl;
}
I believe the error is caused because you didn't #include <string>.
But there are other problems too. It sort of looks like you meant to create a send function but the semicolon ; is causing it to look like a declaration of a global variable send constructed with message, and then a code block which is a syntax error because it isn't part of a function.

request for member 'resize' in 'abs', which is non-class type 'char**' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i have this code, and the compiler says in this line
abs.resize(jlh);
request for member 'resize' in 'abs', which is non-class type 'char**'.
edit:i need abs and mhs to be in char so i can encrypt in the next step.
what do i have to do? thanks!
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
int jlh,x,y;
char **mhs=new char*[100];
char **abs=new char*[100];
cout<<"enter students total: ";
cin>>jlh;
abs.resize(jlh);
for(x=0;x<jlh;x++){
cout<<"enter students name: ";
mhs[x] = new char[1024000];
cin>>mhs[x];
cout<<"enter students presensi: ";
cin>>abs[x];
cout<<endl;
}
}
Like this
#include <vector>
#include <string>
int main()
{
std::vector<std::string> abs(100);
...
abs.resize(jlh);
...
}
For full details consult a book on C++. You really need one if you are going to learn C++. You cannot learn C++ by guess work.

getline function is not defined error.Can't hold the veriable in string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
cout << "Type your message: ";
cin.ignore();
getline(cin, message);
return 0;
}
It gives getline function is not defined error.
I just want to hold the writed string inside the message veriable.
Here's the picture
"...string, stdafx.h and iostream...": "stdafx.h" must be the first line. The compiler will ignore everything above it (<string> will not be included, so the compiler will complain: getline not found).
I do not know what you're trying to do but the code below will compile and run:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
cout << "Type your message: ";
cin.ignore();
getline( cin, message );
return 0;
}

How to print a string in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried this, but it didn't work.
#include <string>
string someString("This is a string.");
printf("%s\n", someString);
#include <iostream>
std::cout << someString << "\n";
or
printf("%s\n",someString.c_str());
You need to access the underlying buffer:
printf("%s\n", someString.c_str());
Or better use cout << someString << endl; (you need to #include <iostream> to use cout)
Additionally you might want to import the std namespace using using namespace std; or prefix both string and cout with std::.
You need #include<string> to use string AND #include<iostream> to use cin and cout. (I didn't get it when I read the answers). Here's some code which works:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string name;
cin >> name;
string message("hi");
cout << name << message;
return 0;
}
You can't call "printf" with a std::string in parameter.
The "%s" is designed for C-style string : char* or char [].
In C++ you can do like that :
#include <iostream>
std::cout << YourString << std::endl;
If you absolutely want to use printf, you can use the "c_str()" method that give a char* representation of your string.
printf("%s\n",YourString.c_str())
If you'd like to use printf(), you might want to also:
#include <stdio.h>
While using string, the best possible way to print your message is:
#include <iostream>
#include <string>
using namespace std;
int main(){
string newInput;
getline(cin, newInput);
cout<<newInput;
return 0;
}
this can simply do the work instead of doing the method you adopted.