toupper

Instead of writing each function to respond to 't' and 'T' etc with several functions how do I use toupper(ch) to recognize lowercase input and convert it to the uppercase so the user is not restricted to just using uppercase?

1
2
3
4
5
6
7
  cout << "Enter item: ";
		cin >> woodType;
		if (woodType == 'T')
		{
			cout << "Total cost : $" << total << endl;
			break;
		}
What type is woodType? Are you just asking how to call toupper?
See examples at:
https://en.cppreference.com/w/cpp/string/byte/toupper
https://www.cplusplus.com/reference/cctype/toupper/

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>
using namespace std;

int main()
{
    double total = 42.0;
    
    cout << "Enter item: ";
    char woodType;   
    cin >> woodType;
    
    woodType = toupper(woodType);
    
    if (woodType == 'T')
    {
    	cout << "Total cost : $" << total << endl;
    }
}
Last edited on
That does exactly what I was looking for. Thank you. I read about the function beforehand I'm not too sure why I wasn't understanding it maybe not realizing that my ch directed to woodType and the examples just did the straight ch exchanges or changed a lowercase input and put in output as uppercase.
An alternate way to use toupper that doesn't alter the original input, and allows for string input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cctype>

int main()
{
   double total { 42.0 };

   std::cout << "Enter item: ";
   std::string woodType;
   std::cin >> woodType;

   if ('T' == ::toupper(woodType[0]))
   {
      std::cout << "Total cost : $" << total << '\n';
   }
}

Enter item: teak
Total cost : $42
Note that in C++ toupper() should be used like this:

 
static_cast<char>(std::toupper(static_cast<unsigned char>(ch));


See https://en.cppreference.com/w/cpp/string/byte/toupper
Topic archived. No new replies allowed.