Problem with C string program and cin

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

using namespace std;
const int SIZE = 50;

void reverse(char *);
void lower(char *);
void upper(char *);


int main()
{
	char cstr[SIZE];
	cout << "Enter a sentence.\n";
	
	cin.getline(cstr, SIZE);

	reverse(cstr);
	
	lower(cstr);

	upper(cstr);

	return 0;
}



void reverse(char *str)
{
	for (int count = 0; count < SIZE; count++)
	{
		if (isupper(str[count]))
			str[count] = tolower(str[count]);
		else if (islower(str[count]))
			str[count] = toupper(str[count]);
	}
	cout << str << endl;
}

void lower(char *str)
{
	for (int count = 0; count < SIZE; count++)
		str[count] = tolower(str[count]);
	cout << str << endl;
}

void upper(char *str)
{
	for (int count = 0; count < SIZE; count++)
		str[count] = toupper(str[count]);
	cout << str << endl;
}


Can someone please help me with what is causing this program to error when it runs? It compiles and the window opens but when I type a sentence and press enter I get an error message and have to abort. I first made the program with the first char array in main as char cstr[SIZE] = "Mary Had a Little Lamb"; so that I would not have to type something in everytime I ran the program for a test. Initially the program worked this way. When the programming was completed I then changed main to
1
2
3
 
char cstr[SIZE];
cin.getline(cstr, SIZE);

and I am now receiving this error:
1
2
3
4
5
6
Debug Assertion Failed.
Line: 56

Expression: c >= -1 && c <= 255

For information on how your program... yada yada.

What the heck!
Okay I found the way to correct it is to change the count < SIZE part of the for loops to count < strlen(str) but I don't understand why? I declared SIZE as a global variable and I can't see any other reason as to why it wouldn't work. Any one else know?
We have
1
2
const int SIZE = 50;
char cstr[SIZE]; // array of 50 characters 


cin.getline(cstr, SIZE); User enters: Hello
Though the size of the array is 50, only five characters have been entered; the array contains these five characters plus a null terminating character at the beginning. The remaining 44 characters are uninitialised and should not be used.

The name reverse does not suggest toggling the case: maybe rename it as reverse_case?

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

// using namespace std; // **** avoid

const int SIZE = 50;

void reverse_case(char *);
void lower(char *);
void upper(char *);

int len( const char* cstr ) // *** added: find the length of a string
{
    /*
    int n = 0 ;
    while( cstr[n] != 0 ) ++n ;
    return n ;
    */
    return std::strlen(cstr) ;
}

int main()
{
    char cstr[SIZE];
	std::cout << "Enter a sentence.\n";

	std::cin.getline(cstr, SIZE);

	reverse_case(cstr);
	std::cout << cstr << '\n' ;

	lower(cstr);
	std::cout << cstr << '\n' ;

	upper(cstr);
	std::cout << cstr << '\n' ;

	// return 0; // **** implicit return 0 ;
}

void reverse_case(char *str)
{
    const int sz = len(str) ;
	for (int count = 0; count < sz ; ++count )
	{
	    if( std::isupper( str[count] ) ) str[count] = std::tolower( str[count] ) ;
	    else str[count] = std::toupper( str[count] ) ;
	}
	//std::cout << str << endl;
}

void lower(char *str)
{
    const int sz = len(str) ;
	//for (int count = 0; count < SIZE; ++count )
	for (int count = 0; count < sz ; ++count )
		str[count] = tolower(str[count]);
	//std::cout << str << endl;
}

void upper(char *str)
{
    const int sz = len(str) ;
	//for (int count = 0; count < SIZE; ++count )
	for (int count = 0; count < sz ; ++count )
		str[count] = toupper(str[count]);
	//std::cout << str << endl;
}

http://coliru.stacked-crooked.com/a/f3359cc2a1da9937
Topic archived. No new replies allowed.