String to Float with Precision in C++? [closed] - c++

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.

Related

C++ Int array type in function call [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 2 years ago.
Improve this question
I have an array initialised like the following:
int example[5][5];
example[5][5] = 55;
And a function:
void example_function(auto inArray, int z){
cout << "example text = " << inArray[z][z] << endl;
}
And I am calling it into the function like this:
example_function(example, 5);
As you can see, I have the parameter for the function as auto when it is really using an integer array.
When I use typeid(table).name() to get the type of the array example, it outputs the type as A5_A5_i where the fives are from the initialisation (e.g. int example[3][4][5] would output A3_A4_A5_i)
When using typeid(table).name() on inArray after changing the type of the parameter from int to auto, I get the type name as PA5_i which is different to the one mentioned above.
How can I get a suitable type for a parameter in my function, and is there a better way to do this
If the array passed to the function is known beforehand, in this case as int example[5][5];, you can use the following,
void example_function(int (&inArray)[5][5], int z){
cout << "example text = " << inArray[z][z] << endl;
}
Here we take the array by reference to avoid array decay.
If the size might vary at runtime, use std::vector.
std::vector<std::vector<int>> example;
void example_function(std::vector<std::vector<int>> inArray, int z){
cout << "example text = " << inArray[z][z] << endl;
}

mt19937 random number range - weird output 00A8106E [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 6 years ago.
Improve this question
I want to create a little game in c++, and therefore I need a function to return random numbers in a specific range.
Most of the answers I found were similar to this one https://stackoverflow.com/a/19728404/5780938, and I think this is the solution I'm looking for.
To test if the function does, what I want it to, I tried outputting the results in several different ways.
At the moment my code looks like this:
#include "stdafx.h"
#include <iostream>
#include <random>
int zufälligeZahl();
int main()
{
using std::cin;
using std::cout;
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
return 0;
}
int zufälligeZahl()
{
std::random_device rd;
std::mt19937 zGenerator(rd());
std::uniform_int_distribution<int> uni(1, 13);
int random_integer = uni(zGenerator);
return random_integer;
}
I've tried this in many different ways, but no matter what I do, it doesn't work. Either the output is something like 00A8106E, or I don't get any output at all.
I'm using Visual Studio Community 2015.
You are not calling the function zufälligeZahl, you are printing out the address of the function.
Fix your code by actually calling the function:
cout << zufälligeZahl() << "\n";
You forgot the parentheses.

C++ convert *string to int [closed]

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++!

how can i pass from char[i] to a double? [closed]

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.

How to compare a string with a string in pointer in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am implementing linked list in c++. In that am trying to compare a data stored in the node with a string. Here is my code:
String f;
cin>>f;
if(strcmp(temp->data,f)==0)
{ cout<<"same"; }
else
{ cout<<"not same"; }
Here is my error:
"assignment1.cc", line 160: Error: Cannot cast from std::string to const char*.
"assignment1.cc", line 160: Error: Cannot cast from std::string to const char*.
How to compare those two strings?
If you only need to check for equality, you can simply use operator== to compare two strings. In your case, that seems to be:
if (data->temp == f)
However, if you want the functionality provided by strcmp (that is, if you need to know which string is larger lexicographically in case they are not equal), you can use string::compare:
if ( s1.compare(s2) < 0 )
You can use the std::string::c_str method:
std::string f;
cin>>f;
if(strcmp(temp->data,f.c_str())==0)
cout<<"same";
else
cout<<"not same";
You can either use f's "compare()" operator (http://en.cppreference.com/w/cpp/string/basic_string/compare) or you can use operator ==
#include <iostream>
#include <string>
int main() {
std::string f("hello world");
const char* p = "hello world";
if (f == p)
std::cout << "f == p" << std::endl;
}
See http://ideone.com/TTXRZv