Cin input

closed account (ENh0ko23)
I know i ask alot of questions but as you can see I REALLY am a beginner. Let me just put straight out what it is im doing and what exactly i need help with.

Basically, I have to input two random initials, and then i have to see if the initial is uppercase or lowercase and what place number it has in the alphabet.

example:
If i enter y and Z:
Your first initial is lowercase and is the 25th letter in the alphabet.
Your second initial is uppercase and is the 26th letter in the alphabet.

Now i already know how to say if the letter is uppercase or lowercase. (thats been figured out already)
What i am really confused on is how in the world the st, nd, rd, and th would be incorporated into these positions.
If it was just two SET initials, I would have no trouble at all. But the reason i have trouble is because each letter is random.


also thank you in advance.
What i have so far( The second initial is commented out for now until i can even figure out what to do)
Also: (I have alot of cin.gets because with the program i use, if i dont input that. The compiled code will just shut off)

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
#include <iostream>
using namespace std;
int main()

{
	char a;
	//char b;

	cout << "Please enter your initials." << endl;
	cin >> a;
	//cin >> b;
	cin.get();


	if (a >= 'a' && a <= 'z')
	{
		cout << "Your first initial is in lowercase." << endl;
	}
	else if (a >= 'A' && a <= 'Z')
	{
		cout << "Your first initial is in uppercase." << endl;
		cin.get();
	}






		cin.get();
	return 0;

}
Last edited on
I'll give you a hint

Go here
https://en.wikipedia.org/wiki/ASCII

Scroll down to Printable characters.

You can see A is DEC 65 and a is DEC 97.

You can find the DEC for Z and subtract 64 to find it's position in the alphabet.


closed account (E0p9LyTq)
Half of your question can be answered if you look at the C library header <cctype>. islower() or isupper().

http://www.cplusplus.com/reference/cctype/

You are using C++, you should use the C++ header version of the library <cctype>, not the C version <ctype.h>.
the st, nd, rd, and th would be incorporated into these positions

Sorry, are you just asking how to add st, nd, rd, or th after the number?
If that's what you are asking, hope this code can give you some hints:
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
// Basically, I have to input two random initials, and then i have to see 
// if the initial is uppercase or lowercase and what place number it has 
// in the alphabet.
// example:
// If i enter y and Z:
// Your first initial is lowercase and is the 25th letter in the alphabet.
// Your second initial is uppercase and is the 26th letter in the alphabet.

// Now i already know how to say if the letter is uppercase or lowercase. 
// (thats been figured out already)
// What i am really confused on is how in the world the st, nd, rd, and th 
// would be incorporated into these positions.
// If it was just two SET initials, I would have no trouble at all. 
// But the reason i have trouble is because each letter is random.
#include <iostream>

using namespace std;

int main()
{
    char a;

    cout << "Please enter your initials: ";
    cin >> a;

    if (a >= 'a' && a <= 'z')
    {
        cout << "Your first initial is in lowercase ";
    }
    else if (a >= 'A' && a <= 'Z')
    {
        cout << "Your first initial is in uppercase ";
        cin.get();
    }

    // let's say you have calcuated the position and you have stored
    // the result into an int variable. E.g.:
    // int position <-- contains the current position
    // Now you can print the first part of the result on screen:
    cout << "and is the " << position;
    // Then, to add st, nd, rd or th:
    // If you don't know switch:
    // if position is equal to 1 or position is equal to 11 or position is equal to 21
        cout << "st ";
    // else if position is equal to 2 or position is equal to 12 or position is equal to 22
        cout << "nd ";
    // ...other two istructions very similare to the ones above, then:
    cout << "letter in the alphabet.\n";

    // Otherwise, if you know switch:
    switch(position)
    {
        case  1:
        case 11:
        case 21:
            cout << "st ";
            break;
        case  2:
        case 12:
        case 22:
            cout << "nd ";
            break;
        case  3:
        case 13:
        case 23:
            cout << "rd ";
            break;
        case default:
            cout << "th ";
            break;
    }
    cout << "letter in the alphabet.\n";

    cin.get();
    return 0;
}

Topic archived. No new replies allowed.