How to print out array in all caps

I'm working on a project for fun and was trying to figure out how do I capitalize whatever the user inputted in all caps? For example I want it to print
NAME1 NAME2 NAME3 etc. I'm using an old homework layout that we had to do with integers and just switched it to string. But my question remains, how do I capitalize every letter that the user inputted? Here's what I have so far. I also found a new method called toupper but I don't completely get it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include<iostream>
#include<iomanip>

using namespace std;


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


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


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

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

} 


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

  cout << "Enter 12 numbers" << endl;

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

  } 

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

    cout << right << setw(20) << "Row Totals" << endl; 

    for (int row = 0; row < ROWS; row++)
    {
        int sum = 0;  

        for (int column = 0; column < COLUMNS; ++column)
        {
            cout << setw(3) << myArray[row][column]; 

            sum = sum + myArray[row][column]; 
        }

        cout << setw(6) << sum<<'\n';  
    } 

} 
Use std::toupper().

e.g. (Your printValues() function):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void printValues (const int myArray[ROWS][COLUMNS])
{
	cout << right << setw(20) << "Row Totals" << endl;

	for (int row = 0; row < ROWS; row++)
	{
		int sum = 0;

		for (int column = 0; column < COLUMNS; ++column)
		{
			// toupper (x) converts 'x' to uppercase.
			cout << setw(3) << std::toupper (myArray[row][column]);

			sum = sum + myArray[row][column];
		}

		cout << setw(6) << sum<<'\n';
	}
}


toupper() and tolower() are found in the <cctype> header file.

Note: if you want to convert to lowercase, use std::tolower().

Good luck!
max
toupper(ch) returns the passed character as uppercase if it's lower case - otherwise returns what's passed.

One way to convert a string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <iostream>

std::string toupper(const std::string& str)
{
	std::string low;
	low.reserve(str.size());

	for (const auto ch : str)
			low += static_cast<char>(std::toupper(static_cast<unsigned int>(ch)));	// Make lower case

	return low;
}

int main()
{
	std::string str;

	std::cout << "Enter a string: ";
	std::getline(std::cin, str);

	const auto ucstr {toupper(str)};

	std::cout << "As uppercase\n" << ucstr << '\n';
}



Enter a string: qwe
As uppercase
QWE


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cctype>
using namespace std;

int main()
{
   transform( istream_iterator<char>{ cin }, {}, ostream_iterator<char>{ cout, "" }, ::toupper ); 
}
I'm back and realized I copied my past homework code lol. This is my code to my side project however it isn't working when I try it. I get "error: cannot convert ‘const string {aka const std::basic_string}’ to ‘int’ for argument ‘1’ to ‘int toupper(int)’" at line 85. I get that I can't pass a string where an int is expected but I've never tried this before so any suggestions? Sorry for the mix up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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;

} // End of main function


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: ";
       cin >> myArray[row][column];
    }
    
  } // End of for-loop

  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;
    
        
    } 
    
} 


Where exactly is Line 85? I only see 63 lines in your posted code.

Well ok, on line 55, your toupper call is wrong. It should be
 
std::toupper

not just
 
::toupper

although if you're using namespace std, you don't actually need the std::

So correct that and try again.
So with std::toupper I get, "error: no matching function for call to ‘toupper(const string&)’"
and with ::toupper I get, "error: cannot convert ‘const string {aka const std::basic_string}’ to ‘int’ for argument ‘1’ to ‘int toupper(int)’"
and with just toupper I get, "error: no matching function for call to ‘toupper(const string&)’"

And yes it's the error was at line 55, sorry.
seeplus already showed you, you need some sort of helper function to convert the whole string to uppercase. The built-in library toupper (and tolower, isdigit, isalpha, etc.) expects a single character as its argument, so you need to loop through every character.

<I wrote the following before having seen seeplus's reply, so it's entirely redundant>

You could write a helper function to convert every character in a string to uppercase.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Example program
#include <iostream>
#include <string>
#include <cctype>

std::string toupper(std::string str_copy)
{
    for (char& ch : str_copy)
        ch = std::toupper(ch);

    return str_copy;
}

int main()
{
  std::string name;
  std::cout << "What is your name? ";
  getline (std::cin, name);
  
  std::cout << "HELLO, " << toupper(name) << "!\n";
}

What is your name? Edward
HELLO, EDWARD!
Last edited on
Registered users can post here. Sign in or register to post.