kd7vdb
kd7vdb

Reputation: 87

Getting Arrays to work with toupper

For some reason I keep getting a error that says "toupper cannot be used as a function". But to my undersatnding toupper is a global function that converts lowercase chars to uppercase.

#include <cctype>
#include <iostream>              
#include <string>

using namespace std;                

int main ()

{


  string input;
  string output;
  int toupper;

  cout<<"Enter a String of Charchters to be Capitalized : ";
  cin>>input;
  string arrayinput[20]= input;
  output = toupper(arrayinput);
  cout<<"\n\n\n"<<output<<"\n\n\n";



cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();      
return 0;                        
}

Upvotes: 0

Views: 1833

Answers (3)

tmpearce
tmpearce

Reputation: 12693

You've created a local variable called int toupper - rename that one to something else.

Edit to add: There are more problems than just this. input is a string; you want to iterate through the length of that string and get char* at each position using string::at(i). Then use atoi to convert the char to an int, which is the type that toupper takes as an argument.

Upvotes: 3

Alex Z
Alex Z

Reputation: 2590

You have caused an ambiguity by declaring a variable with the same name as the function. As has already been stated, simply change the name of the variable, and that should resolve it.

Upvotes: 0

Seth Carnegie
Seth Carnegie

Reputation: 75150

If you want to do it on an array of strings, then after fixing the variable name issue, use std::transform in a loop:

for (auto& str : arrayinput)
    std::transform(std::begin(str), std::end(str), std::begin(str), ::toupper);

Or if you don't have range-based for, you can use for_each:

std::for_each(std::begin(arrayinput), std::end(arrayinput), [](string& str) {
    std::transform(std::begin(str), std::end(str), std::begin(str), ::toupper);
});

Upvotes: 1

Related Questions