How do I capitalize?

//Sam Vernaza CS 162 Lab #2
2 //Program that writes out and compiles employees initials
3
4 //The first section of this program is going to take initials and cap them.
5
6 #include <iostream>
7 #include <cctype>
8
9 using namespace std;
10
11 int main()
12 {
13 char initials[4];
14 char first;
15
16 cout << "Please enter your initials:" << endl;
17 first = toupper(initials[4]);
18 cin >> initials;
19 first = toupper(initials[4]);
20 if ((initials[0] >= 'A' && initials[0] <= 'Z') || (initials[0] >= 'a' && initials[0] <= 'z'))
21
22 {
23 cout << "You've entered: " << initials << endl; // capitalize
24 }
25 else
26 cout << "Enter a valid entery" << endl;
27
28
29 return 0;
30 }
closed account (j3Rz8vqX)
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
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main()
{
    char initials[4];

    cout << "Please enter your initials:" << endl;
    cin >> initials;
    for(unsigned int i=0;i<strlen(initials);++i)//From zero to the strings length
    {
        initials[i] = toupper(initials[i]);//<---Read the character, uppercase it, and store it at its location.
    }
    if ((initials[0] >= 'A' && initials[0] <= 'Z') || (initials[0] >= 'a' && initials[0] <= 'z'))
    {
        cout << "You've entered: " << initials << endl; // capitalize
    }
    else
    {
        cout << "Enter a valid entery" << endl;
    }
    return 0;
}

Use #include <cstring> to enable strlen().

Edit: By the way...
Use the code tag:
[code]//This is code[/code]

Or the source code format <>.
Last edited on
Thank you!

Will do!
Why did you use an unsigned int?

Also, why did that code require a for loop?

Can the to upper statement not capitalize everything within the initials array?
closed account (j3Rz8vqX)
The "toupper" function is expecting a single character:
int toupper ( int c );
http://www.cplusplus.com/reference/cctype/toupper/

And will return an integer; integers and characters are interchangeable:
'A' == 65;
'a' == 97;
http://www.asciitable.com/

Since you've initialized a c style string of size 4:
We can operate on its characters using indexes [0], [1], [2], [3], [4].

If you've entered "qt" for example, 'q' would be at index [0], and 't' would be at index [1].

Starting explanation of the for-loop:

Our use of the "for loop" is to iterate through the entire array of characters and attempt to initialize each character.

In the "for loop", we've declared an integer of "i" and assigned it a value of 0. We then compare it to our limit: the string length of our character array; the valid characters in the array. The last argument is applied after the embedded procedures; which is our increment counter.

In the loop, we would get the value in the array at index "i". Apply the value to the toupper function. And insert the returning value into our character array at index "i"; erasing its lowercase value!

Ending explanation of the for-loop.

As for the unsigned it: it simply ensures the integer variable is a positive number; regular int would work as well.
Thank you so much. That was a very thorough explanation.

I'm so bad at this programming thing and I'm wondering if I picked the wrong degree. :(

I'm so bad at this programming thing

No you aren't.Everyone's a beginner at some point.

I'm wondering if I picked the wrong degree. :(

No u picked the most interesting job of this world :)
Topic archived. No new replies allowed.