uppercase/lowercase with arrays

Program is supposed to take any sentence and convert the lowercase to uppercase and uppercase to lowercase. It runs, but it isn't outputting. Any help would be appreciated.

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
  #include <iostream>
#include <ctype.h> // allows the use of the functions toupper and tolower

using namespace std;

char changeCase(char arrayIN[]); // user-defined function to change case from upper-to-lower

int main ()
{

char arrayIN[256];
int i = 0;
int counter = 0;
int x,y;

{
     cout << "Enter a sentence to test. Hit enter when you are done: ";
     cin.get(arrayIN, 256);
}
    while (arrayIN[256] != '\n');
    {
        changeCase(arrayIN);
        cout << "Converted sentence: " << arrayIN << endl;
    }
    return 0;
}
char changeCase(char arrayIN[])
{

    {for (int i = 0; i < 256; i++)
        if(isupper(arrayIN[i]))
            arrayIN[i] = tolower(arrayIN[i]);
        else
        arrayIN[i] = toupper(arrayIN[i]);
    }
}
Well. It never enters the loop

while (arrayIN[256] != '\n'); What are you trying to do here?

Also another big problem. in the function changeCase, you change to whatever, but then you want to print out the change whatever in main, how is main suppose to know you changed anything? It doesnt. You have to return it to be able to print it out in main, or just print it out in the actual function.

But I think the biggest question here is, Why the h are you using a character array and not a string?
I had a feeling that was the case.

In the case of the while loop, it's supposed to exit after a test sentence is entered.

How would I get main to print out the what I want to change? Or in the case of just printing it out in the actual function?

I think it'd be a heck of a lot easier changing it to a string.

Last edited on
Its waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay better with strings.

I played around with your program. Ive never done this upper/lowercase thing, so its been a bit of a challenge. But I got it to work but only if you print out the results in the actual function.

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
void changeCase(string ss); // user-defined function to change case from upper-to-lower

int main()
{
	string ss;

	int i = 0;
	int counter = 0;
	int x, y;

	cout << "Enter a sentence to test. Hit enter when you are done: ";
	getline(cin, ss);

	changeCase(ss);
	
	system("pause");
	return 0;
}

void changeCase(string ss)
{
	if (isupper(ss[0]))
	{
		ss[0] = tolower(ss[0]);
		cout << "Converted sentence: " << ss << endl;
	}
	else if (islower(ss[0]))
	{
		ss[0] = toupper(ss[0]);
		cout << "Converted sentence: " << ss << endl;
	}
}


If there is anything you dont understand please ask. But We'll need someone more experienced to show us how to return the string, and print it out in main :)
Last edited on
Thank you so much for the help! :)

I'll have to play around with strings more to get used to using them.
And yes, it'd be great to know how to print the string out in main. :)
Topic archived. No new replies allowed.