-2

I was wondering how to use the toupper function so when the user can type in these words any way they want.

    //fill in arrays
     word[0]  =  "VERY";
     word[1]  =  "MERRY";
     word[2]  =  "CHRISTMAS";
     word[3]  =  "EVERYONE";


    for (i= 0; i<4; i++)
    {
        word[i] = toupper(word[i]);
    }

    food[0]  =  "CANDY";
    food[1]  =  "CAKE";
    food[2]  =  "SOUP";
    food[3]  =  "COOKIE";


    for (j=0;j<4;j++)
      {
        food[j] = toupper(food[j]);
     } 


     cout<<"\n Pick a word it can either be VERY, MERRY, CHRISTMAS, EVERYONE (can be written in anyway)";
     cin>> word[i];


     cout<<"\n Pick a food, it can be CAKE,SOUP, CANDY, OR COOKIE, can be written anyway)";
     cin>> food[j];

Also what does this error mean

[Error] no matching function for call to 'toupper(std::string&)'
Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • 2
    [`std::toupper`](https://en.cppreference.com/w/cpp/string/byte/toupper) accepts *characters* (more to precisely a character `int`), not `std::string`s, as its sole argument. In other words, the error message is telling you *exactly* what the problem is. – WhozCraig Jan 19 '20 at 19:29
  • 2
    Does this answer your question? [Convert a String In C++ To Upper Case](https://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case) – Zheng Qu Jan 19 '20 at 19:33
  • Search the internet for "c++ string transform toupper". – Thomas Matthews Jan 19 '20 at 19:43
  • I looked at that it's not that helpful –  Jan 19 '20 at 20:22
  • @LolaBenson "_I looked at that it's not that helpful_" In what way it isn't helpful? – Algirdas Preidžius Jan 19 '20 at 20:29
  • @AlgirdasPreidžius it doesn't answer my question about how to use it on arrays –  Jan 19 '20 at 20:33
  • @LolaBenson How is using it on string arrays that much different? Just do the same operation, that does it for a single string, on every array element. – Algirdas Preidžius Jan 19 '20 at 20:35
  • @AlgirdasPreidžius well that's the part i don't understand how to do –  Jan 19 '20 at 20:36
  • @LolaBenson You seem to show understanding on how to iterate through the array, and modify the values of the array, in the question itself. Hence, this is why I fail to understand, what, **exactly**, you don't understand about the linked question, in relation to applying it to array elements. – Algirdas Preidžius Jan 19 '20 at 20:48
  • @AlgirdasPreidžius you know what dw about it. I just wont use it –  Jan 19 '20 at 20:56

3 Answers3

1

I always wrap them up in convenience functions for my own sanity:

// uppercase a string
static inline string upper(string s) {
    std::transform(s.begin(), s.end(), s.begin(), [](int ch) { return std::toupper(ch); });
    return s;
}

Then you can just apply them like you'd like to:

for (j=0; j < 4; j++) {
     food[j] = upper(food[j]);
}

Assuming you have an array of strings.

gct
  • 14,100
  • 15
  • 68
  • 107
1

If you have an array with element type std::string like this

std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

then you can convert all its elements for example to the lower case the following way using the range-based for loop

#include <string>
#include <cctype>

//…

for ( auto &s : word )
{
    for ( char &c : s ) c = tolower( ( unsigned char )c );
}

Here is a demonstrative program.

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

int main()
{
    std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

    for ( const auto &s : word ) std::cout << s << ' ';
    std::cout << '\n';

    for ( auto &s : word )
    {
        for ( char &c : s ) c = std::tolower( ( unsigned char )c );
    }

    for ( const auto &s : word ) std::cout << s << ' ';
    std::cout << '\n';

    for ( auto &c : word[0] ) c = std::toupper( ( unsigned char )c );

    for ( const auto &s : word ) std::cout << s << ' ';
    std::cout << '\n';
}

Its output is

VERY MERRY CHRISTMAS EVERYONE 
very merry christmas everyone 
VERY merry christmas everyone 

Or you can use a user-defined function like this.

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

std::string & change_string_case( std::string &s, bool upper_case = true )
{
    for ( auto &c : s ) c = upper_case ? std::toupper( static_cast<unsigned char>( c ) )
                                       : std::tolower( static_cast<unsigned char>( c ) );

    return s;
}

int main()
{
    std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

    for ( auto &s : word ) std::cout << change_string_case( s, false ) << ' ';
    std::cout << '\n';
}

The program output is

very merry christmas everyone 

Or you can write two separate functions as for example

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

std::string & lowercase_string( std::string &s )
{
    for ( auto &c : s ) c = std::tolower( static_cast<unsigned char>( c ) );

    return s;
}

std::string & uppercase_string( std::string &s )
{
    for ( auto &c : s ) c = std::toupper( static_cast<unsigned char>( c ) );

    return s;
}

int main()
{
    std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

    for ( auto &s : word ) std::cout << lowercase_string( s ) << ' ';
    std::cout << '\n';
    for ( auto &s : word ) std::cout << uppercase_string( s ) << ' ';
    std::cout << '\n';
}

The program output is

very merry christmas everyone 
VERY MERRY CHRISTMAS EVERYONE 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

There are several ways to do this task. You can do it by transform from algorithm headerfile.

#include <algorithm>
void  toUpperCase(std::string& str)
{
     std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}

int main()
{
    std::string str = "hello";
    toUpperCase(&str);
    std::cout << str << std::endl;    // "HELLO WORLD"
}

Another way would be using boost provided by boost/algorithm/string.hpp.

#include <boost/algorithm/string.hpp>
#include <string>

int main() {
    std::string str = "Hello World";

    boost::to_upper(str);   
    std::cout << str << std::endl;    // "HELLO WORLD"

    return 0;
}

You can learn more detail from here.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Faruk Hossain
  • 1,205
  • 5
  • 12