Cstring Uppercase method?

I have this code that will take the cstring from the user. Total limit inside the cstring would be 10 including the null character. I'm trying to convert the user input string to all uppercase letters. Unfortunately, I m not able to do so and i'm getting a silly output like this. I type in hello there. cstring stores in hello ther. and then this is the output


hello ther7269767932.......




1
2
3
4
5
6
7
8
9
10
11
12
13
14
  char cstring[10];
	cout<<"\n\tType in a string: ";
	cin.get(cstring,11);

	cout<<"\n\n\t" <<cstring;

	int index=0;
	char ch=0;
	for(index=0;index<10;index++)
	{
		ch=cstring[index];
		cout<<toupper(ch);
	
	}
Also, May I know what this function is doing? I just read the book and realized that toupper will return an int value. I would like to know what this function is doing, but would also like receiving an hint please :0
Hello :)
I think you have your 10 and 11 mixed up. In other words, cin.get should pull in no more characters than the array can hold.
If all you want to do is to take a user input and show them as uppercase letters use this..

line 12 cout<<char(toupper(ch));
Alright I got that :) but what is my original code doing? And what does your code do. Yes I know it converts it to uppercase :D
Oh and booradely, as far as I know and have heard the extra 11 is used on c strings because of the null character. If I have ten in the character array, it will store only 9, that's why I have 11 on the get function so it can discard the null.
I have just converted the int from toupper() to char. For ex. if toupper() returns 72 then char(72) is H.
What I'm saying is that you don't want to discard the null. The way c-strings are usually interpreted is that they go on until the null terminator is detected. Since there isn't such a character stored in your array, it keeps right on printing garbage after the contents of the array are exhausted.

EDIT: toupper(ch) returns an int, so you would have to perform some sort of conversion (like tom221b did by adding char) so that cout's << operator prints the character instead of its integer equivalent.
Last edited on
Was checking my code out and I figured out what you were saying. I get the results I want when I enter in the characters more then 10. If I enter in less then 10, I get garbage out of it. How can I not discard the null in my case?

Tried using getline, but I guess that was a wrong approach :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char cstring[10];
	cout<<"\n\tType in a string: ";
	cin.getline(cstring,10);

	int index=0;
	char ch;
	for(index=0;index<10;index++)
	{
		ch=cstring[index];
		cout<<(char)toupper(ch);
	
	}


Apologize for asking to many questions. Fyi, this is not my homework and i'm not looking forward to get answers from here. Same code. I was just practicing converting the string to uppercase. I'm not to the uppercase part yet, but for the second part of my code. String variable. It doesn't let the user input a name.In fact, it just scrolls doing to, the name is...
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

int _tmain(int argc, _TCHAR* argv[])
{

	char cstring[10];
	cout<<"\n\tType in a string: ";
	cin.getline(cstring,10);

	int index=0;
	char ch;
	for(index=0;index<10;index++)
	{
		ch=cstring[index];
		cout<<(char)toupper(ch);
	
	}

	//Type in a string variable

	string name;
	cout<<"\n\n\tEnter Your name: ";
	getline(cin,name);

	cout<<"name is" <<name;


	_getch();
	return 0;
}
either something like this:
1
2
3
4
5
6
7
8
    char cstring[10];
    cout<<"\n\tType in a string: ";
    cin.getline(cstring,10);
	
    for (int i=0; i<10; i++)
        cstring[i] = toupper(cstring[i]);
	
    cout << cstring;


Or change the for loop to terminate when the null terminator is found:
1
2
3
4
5
6
7
8
    char cstring[10];
    cout<<"\n\tType in a string: ";
    cin.getline(cstring,10);
    
    for (int index=0; char ch=cstring[index]; index++)
    {
        cout<<(char)toupper(ch);
    }

Got it chervil :D


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

char cstring[10];
	cout<<"\n\tType in a string: ";
	cin.getline(cstring,10);

	int index=0;
	char ch;
	for(index=0;index<10;index++)
	{
		ch=cstring[index];
		if (ch=='\0')
		{
			break;
		}
		cout<<(char)toupper(ch);
	
	}
That code looks ok. Though your code is doing in a long way, what my code already does in the for loop condition.
I thought you were giving me an idea :D Unforunately,i can't see where your terminating the null character in your for loop. It looks identical to my other previous code...
In this loop here:
1
2
3
4
    for (int index=0; char ch=cstring[index]; index++)
    {
        cout<<(char)toupper(ch);
    }
the for loop will execute so long as the condition is true.

What is the condition? char ch=cstring[index]
Here the character cstring[index] is assigned to the variable ch.
Then the resulting value is evaluated as a boolean (true or false) value. Any non-zero value (such as a letter of the alphabet) gives true and a zero byte will give false. Thus the body of the loop is executed only for the actual text of the string.

Hope that makes sense.
Topic archived. No new replies allowed.