-6

I'm working on a side assignment for fun and want to capitalize the output from whatever the user entered using ::toupper but I'm struggling. This is what I have so far;

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

using namespace std;


int const ROWS = 4;
int const COLUMNS = 3;

void userInputValues(string myArray[ROWS][COLUMNS]);
void printValues(const string myArray[ROWS][COLUMNS]);


int main()
{
  string myArray[ROWS][COLUMNS];

  userInputValues(myArray);
  printValues(myArray);
  return 0;

} 

void userInputValues(string myArray[ROWS][COLUMNS])
{
  int index = 0;

  cout << "Enter 12 names" << endl;

  for(int row = 0; row < ROWS; ++row)
  {
    for(int column = 0; column < COLUMNS; ++column)
    {
       cout << ++index << ". Enter a name: ";
       getline(cin, myArray[row][column]);
    }

  } 

  cout << endl;


} 
void printValues(const string myArray[ROWS][COLUMNS])
{

    for (int row = 0; row < ROWS; ++row)
    {
        for (int column = 0; column < COLUMNS; ++column)
        {
        
         cout << setw(3) << ::toupper (myArray[row][column]) << " ";
        }

    
        cout << endl;

    
    } 

} 

When I run the code I get, "error: cannot convert ‘const string {aka const std::basic_string}’ to ‘int’ for argument ‘1’ to ‘int toupper(int)’" Which I sort of get because I can't pass a string as an int. But idk where to go from there. Also, I'm not too familiar with toupper since I never learned about so any help is appreciated.

  • [toupper](https://en.cppreference.com/w/cpp/string/byte/toupper) converts an individual character, not an entire string. Also why are you prefixing it with `::`? – Nathan Pierson Apr 09 '21 at 22:27
  • 1
    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) Please try searching next time. This was the first result. – ChrisMM Apr 09 '21 at 22:32
  • Tip: Get rid of all this C array stuff and use `std::vector` instead. Avoid `using namespace std`. That prefix exists for a reason. What you want is a 1D vector. This 2D stuff isn't even sued properly. A vector of length N*M is almost always better than N vectors of length M. – tadman Apr 09 '21 at 22:55

1 Answers1

3

std::toupper works per character, so you need to make a function that takes in a string and returns the capitalized version of it. You can do this using a traditional for loop to iterate through every character in the input string and toupper it, or you can use std::transform and pass in the input string iterators + the transformation to be applied (in this case a lambda) to do the same. You can read up on std::transform at https://en.cppreference.com/w/cpp/algorithm/transform.

#include <algorithm>

std::string StringToUpper(const std::string & iString) 
{
    std::string oString = iString;
    std::transform(oString.begin(), oString.end(), oString.begin(), 
        [](unsigned char c) { return std::toupper(c); }
    );
    return oString;
}
newbane2
  • 130
  • 5
  • Addendum: If you get weird behaviour using `toupper`, [read the Notes here](https://en.cppreference.com/w/cpp/string/byte/toupper#Notes). You don't trip over this very often, not in English at any rate, but when it happens it's nice to know ahead of time. Being a paranoid, I follow the recommendation all the time just so I don't have to worry about finding one of the rare cases where it matters. – user4581301 Apr 09 '21 at 22:49