Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have information stored in an array of char and I would like to pass them to a single double value.
As an example, I want to pass it from,
data[0]=3;
data[1]=.;
data[2]=1;
data[3]=4;
data[4]=1;
data[5]=5;
data[6]=1;
to
double data = 3.14151;
How can I do it?
Thanks!
You can use functions std::stringstream from sstream or strtod from cstdlib or stod (If you are using C++11) as per your need.
Quoting example from cplusplus.com
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
return 0;
}
I am assuming that your array actually contains characters and you just forgot the apostrophs. I used an easier, less error-prone way to initialize data, with the added benefit that my version actually does a zero-termination, which yours didn't (as Axel correctly pointed out).
char const * data = "3.1415";
The solution would be:
#include <cstdlib>
// ...
double number = std::strtod( data, NULL );
// ...
Check the strtod() documentation for behaviour in case of error, and how to use the second parameter to check if your conversion actually went as far as you expected.
Related
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 2 years ago.
Improve this question
Is there a better way to convert a String to a Float?
I'm using this method here:
std::string test = "4.9102919";
float test2 = std::atoi(test.c_str());
std::cout << test2;
It prints out: 4 instead of 4.9102919.
You are just using the wrong function.
The behavior of atoi is described by its name itself: ascii to integer. That's why your input is truncated to its integer part, before being assigned to the float variable.
What you need is std::atof, instead: ascii to float.
"atoi()" is for integers
You're looking for "atof()" which is for floating point
This:
float test2 = std::atoi(test.c_str());
First converts const char* to int, and then implicitly cast int to float.
use std::atof:
float test2 = std::atof(test.c_str());
If you want to use C++ strings then you want the std::stod function. atof uses C strings (char arrays) and I'm sure somewhere I was taught to avoid them in C++ when posible.
std::string test = "4.9102919";
double test2 = std::stod(test);
std::cout << std::fixed << std::setprecision(7) << test2 << std::endl;
PS. The precision of the number you're trying to convert is too large for a float, you need to use a double or all results from either stof or atof will be wrong.
You just have to use <std::atof> instead of <std::atoi> in float test2 = std::atoi(test.c_str());
// stof example
#include <iostream> // std::cout
#include <string> // std::string, std::stof
int main ()
{
std::string orbits ("686.97 365.24");
std::string::size_type sz; // alias of size_t
float mars = std::stof (orbits,&sz);
float earth = std::stof (orbits.substr(sz));
std::cout << "One martian year takes " << (mars/earth) << " Earth years.\n";
return 0;
}
Possible output:
One martian year takes 1.88087 Earth years.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I parse a char array such as:
"2019-11-01T17:00:10.000Z"
I want 17:00:10.000 and nothing else. How do I parse with respect to the char array indices?
The simplest and safest way to deal with strings is to use the standard std::string. The std::string has many class member functions that can help with this. One of them is substr that can be used to create a new std::string from a part of a string.
Example:
#include <iostream>
#include <stdexcept>
#include <string>
std::string get_time_str(const std::string& timestamp) {
if(timestamp.size() != 24)
throw std::runtime_error("bad timestamp: "+ timestamp);
return timestamp.substr(11, 12); // start at pos 11 and 12 chars forward
}
int main() {
const char* ts = "2019-11-01T17:00:10.000Z";
std::cout << get_time_str(ts) << '\n';
}
Output:
17:00:10.000
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to ask,
how does string::string operator function, I know that it is a standard constructor, for using strings, yet what does operator do? Does it allow me to use the multiplier operator at the end? Size_t represents the size of an object and string& is a pass by reference. How are these concepts making sense?
#include <iostream>
#include <string>
using namespace std::literals::string_literals;
std::string operator*(std::size_t n, const std::string& s)
{
std::string ret;
while (n--)
ret += s;
return ret;
}
int main()
{
std::cout << 5 * std::string("Hallo") << std::endl;
std::cout << 5 * "Test"s << std::endl;
}
What does std::string ret mean, can I use it because of std::string? Because std::string has been defined at the beginning ?
By implementing operator*, you allow type size_t to be "multiplied" by type string. The reason multiplied is in quotes is because you implement yourself what "multiply" means. In this particular implementation, the string is just appended to itself n times.
So 5 * std::string("Hallo") will result in HalloHalloHalloHalloHallo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This may be a trivial question for most but I am new to c++. My question is, how would I pass a pointer which is deference to a function to operate on the pointed value?
char first_name[] = "hello";
int myFunc(const char *source){
innerFunc(char *source){/*append world*/}
}
This doesnt seem to work.
One example:
char first_name[] = "hello";
int inner_func(const char* source) { /* do something, read-only */ }
int my_func(const char* source) {
inner_func(source);
}
So, you merely need to pass the name, that's all.
However, note that you have passed the pointer as const, which means that you cannot change it. Appending world does not work in that instance. In fact, if you would like to operate on your char string in a changing manner, you would need to create a second char* dynamically with the extended size. You cannot change source.
Also, inner functions like that cannot be defined in C++. Just define it outside of myFunc. You can create inner functions with lambdas, but this would be another answer.
Luckily, in C++, manipulating strings is far easier and deeply to recommend:
#include <string>
std::string first_name = "hello";
int inner_func(std::string& source) {
source += " world";
}
int my_func(std::string& source) {
inner_func(source);
}
Now, when you pass a string like first_name to my_func, it will be passed to inner_func where some string is appended.
Note, though, that hello world is quite a strange name, especially as a first name. It might not be what you want.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Okay So i have this function
long valid(const char* str)
{
int temp = atoi(str)
return long;
}
`str = 9789070002046` //13 digit long
the function returns some random 10 digit number.I have also tried STOI stringstream
#shivam-gupta hi buddy,
You maybe have been quite new to C/C++ programming is that correct? Some people at the forum require you to make explain what your problem is in detail also with the right C++ syntax.
I know what you mean, and it had been a main problem with me in the beginning when I tried to learn C/C++. The function that you might be looking at is atol() not atoi(). From my point of view it is better to convert the string to double, then cast it to a long/integer variable.
The code I used to complete your snippet is this
#include <iostream>
long valid(const char* str);
int main()
{
long test;
const char *str = "9789070002046"; //13 digit long
test = valid(str);
std::cout << test << " sizeof test is " << sizeof(test) << std::endl;
return 0;
}
long valid(const char* str)
{
long temp = (long)atol(str);
return temp;
}
Enjoy coding C++! And remember, life is not too short to learn C++!