Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Apologies for the cryptic questions. What I'm referring to is that if you use toupper() you end up with 'a' becoming 'A' How would you make a symbol such as ']' become '}' when incrementing a character within a code.

Here is my current code for reference:
// This file contains the function main, which lists lines of characters
// in the format 'UppercaseChar' --> 'lowercaseChar' 
// starting with 'A', incrementing
// by one until the number of lines desired by the inputter is reached.

#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;

int main()
{
    int inputvalue;
    int incrementalvar = 0;
    int letterlow = 'a';
    int letterhigh = 'A';
    //promtps for user input
    cout << "Please enter any positive decimal integer value.!\n";
    cin >> inputvalue;
    //loop that prints characters a through the character whose value is
    //equal to input value in the desired format.
    while (incrementalvar < inputvalue)
    {
        cout << "'" << (char)(letterhigh) << "' --> '" 
             << (char)letterlow << "'\n";
        incrementalvar++;
        letterlow++;
        letterhigh++;
    }
    return EXIT_SUCCESS;
}


What I have tried:

I've tried incrementing both a character set to 'A' originally and using toupper() on a character originally set to 'a.' Both have resulted, when a value such as 30 is entered into the code, in simply printing the symbol twice rather than it's equivalent when holding shift.
Posted
Updated 23-Jul-20 6:00am

I can only think of two ways to handle cases where isalpha() ie for ['a'..'z'] is false .. a customised 'toupper' where one uses

1) a dictionary/hash table of 'char' -> 'toupper' equivalent, ie '[' -> '{' OR,

2) a dictionary/hash table of 'char' -> integer offset to equivalent ... '[' -> '{' AND ']' -> '}' both have offset 32

.. it will be interesting to see other suggestions
 
Share this answer
 
v2
Comments
Ryan Thomsen 21-Jul-20 23:42pm    
This is the second week of a class in intro to C++, i would imagine there's meant to be an easier way to do this. Is there a way to do this without a to_upper being used at all?
Garth J Lancaster 22-Jul-20 0:31am    
then, be aware that the behaviour of toupper is defined as "If the argument passed is other than a lowercase alphabet, it returns the same character passed to the function" .. in reality, Ive never seen the need to 'uppercase' or shift '[' to '{' for example, so maybe anything I've suggested is too advanced for your class and your teacher or lecturer or ?? only means for you to use the standard toupper()
Ryan Thomsen 22-Jul-20 0:49am    
He has a basic assignment checker in place and it flags my output as incorrect when entering a value of 30 as its expected value is '{' while it is getting '['. However, it is possible he did not mean for us to use toupper at all if there is possibly a simple function to increment using an ascii conversion or something similar.
Garth J Lancaster 22-Jul-20 0:59am    
hmmm, not sure what he's looking for, maybe for you to write your own 'toupper()' .. assuming ASCII, if you have
char ch;
cin>>ch;
ch=ch-32;
you effectively have an 'uppercase' version of ch
Non-alphabetic characters do not have direct mappings in their character codes, so you will need to create your own translate table. The simplest way would be to use the 'lower' case character as an index into a table which contains the 'upper' case version, and vice versa. If you look at the Windows Character Map application you can see the actual numerical values for all the characters. The only reason you have two such characters on the keycaps of the keyboard is to reduce the number of keys required, but there is no direct relationship between the two symbols. And different keyboards have different mappings (qwerty vs azerty etc.).
 
Share this answer
 
This conversion depends on the specific keyboard layout, so you need to query the raw key code, and apply the desired qualifiers (shift in this case) to translate your raw code into an actual character.

For the first part, getting the raw keycode, I suggest looking at the sample code here: Using Raw Input - Win32 apps | Microsoft Docs[^]

For the second part, I think MapVirtualKeyA function (winuser.h) - Win32 apps | Microsoft Docs[^] could do the trick.

Note that there are two virtual key codes for shift: VK_LSHIFT and VK_RSHIFT. However, I'm not aware of any keyboard layout that actually makes use of that. Using either should be fine.
 
Share this answer
 
If you test for a char being alpha then you can & the char with 11011111 (xCF). That's the difference between UC and LC in the ASCII char set. This has no effect on UC characters.

The retro process, UC->LC can be done by | with 00100000 (x20). That happens to also be the space character.

Toggling between them (UC <-> LC) is ^ with x20

Beware that you only apply this to appropriate characters lest you accidentally create strange results and even ctrl chars.
 
Share this answer
 
v2
Comments
Stefan_Lang 24-Jul-20 4:22am    
He specifically asked about non-alphanumerical characters such as '['. x20 won't help there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900