If input is NULL

Im sorry this site doesnt allow me to format the code.

Code works fine except I need to add this constraint.
"If input is NULL, return -1".

Im just wondering how I can do this. Everytime I put NULL in for s, it crashes.

Side Note: If you need to know, this converts the excel titles to numbers like A = 1, Z = 26, AA = 27, AB = 28, etc.

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
  #include <iostream>

using namespace std;

class CIS14
{
public:
    int convertExcelTitleToNumber(string* s)
    {

        string str = *s;

        int num = 0;
        for (unsigned int i = 0; i < str.length(); i++)
        {
            num = num * 26 + str[i] - 64;
        }
        return num;
    }
};
int main()
{
    CIS14 cis14;
    string s = "AA";
    cout << cis14.convertExcelTitleToNumber(&s) << endl;

    return 0;
}
Hello donda97,

Try adding this if (str.length() == 0) return -1; at line 18.

It would also help if you added the header file "<string>".

Hope that helps,

Andy
Topic archived. No new replies allowed.