Convertin String to uppper case

Earlier I converted character strings to all uppercase letters. Now i'm trying to do string variable to upper case. May I know what is wrong with my code? I'm not getting an output at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
	//Name
	cout<<"\n\n\n\tType in name: ";
	string name;
	char outputname;
	getline(cin,name);

	for(int index=0;name[index]<name.length();index++)
	{
		outputname=(char)toupper(name[index]);
		cout<<outputname;
	}

	
1
2
3
4
5
6
7
8
9
10
11
12
13

//Name
	cout<<"\n\n\n\tType in name: ";
	string name;
	char outputname;
	getline(cin,name);

	for(int index=0;index<=name.length();index++)
	{
		outputname=(char)toupper(name[index]);
		cout<<outputname;
	}



fixed my expression but i'm getting an error saying debug assertion failed. string subscript out of range.
Anyone wants to volunteer :D
Sounds straightforward. Index out of range. You're using index 0-name.length, but name[name.length] is one element past the end of your string. Remember that arrays are 0-based.
Last edited on
Booradely I'm still not clear on this, but I'm assuming I'm suppose to change the range for that expression or make index=1.also, I thought name.length returns the total number of characters in the string.
I would use an iterator, but since you're a beginner: use the .size() member function of string. size will return the actual size (the number of characters in the string, aka: 0 being nothing) and you can use that.

(string.size() - 1) would be the last element in the string. If you want to loop through it, just use a for loop from 0, and while x < your_string.size().

for(unsigned int x = 0; x < your_string.size(); x++). Make an effort to understand this, or you will pay for it: you will be using these basics a lot as you progress.

Also, chars are implicitly converted to unsigned integers. You do not need to typecast toupper().

Also, you can use cout on toupper... it will display the return value.

cout<< toupper(your_string[x]);

******************************
Important: Your for-loop is bad: you can not use that. Study for loops in more detail. Hint: you should use a nested for-loop for this, or write another function that will display a string in all caps.
Last edited on
@madsurfman

That is unnecesary for such a simple task.
C++11
for (auto& c : s) c = toupper( c );

C++98
std::transform( s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper );
@IWishIKnew
Boost solution is the simplest to use, I find it most suitable for a simple task. (although it would indeed not answer OP's question "what's wrong with my code")

also, just for fun, lesser-known C++98 function:

std::use_facet< std::ctype<char> >(std::locale()).toupper(&s[0], &s[0] + s.size());

Last edited on
To be fair, Boost's solutions are the most comprehensive using the Boost Locale Library.
http://www.cplusplus.com/faq/sequences/strings/case-conversion/

Because case conversion is a difficult subject.

To do it right, first install ICU. Then install (compile) boost locale, making sure that it hooks ICU (it should by default, IIRC, but on Windows at least you might have to make sure that it does).

Once done, life gets significantly easier. You still have to be careful with multi-byte characters, of course.

For example, in the example in the FAQ, if you started with "GrüSSEN", then to_lower() would have returned "grüssen", instead of the correct (in this particular case) "grüßen".

Good luck!
Topic archived. No new replies allowed.