473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 21995
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,065 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

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

Similar topics

7
by: Kerri | last post by:
Hi, I have a string 'hello world' I can UCase my string using String.ToUpper Is there anyway to just UCase the first letter ofe ach word? Thanks,
2
by: JasBascom | last post by:
if i have struct crecord{ char record_type; } if record_type = 'c'; how do I make record_type toupper?
7
by: Duane | last post by:
Aside from the pitfalls of using this function, according to the standard, what is the correct way to call it? #include <string> #include <locale> // seems to work with BCC5.6/STLPort...
18
by: didgerman | last post by:
Chaps, I need to properly format the case of a struct. Can I just hit it with tolower, and then 'while (string ==' ') pos++; string=toupper(string); to add in the higher case for the start of...
3
by: gelbeiche | last post by:
I have a question regarding the following small C program. #include <locale.h> int main() { char* loc = 0; char before,after; int i;
0
by: Shrinivas Reddy | last post by:
Hi, I am using the ToUpper() function in an FXCop rule which checks whether a boolean variable has "is" or "has" as the prefix. The ToUpper() function does not work. When I put the expression...
48
by: Frederick Gotham | last post by:
The "toupper" function takes an int as an argument. That's not too irrational given that a character literal is of type "int" in C. (Although why it isn't of type "char" escapes me... ) The...
4
by: sandy | last post by:
I am trying to upper case a string, so I have this method: string FileSystem::toupper(string S) { for (int i=0; i<S.length(); ++i) { S=toupper(S); } return S;
13
by: JanWhitney | last post by:
I am learning C++, so please be kind. Is there anywhere that I can view the source code for the toUpper function? Thanks.
16
by: gaga | last post by:
my function should accept a pointer to a string as its argument. and it should convert each charater to an uppercase letter. I know what i need to do, its just my syntax is all out of whack. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.