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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

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

Post 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.
I'm not modest, I'm brutally honest.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: An Introduction to Programming, For beginners

Post 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;
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: An Introduction to Programming, For beginners

Post by cplusplusnoob »

thank you so much. :bow:
I'm not modest, I'm brutally honest.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: An Introduction to Programming, For beginners

Post 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.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: An Introduction to Programming, For beginners

Post 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;
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: An Introduction to Programming, For beginners

Post 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;
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: An Introduction to Programming, For beginners

Post by Scoody »

I try to stay pure C++ and therefore, static_cast.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: An Introduction to Programming, For beginners

Post 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++. :)
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: An Introduction to Programming, For beginners

Post 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
"
I'm not modest, I'm brutally honest.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: An Introduction to Programming, For beginners

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: An Introduction to Programming, For beginners

Post by MarauderIIC »

Yeah, Scoody's the only person I've seen actually use C++ casts so far. :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post by MarauderIIC »

I split this from the Intro to Programming sticky.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

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

Post by cplusplusnoob »

dont think my last post made anyway i get it thank you so much falco was really right about the forums ;)
I'm not modest, I'm brutally honest.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Post Reply