How to pass toupper to a string in a 2D array

Hello, I'm working on an assignment and I'm having trouble with it. The instructions are to just use 4 libraries and they are, <iomanip>, <iostream>, <string>, and <fstream>. He only wants us to use 3 functions and they are for user input, print, and saveToFile. We are to create a 2D array with 4 rows and 3 columns and then print the array in a spreadsheet format with all caps. The last time we did this I used <algorithm> and transform but this time we can't do that. We are to save the results to a file. Also he said we must use loops for every function. Here's what I have so far.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

void userInputValues(string myArray[4][3]);
void printValues(const string myArray[4][3]);
void saveToFile(string myArray[4][3]);

int main()
{
  string myArray[4][3];
  
  userInputValues(myArray);
  printValues(myArray);
  saveToFile(myArray);
  return 0;

} 

void userInputValues(string myArray[4][3])
{
  int index = 0;
  
  cout << "Enter 12 names" << endl;
  
  for(int row = 0; row < 4; ++row)
  {
    for(int column = 0; column < 3; ++column)
    {
       cout << ++index << ". Enter a name: ";
       getline(cin, myArray[row][column]);
    }
    
  } 

  cout << endl;


} 

void printValues(const string myArray[4][3])
{
  
    for (int row = 0; row < 4; ++row)
    {
        for (int column = 0; column < 3; ++column)
        {
            
        cout << setw(3) << myArray[row][column] << " ";
        
        }
        
        cout << endl;

    } 
    
} 

void saveToFile(string myArray[4][3])
{
    ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    for (int row = 0; row < 4; ++row)
    {
        for (int column = 0; column < 3; ++column)
        {
        myfile << myArray[row][column] << " " ;
        }
    
    }
    
    myfile.close();
    
  }
  
  else cout << "Unable to open file";
  
}


I tried this method in my printValues function with toupper but couldn't get it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 void printValues(string myArray[4][3])
{
   for(int row = 0; row < 4; ++row)
    {
        for (int column = 0; column < 3; column++)
        {

        myArray[row][column] = toupper(myArray[row][column]);
        
        cout << setw(3) << myArray[row][column] << " ";
        
        }

        cout << endl;
    }
  
}
Last edited on
Hello av16352,

First you need the header file "<cctype>" to use "toupper" or "tolower", but you can not use that file, so you will have to do something different.

Next problemwyould have been the "toupper" only works on a single character not a whole string.

Using the print function to work with and this is untested:
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
void printValues(string myArray[4][3])
{
	std::string upperCaseString;

	for (int row = 0; row < 4; ++row)
	{
		for (int column = 0; column < 3; column++)
		{
			upperCaseString = myArray[row][column];

			for (size_t idx = 0; idx < upperCaseString.size(); idx++)
			{
				if (upperCaseString[idx] >= 'a' && upperCaseString[idx] <= 'z')
				{
					upperCaseString[idx] -= 32;
				}
			}

			cout << setw(3) << upperCaseString << " ";

		}

		cout << '\n';
	}
}

It is a thought, but still needs to be tested.

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

using namespace std;

int main()
{

    string str {"A string"};

    cout << str << '\n';

    for(int i = 0; i < str.length(); i++)
    str[i] = toupper(str[i]);

    cout << str << '\n';


    return 0;
}



A string
A STRING
Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
void printValues(const string myArray[4][3])
{
	for (size_t row = 0; row < 4; ++row)
		for (size_t column = 0; column < 3; ++column) {
			auto upperCaseString {myArray[row][column]};

			for (auto& ch : upperCaseString)
				ch -= ('a' - 'A') * (ch >= 'a' && ch <= 'z');

			cout << setw(3) << upperCaseString << (column == 2 ? '\n' : ' ');
		}
}

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

//using namespace std; //<--

int main()
{

    std::string str {"A string"};

    std::cout << str << '\n';

    for(int i = 0; i < str.length(); i++)
    str[i] = toupper(str[i]);

    std::cout << str << '\n';


    return 0;
}
Thank you everybody! It's always nice seeing the amount of different ways a program can be written when it comes down to personal choice. I decided to go with Andy's suggestion. I now know and understand how to use toupper properly.
Topic archived. No new replies allowed.