How can I parse double from a string? - c++

I have the following code but it only converts strings to double. if the string contains a letter, it causes an error
while (cin >> s){
const char *c_ptr=s.c_str();
d = atof(c_ptr);
v.push_back(d);
}
I want to input a string like "a1.2 3 4b" and have the vector populated with "1.2 3 4"

What about this:
1) replace all chars in the string that aren't digits or decimal points by spaces
2) read doubles in a loop
since you got rid of the letters before parsing the doubles, they should no longer cause issues.

Related

I am having problem with this question my output is not correct

Write a program to display the sizes of basic four datatypes i.e integer, double, float, character.
Input:
The first line of input contains integer T denoting the number of test cases. For each test case, the user can input any of the above data types.
Output:
For each test case, there is a single line output displaying the size of that data type.
Constraints:
1<=T<=100
Example:
Input:
4
1
#
7.98
9.985647851
Output:
4
1
4
8
I tried this
int main() {
//code
int x;
cin>>x;
std::string s;
for(int i = 0;i<x;i++){
cin>>s;
cout << sizeof(s) << endl;
}
return 0;
}
Output was
32
32
Wrong Answer
I started to work on this problem to provide the OP with some insight however I have come to conclusion that many others have within the comment section of the OPs original question: and that is that this question or problem has an indeterminate solution!
Reasoning:
If the user enters any of the following digits as a single entity for input { 0, 1, ... 9 } into the console this can be at the least interpreted as an int or a char type and at worst even a possible double, but without the '.' character being present we could eliminate that as a candidate. This problem definitely has ambiguity to it. Checking to see if it is a float or a double is easy; all one has to do is check the string to see if there is at least a '.' then it's either a double or a float then check the last character of the string to see if it is a f and if so then it is a float. Checking to see if it is a character that is a non digit is easy, however distinguishing a single character digit between a char and an int is the troublesome part!
Work around:
You could conclude that if the input string is a single character and is a non digit then it is definitely a char type.
You could conclude that if the input string is a single character and is a digit ASCII[48 - 57] then you could conclude that it is an int type. This would be considered a restraint.
You could conclude that if it isn't the above two it is at least a float or a double and it is a float if and only if the last character of the string is a f, but a . must be present for it to be either of the two. Again these would be restraints that you would put on the accepted data input.

c++ How to extract numbers from a string that has backslash?

I am trying to extract numbers from a string.
I would like to save each number to a separate double variable.
I currently tried using a simple stringstream like this.
std::string line = "100.2456426246\200.2624362436\300.136213477347";
std::stringstream stream(line);
stream.precision(20);
double a,b,c;
stream >> a >> b >> c;
Not only is the precision wrong(only prints out 6 digits), it only extracts the first number a(100.245), and b and c is null. I suspect it is due to backslash, but I'm not exactly sure.
What is the best way to read the string which contains backslash between each number and store the whole number with correct precision?
You could use getline with a delimiter to split a string into a vector of elements and atof to extract floats along these lines
vector<string> elems;
stringstream stream(line);
string item;
while (getline(stream, item, "\\")) {
elems.push_back(item);
}
vector<float> val(elems.size());
...
val[i]=atof(elems[i].c_str())
Precision here is more of a matter of presentation, which you can customize with the help of setprecision. For example,
#include <iomanip>
...
cerr << setprecision(6) << val[i] << endl;
will output the truncation to six significant digits.
Another matter is making sure that all digits up to the last one are stored. To make sure that all digits are stored, simply the size of your defined float must be sufficiently large to contain your data, and atof may need to be replaced by an appropriate parser if float should be replaced by double or something else.

How to convert an array of ASCII codes to int C++

First of all, i would like to read from plain text, i read hundreds of webpages about it and i just can't make it. I want to read every byte of the file and every two byte is a number what i want to store.
I want to read: 10 20.
I get: ASCII code of 1, ASCII code of 0, ASCII code of space etc. etc.
I tried several things, like stream.get, or stream.read, tried to convert with atoi but then i can't concatenate the two digits, i tried sprintf but all of them failed.
Array of ASCII codes:
char ASCII[] = "10 20";
Convert to integer variables:
std::istringstream iss(ASCII);
int x,y;
iss >> x >> y;
Done.
Here's the working sample: http://ideone.com/y8ZRGs
If you want to do this with your own code, there are only two things you need to be able to do.
First, you need to convert from the ASCII code of a digit to the number it represents. This is as simple as subtracting '0'.
Second, you need to convert from the numerical value of each digit of a two digit number to the number that represents. This is simple -- if T is the tens place and U is the units, it's 10T + U.
So, for example:
int twoDigitNumber (char tens, char units)
{
return 10 * (tens - '0') + (units - '0');
}

How to change a string type?

I want to enter a string.
string value;
cin >> value;
And i want to take the string and change parts of a string in to integers.
eg. 00: Input 22
I want to change "00"(without ":") in to an integer. Keep "Input" a a string and make the last two character to integers.
Now i want to take the parts that i have changed and display the in a array. One col for each part of the converted string.
1 - To return a substring
2 - To Convert a String to int

input containing leading zeroes

In c++ will their be any error if we input an integer containing leading zereos.
for eg:
int a;
cin>>a;
we give an input 00 or 01.
or inputing with the help of string for this is a better idea.
Integers (or floats for that matter) do not have leading zeroes. If you want to keep the leading zeroes then you have to read the input as a string instead, and convert it to number when needed. Or you can use formatting to add leading zeroes when printing results.
In c++ will their be any error if we input an integer containing leading zereos.
You might not get what you expect, depending on the settings of the input stream's format flags. The default is to expect user input to always be in decimal. Leading zeros have no effect. What if we turn that off by calling std::cin.unsetf()?
int main () {
int i;
std::cin.unsetf (std::ios::dec);
while (std::cin >> ii) {
std::cout << i << "\n";
}
}
The output will be 25 if you enter 25, but if you enter 025 the output is 21. That's because C++ now interprets a leading zero on input to mean the number that follows is in octal (or in hexadecimal in the case of a leading 0x or leading 0X).
Leading Zeros will be trimmed off. It wont be stored in the memory.