How to print a string in C++ [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 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.

Related

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 does stringstream work with <<? [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 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;
}

Segmentation Fault C++ pointers [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 8 years ago.
Improve this question
I am new to C++ programming but I know that pointers cause segmentation error. The problem is in the Readline() method when I am trying to read a sudoku but I cannot fix it. What am I missing?
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include "Sudoku.h"
using namespace std;
// Constructor
Sudoku::Sudoku(){
root=cells;
row=0;
row_ptr=&row;
}
void Sudoku::Readline(string s,int i) {
int lead;
for(int k=0;k<9;k++){
lead=(9*i)+k;
if (s[k]!=',') {
*(root+lead)=s[k];
} else {
*(root+lead)=0;
}
}
}
void Sudoku::MakeSudoku(string s){
//cout<<(*row_ptr)++<<' '<<s<<"\n";
Readline(s,(*row_ptr)++);
}
The class definition is
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Sudoku{
public:
int cells[81];
int row;
int *root;
int *row_ptr;
public:
Sudoku();
void MakeSudoku(string s);
void Readline(string s,int i);
void PrintSudoku();
};
The main file is
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Sudoku.cpp"
using namespace std;
int main()
{
Sudoku sd;
// Input csv file containing sudoku
ifstream filen("sudoku.csv");
string s;
if(!filen.is_open()){
cout << "Error opening file";
} else{
while(!filen.eof()){
getline(filen,s);
sd.MakeSudoku(s);
}
}
filen.close();
//sd.PrintSudoku();
return 0;
}
Your code is no C++ code. Except file operations it is (bad styled) C code. You are using a plain array (cells), you even do an unnecessary copies of the array (root) and that pointer arithemtic is dangerous (as you are currently experiencing).
I think you should rewrite your code a bit which will solve your problem:
You should use descriptive variable names... k,s,i,etc. are hard to read
Use a two-dimensional array for 'cells'. Or even better a C++ container like a vector of vectors. The latter would check boundaries and you could get rid of your pointer arithmetics (which causes such faults when done the wrong way) and you could use plain indexes.
Use proper indentions and empty lines for block separation
Don't use magic numbers like "81" and "9". Create constants. Give them names! make them dependent from each other if they are dependent.

cout print string variable cannot pass the build

Could any one help me to figure out which the following code cannot build successfully:
#include <iostream>
int main(void){
std::string str1("sfsfasfdsdf");
std::cout << str1 << std::endl;
return 1;
}
Thanks.
You have to include std::string header:
#include <string>
EDIT: According to #ShafikYaghmour's comments, include iostream sometimes brings in string, but it may not be the case for you if you only have the posted code.

Formatting the string YYYYMMDD as YYYY.MM.DD using Boost

I have a std::string such as 20040531, I want to format this as 2004.05.31.
Apart from the straight forward way of doing an std::insert at respective locations, is there a better way to do this using Boost?
PS. I cannot use other Boost calls to get date/time as this string is returned via a custom API. So this question is reduced to basic string formatting which may not sound exciting, but I am trying to learn Boost.
You could use boost::format...
#include <string>
#include "boost/format.hpp"
#include <iostream>
int main()
{
std::string a("20040531");
std::cout << boost::format("%1%.%2%.%3%")
% a.substr(0,4) % a.substr(4,2) % a.substr(6,2);
}
You specifically asked about doing this using Boost, but if you wanted to do this in C++ without introducing a dependency on Boost then you could just use a stream to achieve the same thing:
#include <sstream>
#include <string>
#include <iostream>
int main()
{
std::stringstream s;
std::string a("20040531");
s << a.substr(0,4) << '.' << a.substr(4,2) << '.' << a.substr(6,2);
std::cout << s.str();
}