From an old thread I came upon

Found a very messed up version of this on an old thread, and cleaned it up a bit. Actually got it to run but it's not doing what it's supposed to which is, for every letter you put in, a number value will come out.
Example:

Put in the first initial of your first, middle, and last name.

ABC

Your ID is

123

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
   char cLetter;
   char a= 1,b= 2,c= 3,d= 4,e= 5,f= 6,g= 7,h= 8,i= 9,j=10,
        k=11,l=12,m=13,n=14,o=15,p=16,q=17,r=18,s=19,t=20,
        u=21,v=22,w=23,x=24,y=25,z=26;
   int nNumber;


   cout <<"Please enter your first, middle, and last initials\n\n";
   cin  >> cLetter >> cLetter >> cLetter;

   cout <<"Your initial ID number is:\n\n";
   cout << nNumber << nNumber << nNumber << endl;

system("PAUSE");
return 0;
}


What am I doing wrong here guys?
1. Only one variable is being used for three different initials. So say if you entered abc you would only store 'c'. It should look something more like

1
2
3
4
5
6
7

char cletter1, cletter2, cletter3;

cin >> cletter1;
cin >> cletter2;
cin >>  cletter3;


2. I do not think you can do something like
 
char a = 1; 


since char is a data type of single characters. Unless it is different in C, you will nee some sort of logic to determine what letter corresponds to what number.

So something like:
1
2
3
4
5
6
7
8
9
10

char letter
switch (letter)
{
     case 'a':
          nNumber1 = 1;
          break;
//and so on, but you would need to do three separate ones or figure out a way to put it in a loop
}



3. Same issue with the numbers, should have three different numbers stored
1
2
3

int nNumber1, nNumber2, nNumber3;



Those are some of the things that I see wrong. Could be more, I could also be wrong. I am pretty new to coding as well.
Correct, except on point 2. A char can hold an integral value as it really just holds the ASCII code value of the character it represents anyway.

@TC: In any case, there is no reason to convert the initials to other numbers; just use the ASCII code value and save yourself the trouble.
Thanks for the responses guys!

Okay so @erock, I made changes based on your reply. @Zhuge, I tried doing the char conversion only to see that no matter what char I put in, the same random number pops up. Almost there though! What could be the problem now?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    char cInit1,cInit2,cInit3;
    
    cout << "Please enter your first, middle, and last initials:\n\n";
    cin  >> cInit1 >> cInit2 >> cInit3;

    int nValue1 = (char)nValue1;
    int nValue2 = (char)nValue2;
    int nValue3 = (char)nValue3;
   
    cout << "Your ID is:\n\n" << nValue1
         << nValue2 << nValue3 << endl << endl;

    system("PAUSE");
    return 0;
}
Last edited on
Based on how you did it above, this is how I got it to work

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

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    char cInit1,cInit2,cInit3;

    cout << "Please enter your first, middle, and last initials:\n\n";
    cin  >> cInit1 >> cInit2 >> cInit3;

    int nValue1 = ((int)cInit1) - 96;
    int nValue2 = ((int)cInit2) - 96;
    int nValue3 = ((int)cInit3) - 96;

    cout << "Your ID is:\n\n" << nValue1
         << nValue2 << nValue3 << endl << endl;

    system("PAUSE");
    return 0;
}
Whoa awesome! Thanks! One question though, how do lines 15-17 really work? Why is there - 96?
No problem!


As we know, the cInit1 and so fourth are char values. I type cast those values to reveal the decimal value for the chars. For example, if the first one is 'a', the ASCII value(which can be found on an ASCII sheet, I just google imaged one) is 97. The last operation is just subtracting 96 to get the values you wanted.

For extra clarification:

We have a,b,c

I type cast a to an integer so now it = to 97, I then subtract 96 to get 1.

I type cast b to an integer so now it = to 98, I then subtract 96 to get 2.

I type cast c to an integer so now it = to 99, I then subtract 96 to get 3.

Again, 96 was just found through basic math.

97 - x = 1

which is 96. The magic number to get the numbers you wanted.


Hope this made sense.
That made a whole lotta sense! You are good! Thanks again, just started learning like a week ago and was really anxious to know how to get this to work.
You need neither the casts nor the magic number. As mentioned before, char is an integral type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
 
using namespace std;
 
int main(int nNumberofArgs, char* pszArgs[])
{
    char cInit1,cInit2,cInit3;
 
    cout << "Please enter your first, middle, and last initials:\n\n";
    cin  >> cInit1 >> cInit2 >> cInit3;
 
    int nValue1 = cInit1 - 'a' + 1;
    int nValue2 = cInit2 - 'a' + 1;
    int nValue3 = cInit3 - 'a' + 1;
 
    cout << "Your ID is:\n\n" << nValue1
         << nValue2 << nValue3 << endl << endl;
 
    system("PAUSE");
    return 0;
}
Nice! @cire what exactly does "cInt1 - 'a' + 1" mean? Does putting single quotes on a letter automatically change it to a numeric value?
'a' is a character literal. Any char (including a literal) has an integral value because char is an integral type.

Because the values for 'a' to 'z' are typically consecutive values (as long as you're not on a system using EBCDIC), you can use the short cut of subtracting 'a' from a char to get the values for characters a to z in the form 0 to 25, and adding 1, of course, gives you 1 to 26.
Ok thanks for explaining it. Crystal clear now, thanks everybody!
Topic archived. No new replies allowed.