By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

C++ toupper()

eboyjr14
P: 27
I am very new to C++. I am just learning and wanted to make a simple program that capitalizes the input. What is wrong with this? I use Bloodshed Dev-C++. I get this error:

no matching function for call to `toupper(std::string&)'
candidates are: int toupper(int)

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     while( true )
  10.            {
  11.            cout << "fire> ";
  12.            string cmd;
  13.            getline(cin, cmd);
  14.            string upp;
  15.            upp = toupper(cmd);
  16.            cout << upp << endl << endl;
  17.            }
  18.     return EXIT_SUCCESS;
  19. }
  20.  
Jun 21 '07 #1
Share this Question
Share on Google+
7 Replies


Meetee
Expert Mod 100+
P: 931
I am very new to C++. I am just learning and wanted to make a simple program that capitalizes the input. What is wrong with this? I use Bloodshed Dev-C++. I get this error:

no matching function for call to `toupper(std::string&)'
candidates are: int toupper(int)

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     while( true )
  10.            {
  11.            cout << "fire> ";
  12.            string cmd;
  13.            getline(cin, cmd);
  14.            string upp;
  15.            upp = toupper(cmd);
  16.            cout << upp << endl << endl;
  17.            }
  18.     return EXIT_SUCCESS;
  19. }
  20.  

use char * instead of string
Jun 21 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
You don't need to revert to C and char*. You can still use your string. Each element of the string is a char.
Expand|Select|Wrap|Line Numbers
  1. string str("aBcDeF");
  2. int length = str.size();
  3. for (int i = 0; i < length; ++i)
  4. {
  5.     str[i] = toupper(str[i]);
  6. }
  7. cout << str << endl;
  8.  
You can even avoid writing the loop byb using the transform() algorithm:
Expand|Select|Wrap|Line Numbers
  1. string str("aBcDeF");
  2. transform(str.begin(), str.end(), str.begin(), toupper);
  3. cout << str << endl;
  4.  
Now you are down to three lines of code.
Jun 21 '07 #3

P: 84
I am very new to C++. I am just learning and wanted to make a simple program that capitalizes the input. What is wrong with this? I use Bloodshed Dev-C++. I get this error:

no matching function for call to `toupper(std::string&)'
candidates are: int toupper(int)

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     while( true )
  10.            {
  11.            cout << "fire> ";
  12.            string cmd;
  13.            getline(cin, cmd);
  14.            string upp;
  15.            upp = toupper(cmd);
  16.            cout << upp << endl << endl;
  17.            }
  18.     return EXIT_SUCCESS;
  19. }
  20.  
Be careful:
use #include <cstring> or #include <string.h> but be careful because <string> does not exist!!
Jun 23 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Be careful:
use #include <cstring> or #include <string.h> but be careful because <string> does not exist!!
What does this mean?

string.h is the C string header.
cstring is the C++ version of the C header
string is the header for the C++ string class inthe C++ Standard Library

Why do you say <string> does not exist? It has to exist if the compiler is sold as C++ compiler.
Jun 24 '07 #5

P: 47
You would do well to remove the arguments in the main () when starting to use Dev, using their template causes you to follow those argument parameters. I would get rid of the template return exit success also, simply build a template with your most often used headers, int main () and a return 0; I like to add a system ("pause"); also prior to the return statement otherwise a lot of programs I write I cannot get to display in the cmd screen.

A cstdlib does include # <string>, works for me anyway. When using the manipulator toupper I always include <iomanip>, don't know if you need the library or not but it seems easier to simply use it if your not sure. Anytime I am manipulating anything even setw(3) I use #include <iomanip>, give it a shot it may help you out. You also refer to your 'get' with 'cin' the common input operator which I believe you needed to call prior to, I'm just learning myself.
Whenever I use any of the following I include iomanip in my file headers.

C++ I/O Manipulators

boolalpha
dec
fixed
hex
internal
left
noboolalpha
noshowbase
noshowpoint
noshowpos
noskipws
nounitbuf
nouppercase
resetiosflags
right
scientific
setbase
setfill
setiosflags
setprecision
setw
showbase
showpoint
showpos
skipws
unitbuf
uppercase
ws

Good luck.
Aug 30 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
no matching function for call to `toupper(std::string&)'
candidates are: int toupper(int)
I thought I would just mention because, no-one else has, that the actual reason for this warning is that the only toupper() function that exists is in the C runtime library and has this prototype

int toupper( int ch );

you need to include ctype.h (C) or cctype (C++)

This function takes a character value (in C character constants have type int hence the function takes and returns int) and returns the uppercased version of that character.

You tried to pass a string to toupper, since that does not match this prototype your compiler looked for a function overload that did take a string, failed to find one and produced the error.
Aug 30 '07 #7

P: 2
What does this mean?

string.h is the C string header.
cstring is the C++ version of the C header
string is the header for the C++ string class inthe C++ Standard Library

Why do you say <string> does not exist? It has to exist if the compiler is sold as C++ compiler.
2 Weeks Ago #8

Post your reply

Sign in to post your reply or Sign up for a free account.