I have a string with the value 788597.31 and I am converting this value to double but when I print the variable only 788597 is displayed. I have used std::stod(string) and even stringstream but everytime I get the same previous value. Can anybody help me with this?
I want to store this string value in a double varaible.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s="788597.31";
double d=stod(s);
cout<<d<<" ";
stringstream g;
double a;
g<<s; g>>a;
cout<<a;
return 0;
}
The problem is in how you are printing your result, not in the string parsing. This program:
#include <iostream>
using namespace std;
int main() {
cout << 788597.31 << endl;
return 0;
}
also prints 788597.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setprecision(10) << 788597.31 << endl;
return 0;
}
prints 788597.31
If you want to see more than the default 6 significant digits your program needs to say so.
Related
I want to take in persons name using string object. But in my code if I put two part name separated with a space, only first part is displayed. My understanding is .c_str() returns a pointer to stored string with terminal null. Why is there a problem with space. I'm new to C++ and using Code::Blocks 13.12. This is a simplified version of the problem that I have in another program that I wrote.
Thanks in advance.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
string sCusName;
cout << "Please enter your name-> ";
cin >> sCusName;
int xsize = sCusName.length();
char *tempBuffer = new char[xsize+1];
strncpy(tempBuffer, sCusName.c_str(),xsize+1);
cout << tempBuffer << " is a beautiful name." << endl;
return 0;
}
When I enter single part name, program works fine. But if I put in two part name separated with space. Only first part is taken in.
it is not possible to read multi-word string using cin rather you should use getline() the getline() function takes two arguments cin and string variable forexample:
int main()
{
string sCusName;
cout << "Please enter your name-> ";
getline(cin,sCusName);
cout << sCusName << " is a beautiful name." << endl;
return 0;
}
For a vector of strings, return the sum of each string's size.
I tried to use accumulate, together with a lambda function (Is it the best way of calculating what I want in 1-line?)
Codes are written in wandbox (https://wandbox.org/permlink/YAqXGiwxuGVZkDPT)
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), [](string s){return s.size();});
cout << totalSize << endl;
return 0;
}
I expect to get a number (9), however, errors are returned:
/opt/wandbox/gcc-head/include/c++/10.0.0/bits/stl_numeric.h:135:39: note: 'std::__cxx11::basic_string' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
135 | __init = _GLIBCXX_MOVE_IF_20(__init) + *__first;
I want to know how to fix my codes? Thanks.
That's because you do not use std::accumulate properly. Namely, you 1) did not specify the initial value and 2) provided unary predicate instead of a binary. Please check the docs.
The proper way to write what you want would be:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), 0,
[](size_t sum, const std::string& str){ return sum + str.size(); });
cout << totalSize << endl;
return 0;
}
Both issues are fixed in this code:
0 is specified as initial value, because std::accumulate needs to know where to start, and
The lambda now accepts two parameters: accumulated value, and the next element.
Also note how std::string is passed by const ref into the lambda, while you passed it by value, which was leading to string copy on each invocation, which is not cool
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {5, 'c'};
cout << str; // "\005c"
}
Output: c
With gdb, it confirms that str contains "\005c" with
str[0] = '\005'
str[1] = 'c'
Why str[0] is not being printed in output console?
Used c++ version: c++11
ASCII 5 represents a signal intended to trigger a response at the receiving end. It is not visible on the console.
Reference: http://ascii.cl/
For example: try 65 instead of 5, you will see 'A'.
The ASCII number 5 is non printable. ASCII table
ASCII representation of 5 which is 53 is printable.
string str {53, 'c'};
cout << str; // 5c
Since ASCII value of 5 is 53.
So, you can try this way:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {53, 'c'};
cout << str; // "\005c"
}
I write a test of boost::function.
These codes are working.
#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>
//#include <boost/function.hpp>
#include <boost/ref.hpp>
using namespace std;
using namespace boost;
template<typename FUN,typename T>
T fun( FUN function, T lhs, T rhs ){
cout << typeid(function).name() << endl;
return function(lhs,rhs);
}
int add4(int a, int b, int c){
return a + b + c;
}
int main(){
cout << fun(bind(add4,2,_1,_2),1,4) << endl;
system("pause");
}
But when i add header file "boost/funcation"
VS2012 prompts me it.
error C2668: 'std::bind' : ambiguous call to overloaded function.
Don't import both std and boost namespaces into the global namespace, to avoid such an ambiguity.
Instead, either specify fully qualified names, like boost::function, boost::bind, or import particular symbols: using boost::function;.
I'm trying to combine strings to input text files. My code looks like this:
`#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int year;
string line;
string fileName;
for (int i=1880; i<2012; i++){
stringstream ss;
ss << year;
fileName = string("yob") + string(year) + string(".txt");
ifstream ifile(fileName.c_str());
getline(ifile,line);
cout << line << endl;
ifile.close();
}
}`
The text files look like "yob1880.txt" <-- that's the first text file and it goes all the way to "yob2011.txt". I want to input the text files one by one, but combining these three string types doesn't work it gives me an error saying invalid conversion from int to const char*.
Any thoughts on the problem? Thanks!
You should get it from the stringstream. You're almost there but this is what you should do instead:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int year;
string line;
string fileName;
for (int i=1880; i<2012; i++){
year = i; //I'm assuming this is what you meant to use "year" for
stringstream ss;
ss << year; //add int to stringstream
string yearString = ss.str(); //get string from stringstream
fileName = string("yob") + yearString + string(".txt");
ifstream ifile(fileName.c_str());
getline(ifile,line);
cout << line << endl;
ifile.close();
}
}