Char arrays, validating for no spaces

I'm trying to make a validation to not accept any spaces in the char array, my other validation works, but I'm not supposed to calculate the math or statistics on the array until the whole input doesn't include any spaces. Right now if I input "1 2 3" it still does the math on 1, and it just needs to revert back to invalid input.

My only thought right now is to create another little function to search for any space character and then if it's true display an error message, else display the math.
The purpose of the program is to code with char arrays, so no using string objects or strings.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
int main()
{
    const int LENGTH = 40;
    char numberString[LENGTH];
    int numArray[LENGTH];
    int spaceCounter = 0;

    int sum = 0;
    int count1 = 0;
    int highest = 0;
    int lowest = 0;
    bool valid = true;


    do
    {

    cout << "Enter a series of digits with no spaces between them.\n";
    cin >> numberString;

    while(numberString[count1] != '\0' && valid)
    {
        if(isspace(numberString[count1]))
        {
            cout << "Incorrect input....? \n";
            spaceCounter++;
            valid = false;
        }

        numArray[count1] = charToInt(numberString[count1]);

        count1++;
    }

    if(numberString[count1] == '\0')
    {
        sum = calcSum(numArray, count1);
        highest = charToHighest(numArray, count1);
        lowest = charToLowest(numArray, count1);
        cout << "The sum of those digits is " << sum << "\n";
        cout << "The highest digit is " << highest << "\n";
        cout << "The lowest digit is " << lowest;
    }

    }while(!valid);

    return 0;
}

int charToInt(char numberString)
{
    int num = 0;
    if(isdigit(numberString))
    {
        // Char math to convert char to int
        num = numberString - '0';
    }
    else
    {
        cout << "Incorrect input....?";
        cout << endl;
        cout << "Enter a series of digits with no spaces between them.";
        cin >> numberString;
        return 0;
    }

    return num;
}

int calcSum(int numArray[], int count1)
{
    int sum = 0;
    for(int count2 = 0; count2 < count1; count2++)
    {
        cout << numArray[count2] << "\n";
        sum += numArray[count2];
    }
    return sum;
}

int charToHighest(int numArray[], int count1)
{
    int highest = numArray[0];
    for(int highCount = 0; highCount < count1; highCount++)
    {
        if(numArray[highCount] > highest)
        {
            highest = numArray[highCount];
        }
    }
    return highest;
}

int charToLowest(int numArray[], int count1)
{
    int lowest = numArray[0];
    for(int lowCount = 0; lowCount < count1; lowCount++)
    {
        if(numArray[lowCount] < lowest)
        {
            lowest = numArray[lowCount];
        }
    }
    return lowest;
}


my output should look like:
Enter a series of digits with no spaces between them.
srjc 1234
Incorrect input....?

Enter a series of digits with no spaces between them.
987654321
The sum of those digits is 45
The highest digit is 9
The lowest digit is 1
Last edited on
Perhaps:

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

int main() {
    constexpr size_t LENGTH {40};
    char numberString[LENGTH] {}, highest {}, lowest {};
    const char* str {};
    unsigned sum {};

    do {
        std::cout << "Enter a series of digits with no spaces between them.\n";
        std::cin.getline(numberString, LENGTH);

        for (sum = 0, str = numberString, highest = '0', lowest = '9'; *str; ++str)
            if (std::isdigit(static_cast<unsigned char>(*str))) {
                sum += *str - '0';

                if (*str > highest)
                    highest = *str;

                if (*str < lowest)
                    lowest = *str;

            } else {
                std::cout << "Not a valid input\n";
                break;
            }
    } while (*str);

    std::cout << "The sum of those digits is " << sum << '\n';
    std::cout << "The highest digit is " << highest << '\n';
    std::cout << "The lowest digit is " << lowest << '\n';
}

I forgot to mention even though I set LENGTH to 40, I need to assume I don't know the length of the array. So it needs to be some kind of loop until it hits '\0' I think.
You are are working with essentially a C string array, a null ('\0') as the terminator, use strlen() for the retrieved number of characters. One function, no loop, #include <cstring>.

http://www.cplusplus.com/reference/cstring/strlen/

With an C string array sized to 40 you can only retrieve up to 39 characters and still have a valid C string.
The C++ <string> library has a similar function for getting the length of a C string, std::char_traits::length. #include <string>.

http://www.cplusplus.com/reference/string/char_traits/length/
correct, you can loop until you hit the terminal character without knowing its size.
you can even do it via a throw away pointer...
char cstringz[1000];
... ... get something in there...
char * cp = cstringz;
for(; *cp; cp++)
{
if(isspace(*cp))
blah;
}
@jonnin
I'm not sure still, I'm getting the same issue.
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
int main()
{
    const int LENGTH = 40;
    char numberString[LENGTH];
    int numArray[LENGTH];
    int spaceCounter = 0;

    int sum = 0;
    int count1 = 0;
    int highest = 0;
    int lowest = 0;
    bool valid = true;

    do
    {

    cout << "Enter a series of digits with no spaces between them.\n";
    cin >> numberString;


    while(numberString[count1] != '\0' && valid)
    {
        if(numberString[count1] < '0' || numberString[count1] > '9')
        {
            cout << "Incorrect input....? \n";
            spaceCounter++;
            valid = false;
        }

        numArray[count1] = charToInt(numberString[count1]);
        count1++;
    }
    char *newLength = nullptr;
    newLength = numberString;

    for(;*newLength; newLength++)
    {
        if(isspace(*newLength))
        {
            cout << "Incorrect input....? \n";
            cin >> numberString;
        }
    }


    }while(!valid);
                sum = calcSum(numArray, count1);
            highest = charToHighest(numArray, count1);
            lowest = charToLowest(numArray, count1);
            cout << "The sum of those digits is " << sum << "\n";
            cout << "The highest digit is " << highest << "\n";
            cout << "The lowest digit is " << lowest;

    return 0;
}
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
#include <iostream>

// valid if string has all decimal digits, and at least one digit is present
// c-style string for variety
bool valid_number_string( const char* cstr )
{
    if( cstr == nullptr || cstr[0] == 0 ) return false ; // empty string; invalid
    // loop till a null char is encountered
    for( ; *cstr != 0 ; ++cstr ) if( *cstr < '0' || *cstr > '9' ) return false ; // not a decimal-digit; invalid
    return true ;
}

template < std::size_t N > using char_array_t = char[N] ; // type alias for array of N char

template < std::size_t N > requires ( (N>1) == true ) // must have space for at least one digit
char_array_t<N>& get_number_string( char_array_t<N>& cstr ) // array is passed by reference
{
    std::cout << "Enter a series of digits [1," << N-1 << "] with no spaces between them: " ;
    std::cin.getline( cstr, N );

    if( valid_number_string(cstr) ) return cstr ; // valid input

    std::cout << "invalid input. try again\n" ;
    return get_number_string(cstr) ; // try again
}

template < std::size_t N >
long long calc_sum( const char_array_t<N>& cstr )
{
    long long sum = 0 ;

    for( char c : cstr )
    {
        if( c == 0 ) break ; // loop till a null char is encountered
        sum += c - '0' ; // '7' - '0' == 7 etc.
    }

    return sum ;
}

int main()
{
    const std::size_t SZ = 40 ;
    char number_string[SZ] {} ;

    get_number_string(number_string) ;
    std::cout << "you entered '" << number_string
              << "'\nsum of digits == " << calc_sum(number_string) << '\n' ;
    // TO DO: largest digit, smallest digit
}
Last edited on
Alright so I almost have it fixed, I guess I was using getline wrong at first with getline(numberString[]) which is the wrong use of it. Now I can't figure out why I'm getting
"invalid conversion from char to char type*" in my charToInt() function. I think it's wanting a pointer since the getline is inside the function. Error is on line 71
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstring>

using namespace std;

int charToInt(char numberString, int LENGTH);
int calcSum(int numArray[], int count1);
int charToHighest(int numArray[], int count1);
int charToLowest(int numArray[], int count1);
bool findSpace(char numberString, int count1, bool valid);

int main()
{
    const int LENGTH = 40;
    char numberString[LENGTH];
    int numArray[LENGTH];
    int spaceCounter = 0;

    int sum = 0;
    int count1 = 0;
    int highest = 0;
    int lowest = 0;
    bool valid = true;

    do
    {

    cout << "Enter a series of digits with no spaces between them.\n";
    cin.clear();
    cin.ignore();
    cin.getline(numberString, LENGTH);



    while(numberString[count1] != '\0' && valid)
    {
        numArray[count1] = charToInt(numberString[count1], LENGTH);
        count1++;
    }


    }while(!valid);
                sum = calcSum(numArray, count1);
            highest = charToHighest(numArray, count1);
            lowest = charToLowest(numArray, count1);
            cout << "The sum of those digits is " << sum << "\n";
            cout << "The highest digit is " << highest << "\n";
            cout << "The lowest digit is " << lowest;

    return 0;
}

int charToInt(char numberString, int LENGTH)
{
    int num = 0;
    int newLength = sizeof(numberString);
    for(int lengthCount = 0; lengthCount < newLength; lengthCount++)
    {
        if(isdigit(numberString))
        {
            // Char math to convert char to int
            num = numberString - '0';
        }
        else
        {
            cout << "Incorrect input....?";
            cout << endl;
            cout << "Enter a series of digits with no spaces between them.\n";
            cin.getline(numberString, LENGTH);
            return num;
        }
    }
    return num;
}

int calcSum(int numArray[], int count1)
{
    int sum = 0;
    for(int count2 = 0; count2 < count1; count2++)
    {
        sum += numArray[count2];
    }
    return sum;
}

int charToHighest(int numArray[], int count1)
{
    int highest = numArray[0];
    for(int highCount = 0; highCount < count1; highCount++)
    {
        if(numArray[highCount] > highest)
        {
            highest = numArray[highCount];
        }
    }
    return highest;
}

int charToLowest(int numArray[], int count1)
{
    int lowest = numArray[0];
    for(int lowCount = 0; lowCount < count1; lowCount++)
    {
        if(numArray[lowCount] < lowest)
        {
            lowest = numArray[lowCount];
        }
    }
    return lowest;
}
Last edited on
int charToInt(char numberString, int LENGTH)

char is a single character.
I don't know the length of the array


OK. So without storing any input perhaps:

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

int main() {
    char highest {}, lowest {}, ch {};
    unsigned sum {};
    bool got {};

    do {
        std::cout << "Enter a series of digits with no spaces between them.\n";

        for (sum = 0, highest = '0', lowest = '9', got = false; std::cin.get(ch) && ch != '\n'; )
            if (std::isdigit(static_cast<unsigned char>(ch))) {
                got = true;
                sum += ch - '0';

                if (ch > highest)
                    highest = ch;

                if (ch < lowest)
                    lowest = ch;

            } else {
                std::cout << "Not a valid input \n";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                break;
            }
    } while (!got || ch != '\n');

    std::cout << "The sum of those digits is " << sum << '\n';
    std::cout << "The highest digit is " << highest << '\n';
    std::cout << "The lowest digit is " << lowest << '\n';
}

@TheIdeasMan ok so I need to use a pointer for numberString? or another array?
@jetm0t0 - based upon your code, then perhaps something like:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>

using namespace std;

int charToInt(char numberString, bool& valid);
int calcSum(int numArray[], int count1);
int charToHighest(int numArray[], int count1);
int charToLowest(int numArray[], int count1);

int main() {
    const int LENGTH = 40;
    char numberString[LENGTH] {};
    int numArray[LENGTH] {};

    int sum = 0;
    int count1 = 0;
    int highest = 0;
    int lowest = 0;
    bool valid = true;

    do {
        count1 = 0;
        valid = true;
        cout << "Enter a series of digits with no spaces between them.\n";
        cin.getline(numberString, LENGTH);

        while (numberString[count1] != '\0' && valid) {
            numArray[count1] = charToInt(numberString[count1], valid);
            ++count1;
        }
    } while (!valid);

    sum = calcSum(numArray, count1);
    highest = charToHighest(numArray, count1);
    lowest = charToLowest(numArray, count1);
    cout << "The sum of those digits is " << sum << "\n";
    cout << "The highest digit is " << highest << "\n";
    cout << "The lowest digit is " << lowest;
}

int charToInt(char numberString, bool& valid) {
    if (isdigit(numberString)) {
        valid = true;
        return numberString - '0';
    }

    cout << "Incorrect input....?\n";
    cin.clear();
    valid = false;
    return 0;
}

int calcSum(int numArray[], int count1) {
    int sum = 0;

    for (int count2 = 0; count2 < count1; count2++)
        sum += numArray[count2];

    return sum;
}

int charToHighest(int numArray[], int count1) {
    int highest = numArray[0];

    for (int highCount = 1; highCount < count1; highCount++)
        if (numArray[highCount] > highest)
            highest = numArray[highCount];

    return highest;
}

int charToLowest(int numArray[], int count1) {
    int lowest = numArray[0];

    for (int lowCount = 1; lowCount < count1; lowCount++)
        if (numArray[lowCount] < lowest)
            lowest = numArray[lowCount];

    return lowest;
}


- but this really is a sledgehammer approach to cracking a nut.
@seepluse Ahhh! thank you! I guess I wasn't using my bool valid correctly, I got really hung up on this one. I was stuck for a day or two since I had written inputs with cin >> and it ignores whitespace, so you need getline instead. I've been busy studying for the midterm so just now getting to this post.
Topic archived. No new replies allowed.