0

I am trying to convert an input character array, to an int array in c++. Inputs would be in a format like: 'M 911843 6', where the first value of the char array is a uppercase letter, which I convert to an ASCII value and -55.

Edit: I should also mention I just want to use the iostream library

The last value of the char array can be a letter or number also.

I want to retain the exact number value of any input in the char array, which is why I convert to an ASCII value and -48, which retains the same number, but stored as an int value:

I use the checkdigit() function to check if the char input is a number or not.

The difficulty I am facing is that the input will always have a blank space at i[1] and i[8] (if we count i[0] as the first value) - so I try to give them an int value of 0 (int of a " " is 0)

Upon several debugging attempts, I found that it is after the blank space is given a 0 value, the output in my for loop keeps outputting the wrong values, I suspect it has something to do with the isdigit() function in my for loop.

If the spaces from M 911843 6 were removed, the int output is usually fine, e.g. a char input of M9118436 will return an int array of [22][9][1][1][8][4][3][6].

The output with spaces: [22][0][-183][-120][37][-118][-59][72][0][-55]

Ideal output: [22][0][9][1][1][8][4][3][0][6]

The code is listed below, any help or advice would be greatly appreciated, thanks!!

#include <iostream>
using namespace std;
int main() {
    char a[10];
    int z[10];
    int i = 0;
    int r; //result of the isdigit check (0 or 1)

    cout << "in      ";
    cin >> a;


    for (int i = 0; i < 10; i++) {
        r = isdigit(a[i]);
        if (r == 0) {
            if (i==1 || i==8)
                z[i] = 0;
            else z[i] = int(a[i]) - 55;
        }
        else {
            z[i] = int(a[i]) - 48;
        }
    }

    cout << z[0] << "\n" << z[1] << "\n"<< z[2]<< "\n" << z[3] << "\n"<< z[4] << "\n"<< z[5] << "\n"<< z[6] << "\n"<< z[7]<< "\n" << z[8] << "\n"<< z[9];


    return 0;
}

1
  • Standard std::string for your strings, and std::transform to convert to a std::vector<int>? Can use e.g. std::copy_if to copy the string to itself to remove spaces. Commented Feb 18, 2022 at 10:10

2 Answers 2

0

The problem is that cin >> a; does not read sizeof(a) characters, but up to the first space character and will terminate that with a null.

That means that you array will containt 'M', '\0' and 8 uninitialized characters. You must read the characters one at a time with unformatted reads:

for (auto& c : a) {
    cin.get(c);
    if (!cin) {
        cerr << "Incorrect input\n";
        return EXIT_FAILURE;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just a follow on from Serge's answer which gave me a good understanding of how strings are read - I solved my problem using cin.getline() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.