Splitting strings and atof to handle fractions.

I'm writing a program to convert from Decimal to Octal, vice versa, Hex to Decimal, vice versa, etc. Anyway so I was happily coding along when I realised that if a user entered a fraction; I would be doomed! :O

So I googled how to split a string; so that I could then divide the first number by the second.

I found a function; and I rewrote it for my own purposes. I tried it out; and the program told me the answer was infinity. I am fairly sure 25/5 != infinity:
http://i31.tinypic.com/vzess5.png

Could you tell me what I have done wrong here? I'm not sure what; perhaps I could add cout everytime I do a step in the function... I'll do that and post the results.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//            The string to convert
double cvtFracToNum(string num) {
    string sub1, sub2; // The substrings, 1 will be divided by two to convert into a decimal.
    double val = 0; // The value to return; this will be equal to sub1 / sub2.

    size_t pos = num.find("/"); // Split the string around the division symbol.

    if (pos != string::npos) {

        size_t end = 0;

        if (!(num.empty())) {

            while (end != pos) {
                sub1 += num[end++]; // The first substring = the first number
            }

            while (end != num.length()) {
                sub2 += num[end++]; // The second substring = the second number.
            }

        } else {

            sub1 = num;
            sub2 = "";

        }
    }

    double Dsub1 = atof(sub1.c_str());
    double Dsub2 = atof(sub2.c_str());

    val = (Dsub1 / Dsub2);

    return val;
}

void DecToOct() {
    string  dec;
    double  decNum = 0;//, octNum;
    cout << "What is your decimal number: ";
    cin  >> dec;
    cin.ignore();
    if (dec.find("/") != string::npos) {
        string num = dec;
        cout << dec << endl;
        decNum = (cvtFracToNum(num)); // Handle fractions
    }

    cout << decNum << endl;
}


At this point it just tells me that Dsub1 / Dsub2 is infinity... I haven't even gotten to the point of converting infinity to octal yet :l

Thanks.
Last edited on
After calling std::string num.find(), use the value, the position of the '/' to to use in the std::string num.substr(start, length) function. I think what is happening is something is going wrong when you try and parse the string, and your are not passing a valid value to atof(). If you pass chars other than '0'-'9' and/or '.' the function just returns '0' hence 25/0=inf.
I'll try, thanks.


Edit: In that case, could I not just remove the '/' from the string?

Edit: I tried using the erase function; it didn't work. I'll try your way :)

Edit:
1
2
            sub1 = num.substr(pos, pos-5);
            sub2 = num.substr(pos, pos+5);
Didn't work. I'm probably using this wrong :(

This is the function as I'm using it now:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//            The string to convert
double cvtFracToNum(string num) {
    string sub1, sub2; // The substrings, 1 will be divided by 2 to convert into a decimal.
    double val = 0; // The value to return; this will be equal to sub1 / sub2.

    size_t pos = num.find("/"); // Split the string around the division symbol.

    if (pos != string::npos) {

        size_t begin = 0;

        if (!(num.empty())) {




            while (begin != pos) {
                sub1 = num.substr(0, pos);
                begin++;
            }

            while (begin != num.length()) {
                sub2 = num.substr(pos+1, string::npos);
                begin++;
            }

            cout << endl << sub1 << endl << "______" << endl << sub2 << endl << "Equals: ";

            double Dsub1 = atof(sub1.c_str());
            double Dsub2 = atof(sub2.c_str());

            val = (Dsub1 / Dsub2);

            return val;
        } else {
            cout << "Failed to parse string";
            return atof(num.c_str());
        }
    }

return 0;
}

void DecToOct() {
    string  dec;
    double  decNum = 0;//, octNum;
    cout << "What is your decimal number: ";
    cin  >> dec;
    cin.ignore();
    if (dec.find("/") != string::npos) {
        string num = dec;
        decNum = (cvtFracToNum(num)); // Handle fractions
    }

    cout << decNum << endl;
}


It works, no matter the length of the string. I tried 255/5 = 51.

Thanks :)
Last edited on
Your function would not work for very large fractions such as 1000000/1000000;

Instead you should do this substr(0,pos) and then substr(pos+1, endOfString).
double post
Last edited on
Double post :P

I noticed that; I was reading the documentation on this website. I don't use strings much for anything other than normal input/output.

According to the documentation, string::npos is the "maxmimum value of size_t" and so it works for that.
Last edited on
Ah, I was reading the part where you said that you think you were doing it wrong. Sorry.
You may have got there before me, now that I think on it it was only after I refreshed the page after updating my post, that I noticed your reply.

Strange sense of de ja vu with that aswell :l
Topic archived. No new replies allowed.