469,275 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

C++ toupper()

eboyjr14
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
7 19488
Meetee
931 Expert Mod 512MB
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
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
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
9,064 Expert Mod 8TB
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
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.
May 2 '19 #8

Post your reply

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

Similar topics

7 posts views Thread by Kerri | last post: by
2 posts views Thread by JasBascom | last post: by
7 posts views Thread by Duane | last post: by
18 posts views Thread by didgerman | last post: by
3 posts views Thread by gelbeiche | last post: by
reply views Thread by Shrinivas Reddy | last post: by
48 posts views Thread by Frederick Gotham | last post: by
4 posts views Thread by sandy | last post: by
13 posts views Thread by JanWhitney | last post: by
16 posts views Thread by gaga | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.