This question already has answers here:
Mixing cout and wcout in same program
(6 answers)
c++ can't get "wcout" to print unicode, and leave "cout" working
(3 answers)
Closed 3 months ago.
After I'm using std::wcout, std::cout isn't printing
int main() {
std::wcout << "Hello " << std::endl;
std::cout << "World" << std::endl;
}
Output:
Hello
int main() {
std::cout << "Hello " << std::endl;
std::wcout << "World" << std::endl;
}
Output:
Hello
World
Related
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 12 months ago.
I'm trying to return a pointer, that points to an array of strings, from a function and then access elements in the array that the returned pointer points to. I seem to get a pointer back from the function, but when I try to output individual elements of the array to the computer screen, my program seems to just stop outputting and end the program.
Running the code below, the pointer to the array outputs as expected (see screen output screenshot). However, when I try to access the array's elements, none of the elements are output to the screen and the program just ends. Any ideas/thoughts would be greatly appreciated. FYI, I realize that I'm not using the data that's being passed to the function, but that's because I'm trying to figure out how to access the data that gets returned first.
roster.h
#ifndef ROSTER_H
#define ROSTER_H
#include <iostream>
#include <string>
class Roster {
public:
std::string* parseData(const std::string Data);
};
#endif
roster.cpp
#include <iostream>
#include <string>
#include "roster.h"
std::string* Roster::parseData(const std::string Data) {
std::string stringData[3] = { "abc", "def", "ghi" };
return stringData;
}
rostertest.cpp
#include <iostream>
#include <string>
#include "roster.h"
void rosterClassTest()
{
Roster roster;
std::string* returnData = roster.parseData("red,blue,black,gray");
std::cout << "pointer returned: "
<< returnData << std::endl << std::endl;
std::cout << "element 0 of array: "
<< returnData[0] << std::endl << std::endl;
std::cout << "element 1 of array: "
<< returnData[1] << std::endl << std::endl;
std::cout << "element 2 of array: "
<< returnData[2] << std::endl << std::endl;
}
Roster::parseData() is returning a pointer to a local array variable that goes out of scope upon exit, destroying the array, and thus the function is returning a dangling pointer to invalid memory.
You should return a std::vector<std::string> (or std::array<std::string, 3>) instead, eg:
std::vector<std::string> Roster::parseData(const std::string Data) {
std::vector<std::string> stringData{ "abc", "def", "ghi" };
return stringData;
}
std::vector<std::string> returnData = roster.parseData("red,blue,black,gray");
std::cout << "pointer returned: " << returnData.data() << std::endl << std::endl;
for(size_t i = 0; i < returnData.size(); ++i) {
std::cout << "element " << i << " of array: " << returnData[i] << std::endl << std::endl;
}
If that is not an option, then you will have to new[] the array, and then the caller will have to delete[] the array when finished using it, eg:
std::string* Roster::parseData(const std::string Data) {
std::string *stringData = new std::string[3];
stringData[0] = "abc";
stringData[1] = "def";
stringData[2] = "ghi";
return stringData;
}
std::string* returnData = roster.parseData("red,blue,black,gray");
std::cout << "pointer returned: " << returnData << std::endl << std::endl;
std::cout << "element 0 of array: " << returnData[0] << std::endl << std::endl;
std::cout << "element 1 of array: " << returnData[1] << std::endl << std::endl;
std::cout << "element 2 of array: " << returnData[2] << std::endl << std::endl;
delete[] returnData;
In which case, you should return a std::unique_ptr instead, eg:
std::unique_ptr<std::string[]> Roster::parseData(const std::string Data) {
//std::unique_ptr<std::string[]> stringData(new std::string[3]);
auto stringData = std::make_unique<std::string[]>(3);
stringData[0] = "abc";
stringData[1] = "def";
stringData[2] = "ghi";
return stringData;
}
auto returnData = roster.parseData("red,blue,black,gray");
std::cout << "pointer returned: " << returnData.get() << std::endl << std::endl;
std::cout << "element 0 of array: " << returnData[0] << std::endl << std::endl;
std::cout << "element 1 of array: " << returnData[1] << std::endl << std::endl;
std::cout << "element 2 of array: " << returnData[2] << std::endl << std::endl;
This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 1 year ago.
code :
#include <iostream>
using namespace std;
int main()
{
int i = 1;
cout << "hello " << i << " end" ;
return 0;
}
OUTPUT : hello 1 end
this works fine but
#include <iostream>
using namespace std;
int main()
{
int i = 1;
cout << "hello " , i , " end" ;
return 0;
}
OUTPUT: hello
why the above terminates with printing hello only?
std::cout << "hello " , i , " end" ;
Because , has a lower operator precedence than <<, this will be evaluate to:
(std::cout << "hello "), i , " end" ;
First, (std::cout << "hello ") will be evaluate, printing hello .
Second, i would get evaluate. The value i will be return for the whole expression (std::cout << "hello "), i.
Third, " end" would get evaluate. The value " end" will be return for the whole expression ((std::cout << "hello "), i ), " end" ;
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 2 years ago.
Currently, I'm trying to understand why I have to use:
std::cout << "&str1:" << &str1 << std::endl;
To get the memory address of:
const char * str1 = "Good Morning";
I expected that:
std::cout << "str1:" << str1 << std::endl;
would do so.
Here is an example:
void charPointerExamples() {
const int * myInt = new int(1);
const char * str1 = "Good Morning";
const char * str2 = str1;
std::cout << "myInt:" << myInt << std::endl;
std::cout << "&str1:" << &str1 << std::endl;
std::cout << "&str2:" << &str2 << std::endl;
std::cout << "str1:" << str1 << std::endl;
std::cout << "str2:" << str2 << std::endl;
std::cout << "*str1:" << *str1 << std::endl;
std::cout << "*str2:" << *str2 << std::endl;
delete myInt;
}
The output itself:
myInt:0081D450
&str1:003CFD20
&str2:003CFD14
str1:Good Morning
str2:Good Morning
*str1:G
*str2:G
This question already has answers here:
Correct use of std::cout.precision() - not printing trailing zeros
(3 answers)
Closed 6 years ago.
I am trying to print double and integers as double. For doing so, I wrote the following program:
int main()
{
string object="1";
std::stringstream objectString;
objectString << std::setprecision(8) << atof(object.c_str());
cout<<"ObjectString="<<objectString.str()<< " "<<std::setprecision(10) << double(atof(object.c_str())) <<"\n";
}
I expected the output to be:
ObjectString=1.0 1.0
However, I am getting the output as:
ObjectString=1 1
Can someone please suggest as to where am I going wrong?
To force trailing zeros, use std::fixed:
std::string object = "1";
std::stringstream objectString;
objectString << std::fixed << std::setprecision(8) << atof(object.c_str());
std::cout << "ObjectString=" << objectString.str() << " ";
std::cout << std::fixed << std::setprecision(10) << double(atof(object.c_str())) << "\n";
Output:
ObjectString=1.00000000 1.0000000000
The way to force the output to always show the decimal point is to use std::showpoint:
#include <iostream>
#include <iomanip>
int main() {
double d = 1.0;
std::cout << std::setprecision(1) << d << '\n';
std::cout << std::setprecision(1) << std::showpoint << d << '\n';
return 0;
}
[temp]$ ./a.out
1
1.
[temp]$
This question already has answers here:
Set back default floating point print precision in C++
(5 answers)
Closed 7 years ago.
I have search the web but couldn't find what I need.
Some people recommend using
streamsize ss = std::cout.precision();
but I couldn't get it to work.
How do I set a double value back to the original state after setprecision?
#include <iostream>
using namespace std;
int main()
{
double a;
cout << "enter a double value: ";
cin >> a;
cout << "your value in 3 decimals is " << setprecision(3) << fixed << a << endl;
cout << "your original value is " << a << endl;
return 0;
}
Obviously the code above will not return the original value of a.
My intended output is: if user enter 1.267432
your value in 3 decimals is 1.267
your original value is 1.267432
How do I set a double value back to the original state after
setprecision?
To do so, you have to get the precision before you use setprecision(). In your question you already mentioned it by the following line:
streamsize ss = std::cout.precision();
but I couldn't get it to work.
Here how you use it:
streamsize ss = std::cout.precision();
double a = 1.267432;
std::cout << "a = " << a << '\n';
std::cout.precision (3);
std::cout << "a becomes = " << a << '\n';
std::cout.precision (ss);
std::cout << "Original a= " << a << '\n';
And the output will be like:
a = 1.26743
a becomes = 1.27
Original a= 1.26743
Reference: setprecision.
Run live.
You can try like this:
#include <iomanip>
#include <iostream>
int main()
{
double a = 1.267432;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(3);
std::cout << a << endl;
return 0;
}