toupper problem

Sep 6, 2009 at 6:49am
//i want to convert a string in to uppercase
// but it gives me an error:
//could not find a match for 'toupper(charT)(string)' in function main()
//could somebody explain what the error meant and give me a solution tnx..

#include<string.h>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;


void main()
{
int i, iNumberInput;
clrscr();
cout <<"Enter number of inputs: \n";
cin >> iNumberInput;
int* iNumber = new(nothrow) int[iNumberInput];
string* sName = new(nothrow) string[iNumberInput];
i=0;
clrscr();
for(int x=0;x<iNumberInput;x++,i++)
{
cout << "please input the following data!" << endl << endl;
cout << "Enter a number input ."<< (i+1) << endl;
cin >> iNumber[x];
cout << "Enter a name input ."<< (i+1) << endl;
cin >> sName[x];
clrscr();
}
cout << "you are done with the inputs!" << endl << endl;
cout << "The following are: " << endl << endl;
for(int x=0;x<iNumberInput;x++)
{
cout << sName[x] << " is "<< iNumber[x] << " years old." << endl;
}
cout << endl;
for(int x= 0;x<iNumberInput;x++){
for(int d = iNumberInput;d>x;d--){
if(iNumber[x]>iNumber[d]){
cout << sName[x] << " is older that " << sName[d] <<"." << endl;
}}}
cout << endl;
for(int x= 0;x<iNumberInput;x++){
for(int d = iNumberInput;d>x;d--){
if(iNumber[x]>iNumber[d]){
cout << sName[d] << " is " << (iNumber[x]-iNumber[d]) << " years younger than " << sName[x] <<"." << endl;
}}}
cout << endl;
for(int x=0;x<iNumberInput;x++)
{
cout<< "there are " << sName[x].length() <<" characters in: " << sName[x]<< endl;
}
cout << endl;
int d=0;
string rave = sName[0];
for(int x=0;x<iNumberInput-1;x++,d++){
cout << "uppercase of " << sName[d] <<" is: "<< toupper(sName[d+1]) << endl;
sName[d] = sName[d+1];
if(x==iNumberInput-2)
{
cout << "uppercase of " << sName[d] <<" is: "<< toupper(rave) << endl; // <=
}} // this part
sName[d] = rave;
cout << endl;
d=iNumberInput-1;
string rave2 = sName[d];
for(int x=0;x<iNumberInput-1;x++,d--){
cout << "lower of " << sName[d] <<" is: "<< tolower(sName[d-1]) << endl; // <=
sName[d]= sName[d-1]; // this part
if(d==1)
{
cout << "lowers of " << sName[d] <<" is: "<< tolower(rave2) << endl;// <=
} // this part
}
delete [] sName;
delete [] iNumber;
}
Sep 6, 2009 at 6:56am
std::transform(string.begin(),string.end(),string.begin(),toupper);
Don't forget to include <cctype> and <algorithm>.
Sep 6, 2009 at 10:25am
does it work on tc++ compiler? because i use a different compiler here at home
Sep 6, 2009 at 10:31am
How is he meant to know? Do you expect him to go downloading some compiler he's possibly never heard of?
If "tc++" is a standards-compliant compiler, it will work. What compiler do you use "at home"?
Sep 6, 2009 at 5:15pm
Yes, it should work on any compiler released since 1998.
Sep 6, 2009 at 6:11pm
Because of the way toupper() is handled in various compilers, you also need to use <functional> and cast it to the proper type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

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

Hope this helps.
Sep 6, 2009 at 6:14pm
You mean there are compilers where toupper() isn't int toupper(int)?
Sep 6, 2009 at 6:17pm
GCC. toupper is overloaded to handle all the fancy C++ stuff.
Sep 6, 2009 at 6:17pm
I think he means the actual code... but then that doesn't explain why you need to typecast anything...
Sep 7, 2009 at 11:15am
helios and duoas when i put your codes it gives me and error

my code gives me a error: could not find a match for 'toupper(charT)(string)' in function main()

helios gives an error: colud not find a match for trasform(InputIterator,OutputIterato,UnaryOperation)(char *,char *,char *,charT (*)(charT,const locale &))' function main()

duoas gives me a error: cannot generate template specialization for toupper(int), in function uppercase(const string)

colud not find a match for ptr_fun(Arg1,Arg2,Result)(charT (*)(charT,const locale &))' function uppercase (const string &)

colud not find a match for trasform(InputIterator,OutputIterato,BinaryOperation)(const char *,const char *,char *,undefined )' in function uppercase(const string &)

could not find a match for 'toupper(charT)(string)' in function main()

// im using the cmd as may complire so the txt error here is not copy paste

// maybe i forgot some thing or im missing some thing in may codes?
Sep 7, 2009 at 5:52pm
helios gives an error: colud not find a match for trasform(InputIterator,OutputIterato,UnaryOperation)(char *,char *,char *,charT (*)(charT,const locale &))' function main()
Did you include <algorithm> like I told you to?
Sep 8, 2009 at 3:24am
yup
Sep 8, 2009 at 4:05am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string UCASE(std::string s)
{
  if(s.empty())
    return s;
  else
  {
    for(int i = 0; i < s.size(); i++)
    {
      if(s[i] >= 'a' && s[i] <= 'z')
        s[i] = (s[i] - ('a' - 'A'));
    }
  }
  return s;
}


This is what I use.
Sep 8, 2009 at 4:40am
yup
Your compiler sucks.
Sep 8, 2009 at 11:07am
tnx garob
Topic archived. No new replies allowed.