Outputting a-z as numbers (a=0...)
Moderator: Coders of Rage
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Outputting a-z as numbers (a=0...)
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.
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.
Re: An Introduction to Programming, For beginners
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:
For example:
Code: Select all
for(int i = 0; i <= 25; i++)
cout << static_cast<char>(i+97) << endl;
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Re: An Introduction to Programming, For beginners
thank you so much.
I'm not modest, I'm brutally honest.
- wtetzner
- 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
This is usually considered better practice, as it makes your code more readable.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;
Code: Select all
for(int i = 0; i < 26; i++)
cout << static_cast<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.
- Ginto8
- 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
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):wtetzner wrote:This is usually considered better practice, as it makes your code more readable.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;
That way, just by looking at the code you can tell it's looping through the lower case letters of the alphabet.Code: Select all
for(int i = 0; i < 26; i++) cout << static_cast<char>('a' + i) << endl;
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.
- wtetzner
- 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
Yeah. You actually don't even need the (int) cast.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;
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.
Re: An Introduction to Programming, For beginners
I try to stay pure C++ and therefore, static_cast.
- wtetzner
- 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
Yeah, that's fine. Although C++ is a superset of C, so technically any C code is still "pure" C++.Scoody wrote:I try to stay pure C++ and therefore, static_cast.
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.
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Re: An Introduction to Programming, For beginners
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.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;
"
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.
Re: An Introduction to Programming, For beginners
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.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
"
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;
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: An Introduction to Programming, For beginners
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Outputting a-z as numbers (a=0...)
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.
- cplusplusnoob
- 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...)
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.
- programmerinprogress
- 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...)
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
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
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 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
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