Error Message When using c-strings

Hello, today I had an assignment regarding c-strings. It states,
"Write a program that prompts the user to enter a strings and outputs the string
in uppercase letters ( use a character array to store the string)"

And here is my code:

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

using namespace std;
void toUppercase(char array[]);
int main()
{
	char toUpper[16];
	cout << "Enter a string with no more than 15 characters." << endl;
    cin >> toUpper;

	toUppercase(toUpper);

}

void toUppercase(char toUpper[])
{
	for (int i = 0 ; i <= 16 ; i++)
	{
		if (islower(toUpper[i]))
		{
			toupper(toUpper[i]);
		}
	}

cout << toUpper << endl;
}


After the code is executed, I get an error that states,
" Debug Assertion Failed!
Progarm:[Program Directory]
File: f:\dd\vctools]crt_bld\self_x86\crt\src\isctype.c
Line: 56
Expression: (unsigned)(c+1) <= 256"

Does anyone know what the heck I could be doing wrong?
There are a few reasons it isn't working right now.

Even if the c-string was full for (int i = 0 ; i <= 16 ; i++) wouldn't run correctly.
You declared an array size 16 elements, seeing as C++ uses zero based indexing that means it uses 0 - 15 (does not include 16 itself).

The second problem with that for loop is you are looping through 16 elements whatever happens which will cause an error is the user enters in a smaller amount. You will want to get the length of the c-string first (using the function strlen) then loop through that amount.

Finally you are not changing anything in the array at toupper(toUpper[i]);
You will want to do something like this to make sure the array actually gets changed toUpper[i] = toupper(toUpper[i]);
James is right. Change the test to i < 16 and that part would be fine.

Also, toupper() returns the changed character so you need to assign it back to the char element you wish to change.
Oh, duh, I wasn't thinking and totally forgot about the zero place. And thank you for telling me about the toupper function. I didn't know that.
That's wierd. I'm still getting that same message even after changing what you said. It looks like it should work, but is still giving me that message.

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

using namespace std;
void toUppercase(char array[]);
int main()
{
	char toUpper[16];
	cout << "Enter a string with no more than 15 characters." << endl;
    cin >> toUpper;

	toUppercase(toUpper);

}

void toUppercase(char toUpper[])
{
	for (int i = 0 ; i < 16 ; i++)
	{
		if (islower(toUpper[i]))
		{
			toUpper[i] = toupper(toUpper[i]);
		}
	}

cout << toUpper << endl;
}


Refer to my post above.

The second problem with that for loop is you are looping through 16 elements whatever happens which will cause an error if the user enters in a smaller amount. You will want to get the length of the c-string first (using the function strlen) then loop through that amount.


1
2
int Length = strlen(toUpper);
for (int i = 0 ; i < Length ; i++)
Last edited on
After implementing what you had added, the output only displays the first word in all caps. So if I imput "Hello James", it only says "HELLO".
That is because cin >> only ever reads user input until the first space is reached. If you want to get the entire line while using c-strings you will want to do something like this.

cin.getline(toUpper, 16);

http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Topic archived. No new replies allowed.