read in floating point number char by char and reassemble as different type?

I have to write a program that I thought would be pretty easy to solve, but I am lost and dont know what to do. The problem is to read in floating point numbers character by character. The problem will separate the whole number into an integer, and the fractional part into an integer, and disregard any non numeral characters.

I though maybe i could store the value in a string and then try and type cast it to an int value, but cant type cast a string into an int value...

I have no clue on how to do this. I cant think of a way to reassemble the read in characters and still be a number value. Anyone point me in the right direction?

3A4.21P6

float = 34.216

int1 = 34, int2 = 216

Try using atoi() and see if you can get any further.

Also use isdigit(). Both in <cstdlib> i think.
Last edited on
thanks. i will look that up.
sorry i edited again mate ^^^
I dont know if im using atoi() incorrectly. I am getting an error on those lines while compiling.

"cannot convert std::string to const char for argument 1 to int atoi(const char)"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    
    string inputString, str1, str2;
    int dotPos, int1, int2;
    
    cin >> inputString;

    dotPos = inputString.find('.');

    str1 = inputString.substr(0,dotPos);

    str1 = inputString.substr(dotPos + 1, 255);

    int1 = atoi(str1);

    int2 = atoi(str2);

    cout << endl << int1 << endl << int2;
    
    system("pause");
    
    return 0;

}
    
Last edited on
atoi() takes a char* as an argument, not an std::string. Use .c_str() to get a char* from a string.
1
2
3
    str1 = inputString.substr(0,dotPos);

    str1 = inputString.substr(dotPos + 1, 255);


I don't understand why you are doing that.^
Also calling system("pause"); is evil, it might not work on all systems, and there are much better ways of pausing a program.
and out goes simple... i am super confused now. the only thing ive ever used .c_str() for is converting an input into a vaild format for a text file. i never read the details on it and that is way over my head right now. and i see char[] all over the place here but i dont understand that either. The part that frustrates me the most is that i dont think the text book that i am working out of gave enough resources so far in order to solve this problem.
for smilodon.

1
2
3
    str1 = inputString.substr(0,dotPos); // <--- supposed to store numbers up to the dot in floating point number

    str1 = inputString.substr(dotPos + 1, 255); // <-- supposed to store digits after dot in floating point number 


I just wanted to see if i could use atoi() and that seemed like the easiest way to give a test run on what my main program is supposed to do.

Last edited on
Umm...
atof()
std::stringstream

By the way, if the second parameter for std::string::substr() is omitted the argument defaults to the end of the string.
at vince, I understand what you think you are doing, not what you actually are doing.
Topic archived. No new replies allowed.