toupper not working

1
2
3
4
5
6
7
8
9
10
11
12
    // Get firstname/lastname
    std::string firstname;
    std::string lastname;

    std::cout<<"Enter First Name? ";
    std::cin>>firstname;
    std::cin.ignore();
    firstname.toupper(firstname.begin(), firstname.end());
    std::cout<<"Enter Last Name? ";
    std::cin>>lastname;
    std::cin.ignore();
    lastname = toupper(lastname);

I have tried using it as string.toupper()
and string = toupper(string) and neither are working, both
are returning different errors. I know now that it's suppose
to take an int..I thought I can pass a string and have it capitalize the
first letter. In this case I just have a word that I need capitalized.
Is this possible?
This format will work with your program, I added a cout to test my toupper arguements, they seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	// Get firstname/lastname
	#include <iostream>
	#include <string>
	int main()
	
{
	int i;
		
	std::string firstname;
	std::string lastname;
	std::cout<<"Enter First Name? ";
	std::cin>>firstname;
	std::cin.ignore();
	for(i=0; firstname[i] ; i++) firstname [i]=toupper(firstname[i]);
	std::cout<<"Enter Last Name? ";
	std::cin>>lastname;
	std::cin.ignore();
	for(i=0; lastname[i] ; i++) lastname [i]=toupper(lastname[i]);	
	std::cout << firstname << " " << lastname;
	return 0;
}



Enter First Name?  tom
Enter Last Name?  turkey
TOM TURKEY


Just to keep it simple and nice :

1
2
3
4
5
6
7
#include <string>
#include <algorithm>
#include <cctype>

void string_to_upper(std::string& str) {
    std::transform( str.begin(), str.end(), str.begin(), &toupper );
}
Nice R0mai:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

void string_to_upper(std::string& str) {
    std::transform( str.begin(), str.end(), str.begin(), &toupper );
}

int main()
{
    std::cout << "Hello world!" << std::endl;
    std::string hw="Hello world!";
    string_to_upper(hw);
    std::cout << hw << std::endl;
    return 0;
}


Hello world!
HELLO WORLD!
See that is the confusing part. I am not wanting the entire word to have upper case. I just want the FIRST letter. Like if you had a sentence..make the first letter of that sentence an uppercase. or in this case since it's a first name/last name just the first letter of the first/last name would need to be just uppercase and the rest lowercase (regardless of how the user enters it in. That is what confused me about toupper...it isn't doing it the way I was expecting.
1
2
    std::string str = "hello world";
    std::transform( str.begin(), str.begin()+str.find(' '), str.begin(), &toupper );


EDIT: Oops, I misread tour post, just realized, you wanted to capitalize only the first letter, not the first word. Sorry
Last edited on
Here is the program just Capitalizing the first letter, same format, simple and nice too :):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// Get firstname/lastname
	#include <iostream>
	#include <string>
	int main()
	
{
	int i;
		
	std::string firstname;
	std::string lastname;
	std::cout<<"Enter First Name? ";
	std::cin>>firstname;
	std::cin.ignore();
	firstname [0]=toupper(firstname[0]);
	std::cout<<"Enter Last Name? ";
	std::cin>>lastname;
	std::cin.ignore();
	lastname[0]=toupper(lastname[0]);
	std::cout << firstname << " " << lastname;
	return 0;
}




Enter First Name?  lloyd
Enter Last Name?  christmas
Lloyd Christmas


Last edited on
Not to nit-pick, but you need to make sure to cast the proper toupper() function. Otherwise your code will fail in different compilers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>
#include <cctype>
#include <functional>

std::string uppercase( const std::string& s )
  {
  std::string result( s );
  std::transform(
    result.begin(),
    result.end(),
    result.begin(),
    std::ptr_fun <int, int> ( std::toupper )
    );
  return result;
  }

Hope this helps.
thanks Duoas, I think my c++ tutorial is somewhat outdated (looking at the copyright date),hmmm 1879,, my next assignment is void functions on the abacus, and adding wooden sticks to a stack and figuring out FIFO methods, kinda like Jenga. Well at least 1879 was a good year for french wine, hehe.

What is a good current c++ beginners tutorial, I feel like I am falling behind because my tutorial doesnt include many current library functions to practice.
That
firstname[0] = toupper(firstname[0]);
worked perfectly. First off I didn't know you could access characters in a string using the array keys. I also didn't know that was how you passed it via toupper. That will help a lot. Thanks again.
Topic archived. No new replies allowed.