GaBoKaS GaBoKaS - 2 months ago 12
C++ Question

How to count symbols before '.' symbol and split entire char[] into 2 arrays in c++?

I want my function to read one char[] and split it into 2 arrays. Both arrays would be one big number, first array is for that number int value and second one is for double values. I must use 2 arrays because this number can be binary, decimal, octal or hex.

void Read(int place[], int &size, int &type, int placedot[], int &sizedot, int &typedot)
{
char reader[limit];

cin >> reader;

size = 1;

while (reader[size] == '.' || size != strlen(reader))
{
size++;
cout << "LOL";
}

cin >> type;
cout << size;

typedot = type;

for (int i = 0; i<size; i++)
{
switch (reader[i])
{
case '0': place[i] = 0; break;
case '1': place[i] = 1; break;
case '2': place[i] = 2; break;
case '3': place[i] = 3; break;
case '4': place[i] = 4; break;
case '5': place[i] = 5; break;
case '6': place[i] = 6; break;
case '7': place[i] = 7; break;
case '8': place[i] = 8; break;
case '9': place[i] = 9; break;
case 'A': place[i] = 10; break;
case 'B': place[i] = 11; break;
case 'C': place[i] = 12; break;
case 'D': place[i] = 13; break;
case 'E': place[i] = 14; break;
case 'F': place[i] = 15; break;
}
}
}

Answer
#include <iostream>
#include <string>

int main()
{
    std::string str = "3423432432.32445654576654578978905";
    std::string strInt;
    std::string strDec;

    int i = 0;

    while('.' != str[i])
        strInt += str[i++];

    for(++i; i < str.length(); i++)
        strDec += str[i];


    std::cout << strInt << std::endl << strDec << std::endl;

    // now you have the integer part and the decimal part as strings so convert each of them to int

    std::cout << std::endl;
    return 0;
}