Page 1 of 1

Outputting a-z as numbers (a=0...)

Posted: Sun Mar 08, 2009 2:48 pm
by cplusplusnoob
I have looked in my programing book and on google and have found no solution to my problem.I am trying to create a program in c++ that the letters a-z using a loop a=0 and adding 1 each time so it should look like this
a 0
b 1
-------
z 25
when i try this i dont know if i get it right but i get a buch of rancom symbols like a heart or spade.please help.

Re: An Introduction to Programming, For beginners

Posted: Sun Mar 08, 2009 4:07 pm
by Scoody
As you can see from the ASCII-table, lowercase 'a' starts at Dec = 97. So your loop has to start at 97, not 0.
For example:

Code: Select all

for(int i = 0; i <= 25; i++)
  cout << static_cast<char>(i+97) << endl;

Re: An Introduction to Programming, For beginners

Posted: Sun Mar 08, 2009 7:00 pm
by cplusplusnoob
thank you so much. :bow:

Re: An Introduction to Programming, For beginners

Posted: Sun Mar 08, 2009 10:49 pm
by wtetzner
Scoody wrote:As you can see from the ASCII-table, lowercase 'a' starts at Dec = 97. So your loop has to start at 97, not 0.
For example:

Code: Select all

for(int i = 0; i <= 25; i++)
  cout << static_cast<char>(i+97) << endl;
This is usually considered better practice, as it makes your code more readable.

Code: Select all

for(int i = 0; i < 26; i++)
  cout << static_cast<char>('a' + i) << endl;
That way, just by looking at the code you can tell it's looping through the lower case letters of the alphabet.

Re: An Introduction to Programming, For beginners

Posted: Mon Mar 09, 2009 2:59 pm
by Ginto8
wtetzner wrote:
Scoody wrote:As you can see from the ASCII-table, lowercase 'a' starts at Dec = 97. So your loop has to start at 97, not 0.
For example:

Code: Select all

for(int i = 0; i <= 25; i++)
  cout << static_cast<char>(i+97) << endl;
This is usually considered better practice, as it makes your code more readable.

Code: Select all

for(int i = 0; i < 26; i++)
  cout << static_cast<char>('a' + i) << endl;
That way, just by looking at the code you can tell it's looping through the lower case letters of the alphabet.
No need to static_cast it methinks. I'm pretty sure you can just do this (made i++ into ++i, 'cuz I feel like it):

Code: Select all

for(int i = 0; i < 26; ++i)
    cout << (char)((int)'a' + i) << endl;

Re: An Introduction to Programming, For beginners

Posted: Mon Mar 09, 2009 3:03 pm
by wtetzner
Ginto8 wrote: No need to static_cast it methinks. I'm pretty sure you can just do this (made i++ into ++i, 'cuz I feel like it):

Code: Select all

for(int i = 0; i < 26; ++i)
    cout << (char)((int)'a' + i) << endl;
Yeah. You actually don't even need the (int) cast.

Code: Select all

for(int i = 0; i < 26; ++i)
    cout << (char)('a' + i) << endl;

Re: An Introduction to Programming, For beginners

Posted: Tue Mar 10, 2009 2:34 am
by Scoody
I try to stay pure C++ and therefore, static_cast.

Re: An Introduction to Programming, For beginners

Posted: Tue Mar 10, 2009 11:21 am
by wtetzner
Scoody wrote:I try to stay pure C++ and therefore, static_cast.
Yeah, that's fine. Although C++ is a superset of C, so technically any C code is still "pure" C++. :)

Re: An Introduction to Programming, For beginners

Posted: Thu Mar 12, 2009 7:09 am
by cplusplusnoob
Scoody wrote:As you can see from the ASCII-table, lowercase 'a' starts at Dec = 97. So your loop has to start at 97, not 0.
For example:

Code: Select all

for(int i = 0; i <= 25; i++)
  cout << static_cast<char>(i+97) << endl;
this does work but i am not sure this was the lesson that the book was trying to teach me i will give it to you just as the book gives it to me and again thank you for your time.
"
try this
the character 'b' is char('a'+1), 'c' is cahr(a+2), etc. Use a loop to write out a table of characters with thier corresponding integer values:
a 97
b 98
...
z 122
"

Re: An Introduction to Programming, For beginners

Posted: Thu Mar 12, 2009 7:35 am
by Scoody
cplusplusnoob wrote:this does work but i am not sure this was the lesson that the book was trying to teach me i will give it to you just as the book gives it to me and again thank you for your time.
"
try this
the character 'b' is char('a'+1), 'c' is cahr(a+2), etc. Use a loop to write out a table of characters with thier corresponding integer values:
a 97
b 98
...
z 122
"
It's almost the same, the difference is that your book is using "C-style casts", they give you more power, which can be good or bad.
Going along the lines of your book you could do it like this:

Code: Select all

  for(int i = 0; i <= 25; i++)
    cout << char('a' + i) << " " << 'a'+i << endl;
So when i=1 then 'a'+1 would be 98 and when casting it to a char you get 'b'. And since the last 'a'+1 isn't casted you get the corresponding ASCII number.

Re: An Introduction to Programming, For beginners

Posted: Thu Mar 12, 2009 3:23 pm
by MarauderIIC
Yeah, Scoody's the only person I've seen actually use C++ casts so far. :)

Re: Outputting a-z as numbers (a=0...)

Posted: Thu Mar 12, 2009 3:29 pm
by MarauderIIC
I split this from the Intro to Programming sticky.

Re: Outputting a-z as numbers (a=0...)

Posted: Thu Mar 12, 2009 4:32 pm
by cplusplusnoob
dont think my last post made anyway i get it thank you so much falco was really right about the forums ;)

Re: Outputting a-z as numbers (a=0...)

Posted: Thu Mar 12, 2009 4:54 pm
by programmerinprogress
I guess if you wanted to make the Ascii code of 'A' (or 'a') produce a number equal to one, you good do a bit of bit manipulation .

The Ascii value for 'A' is 65 (off the top of my head), which is 0100 0001 in binary.

So if you get 'A', store it as an integer, and then AND (Represented by a single apersand in C/C++ '&') it by 0011 1111 (or 3F in hex), you should be able to 'knock' the 64 off, to make the resultant value correspondant to the position of the letter in the Alphabet

you could write a program like this

Code: Select all

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char Letter;
    cin >> Letter; // get a character from keyboard
    int Value(0);
    Value = Letter; // put ASCII value into an integer

    if(isupper(Letter))//checks for uppercase
    {
        Value = Value & 0x3F;
        //AND the bitfield to change the value
    }
    else
    {
        Value = Value & 0x1F;
        //AND the bitfield to change the value
    }

    cout << Value; // show position in alphabet
    return 0;
}


I know the priorities of this post changed a little, due to the fact that the poster was probably more interested in getting the ascii value, but I thought I would demonstrate this anyway :D