Problem with cctype library

ok, i am doing a program where it converts char to upper or lower. I think i have it coded right but when i enter it it outputs a number. What is wrong with my code?
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
//This is program #1 it calls to switch around the capitalization from lower case to upper case and vice versa
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

int main()
{
 
  char inputs; //hold 10000 charactors in one simple array for this project to work.
  
  cout << "enter a word: ";
  cin >> inputs;
  
  if (isupper(inputs)){
    
    cout << toupper(inputs);}
    
    else if (islower(inputs)){
      
      cout << tolower(inputs);}
      
    
    
  
  
  
}


output
1
2
3
john@john-K53E:~/Programming/C++/Book Programming Exercises/Chapter 6 Branching Statements And Logical Operators$ ./atout
enter a word: y
121john@john-K53E:
Line 10 does represent a character array -- it is a single character.

10
11
  char inputs[ 100000 ];  // this is an actual character array

To convert to uppercase or lowercase, use the STL transform() algorithm:

1
2
#include <algorithm>
#include <cctype> 
 
  transform( inputs, inputs + 100000, inputs, (int(*)(int))std::toupper );

The cryptic cast at the end there is necessary, alas, in order to choose the correct overloaded toupper() function (or tolower(), etc).

Hope this helps.
It's because a character is an int, it has a set numerical value. To display a character, you have two options:
1) Leave your code how it is and just cast the output to a char:
cout << static_cast<char>(toupper(inputs));
2) Set inputs equal to the return value of the function and then display inputs. This does modify the value of inputs:
1
2
if (isupper(inputs))
   inputs = isupper(inputs);
Then you just cout inputs.

Edit: Ugh Ninja'd!!!
Last edited on
Topic archived. No new replies allowed.