Hi!
I have just started on c++ and have encountered Expression: (unsigned)(c+1) <=256 Error when I am trying to run the program. The program is suppose to generate an array of letters (lowercase and uppercase) of size from
15 to 25. After the generation, using ONLY one while-loop, try to move all lowercase
letters to the left of the array and all the uppercase letters to the right of the array. It have to contain the following criterias
(a) A function to construct the array
(b) A function to print out the array elements
(c) A function to swap two elements
(d) An iterative function (using ONE loop) to move the array elements so that all the
lowercase letters are on the left hand side of the array and all the uppercase letters
are on the right hand side of the array.

Can anyone please help me with this as I am totally lost and thanks in advance!

#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

const int MAX = 100;
void consArray (char [], int);
void printOut (char [], int);
void swapEle (char[], int, int, int);
void iterative (char[], int); 

int main ()
{
    int length;
    char alpha[MAX];

    srand (time (NULL));

    cout << "Given the following array" << endl;
    length = rand() % 25 + 15;
    consArray(alpha, length);
    printOut(alpha, length);
    cout << "\nIterative swap of array" << endl;
    iterative(alpha, length);
    printOut(alpha, length);
}

void consArray (char myAlpha [], int length)
{
    int even, val;
    char alpha;

    for (int i=0; i < length; i++)
    {
        even =  rand() % 2;
        val =  rand() % 26;

        if (even == 0)
        {
            alpha = toupper(val + 65);
        }

        else 
        {
            alpha = tolower(val + 65);
        }
        myAlpha[i] = alpha;
    }

}

void printOut (char alpha[], int length)
{
    for (int i = 0; i < length; i++)
    {
        cout << "  " << alpha[i];
    }
    cout << endl;
}

void swapEle (char alpha[], int loca1, int loca2)
{
    char temp;
    temp = alpha[loca1];
    alpha [loca1] = alpha [loca2];
    alpha [loca2] = temp; 
}

void iterative (char alpha[], int length)
{
    int a = length;
    int i = 0; 

    while (i < length && a >= 0)
    {

        if(isupper(alpha[i]) && islower(alpha[a]))
        {
            swapEle (alpha, i, a);
            a--;
            i++;
        }
        else if (islower(alpha[i]) && isupper(alpha[a]))
        {
            i++;
            a--;
        }
        else if (islower(alpha[i]) && islower(alpha[a]))
        {
            i++;
        }
        else if (isupper(alpha[i]) && isupper(alpha[a]))
        {
            a--;
        }

    }
}

Recommended Answers

All 4 Replies

Which line contains the error? What compiler are you using? I compiled with Visual Studio 2013 and did not get any errors.

If I had to guess, one of your character tests or manipulators (isupper, toupper, islower, etc) is failing an assertion because you're giving it bad input.

isupper is failing, vs2010 same error.

isctype.c

#if defined (_DEBUG)
extern "C" int __cdecl _chvalidator(
        int c,
        int mask
        )
{
        _ASSERTE((unsigned)(c + 1) <= 256); //<<<<<<<<<<<<<<<<< here
        return _chvalidator_l(NULL, c, mask);
}

does it expect unsigned char?

I think Microsoft removed that in later versions of the compiler because I can't find isctype.h in either 2012 or 2013. Here is anoter thread on the same topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.