How to negate case sensitivity.

Ok, really suck at this so far, but I've managed to code enough to get a working program. The question is for a college programming class, the question wants me to write a program that allows the user to input a character and the program returns the military phonetic alphabet equivalent. Ex: A = Alpha B = Bravo

Only problem is if I type in a lower case A, the program doesn't respond. Teacher wants us to use toupper to avoid case sensitivity, which I have read and read and can't for the life of me figure it out. This is what I have so far. Thank you in advance. He also recommended isalpha, but I don't understand how that applies to this project.

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
 #include <iostream>
#include <ctype.h>
#include <string>

using namespace std;


int main ()
{

 string UserInput; //Declaration of letter.

cout << "Please enter letter to receive ICAO translation: ";

	cin >> UserInput;

	if (UserInput == "A")
	if (UserInput =="a")

    cout << endl;
    cout << "The letter A in ICAO translates to Alpha." << endl;



return 0;

}
Last edited on
Take a look at the reference page for toupper: http://www.cplusplus.com/reference/cctype/toupper/

Note that it takes a character as an argument and returns a value. In order to use toupper(), you will want to call the function and assign the value somewhere.

1
2
char test = 'a';
test = toupper(test);

You could assign the uppercase character to a new variable if you want, or assign it back to the same variable.

Your code captures user input in a string rather than a character, but the principal is the same. Iterate through the string and convert each character via toupper.

It's also possible to use the transform() function to convert an entire string to uppercase, though that involves using the algorithm library and may be beyond the scope of your class at this point.

1
2
3
4
5
#include <string>
#include <algorithm>

string my_string = "cplusplus.com";
transform(my_string.begin(), my_string.end(), my_string.begin(), ::toupper);
Yep... Still speaking Greek. Anyone able to Barney style this? Beginners forum seems pretty advanced. Not sure how that char test takes a user's input and converts it to uppercase.
basically your toupper function converts any alphabet to its upper case

so if the user enters a lowercase 'a' it will be converted to uppercase

and if the user enters a uppercase 'A' it will be/still is a uppercase

so you don't need to worry about the case of the alphabet

PS: EtDecius's post wasn't that geeky, in-fact it was quite nice

PPS: if the user enters A will the uppercase function still work or will it skip the statement? just wondering....
Last edited on
Below is a simple code of the possible use of the toupper.

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
#include <iostream>
#include <cctype> //For toupper and tolower function.
using std::cin;
using std::cout;
using std::endl;
using std::toupper;


int main()
{
    char inputLetter{ ' ' };
    char tryAgain{ ' ' };
    bool keepLooping{ true };

    while (keepLooping)
    {
	   cout << "Input a letter: ";
	   cin >> inputLetter;

	   inputLetter = (toupper(inputLetter));
	   cout << "You inputted " << inputLetter << endl;

	   cout << "Try Again?(Y/N): ";
	   cin >> tryAgain;

	   if (toupper(tryAgain) == 'Y')
		  keepLooping = true;

	   else if (toupper(tryAgain) == 'N')
		  keepLooping = false;

	   else
	   {
		  cout << "Your input " << tryAgain << " is not valid.\n";
		  cout << "This program will assume that you want to continue.\n";
		  keepLooping = true;
	   }
    }
    
    cout << "Program will close...\n";

    return 0;

}
@Viking86
I know it is tough to make sense of all this at first. Just believe it will become much easier to grok as time goes on.

One of the best ways to figure a thing out is to write a small program to play with it. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cctype>
#include <ciso646>
#include <iostream>
using namespace std;

int main()
{
  cout << "Enter characters. Enter . to quit.\n";
  char c;
  while ((cin >> c) and (c != '.'))
    cout << c << " --> " << (char)toupper( c ) << "\n";  
}
Enter characters. Enter . to quit.
abc
a --> A
b --> B
c --> C
ABC
A --> A
B --> B
C --> C
Hello
H --> H
e --> E
l --> L
l --> L
o --> O
.

Hope this helps.
Last edited on
shadder wrote:
if the user enters A will the uppercase function still work or will it skip the statement?
what will the standard function do?
Last edited on
Ok, those post were pretty helpful. And I'm not saying he's speaking too advanced, I'm just saying that I'm THAT bad at this. I have a friend who does this for a living and she went super confusing on me, so I think I got an idea I'm going to play with and give it a shot. Thanks gents.
Last edited on
chicofeo, line 20 looks promising, giving it a go now.
Success! Now I have one more problem. User input is now case insensitive, however, now my if statements are just popping up every possible explanation. So if I type in the letter A, which should say that A translates to Alpha, it show that it translates to Alpha, and then it pops another message saying the letter A translates to Bravo. How do I fix this?


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
#include <iostream>
#include <ctype.h>
#include <string>

using namespace std;


int main ()
{

 char UserInput; //Declaration of letter.

cout << "Please enter uppercase letter to receive ICAO translation: ";

	cin >> UserInput;

	UserInput = (toupper(UserInput));

{
	if (UserInput == 'a')

    cout << endl;
    cout << endl;
    cout << "The letter "<< UserInput <<" in ICAO translates to Alpha." << endl;
}


    if (UserInput == 'b')

    cout << endl;
    cout << "The letter "<< UserInput <<" in ICAO translates to Bravo." << endl;



return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
{
	if (UserInput == 'a')

    cout << endl;
    cout << endl;
    cout << "The letter "<< UserInput <<" in ICAO translates to Alpha." << endl;
}


    if (UserInput == 'b')

    cout << endl;
    cout << "The letter "<< UserInput <<" in ICAO translates to Bravo." << endl;


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
	if (UserInput == 'a')
{
    cout << endl;
    cout << endl;
    cout << "The letter "<< UserInput <<" in ICAO translates to Alpha." << endl;
}}


    if (UserInput == 'b')
{
    cout << endl;
    cout << "The letter "<< UserInput <<" in ICAO translates to Bravo." << endl;
}
Thank you, it compiles fine without that second closed bracket, but the cout doesn't display.
UserInput = (toupper(UserInput));

Should be :
UserInput = (tolower(UserInput));
Woohoo! Thank you Sakura! Now could you tell me why that fixed it so I can never make that mistake again?

Edit: I see why it worked! Thank you so much!
Last edited on
Topic archived. No new replies allowed.