Enter a string and use a loop to find the ASCII Number of its characte

Enter a string and use a loop to find the ASCII Number of its characte

Page 1 of 1

4 Replies - 2045 Views - Last Post: 06 February 2007 - 07:00 PM Rate Topic: -----

#1 ctcoyote16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 07

Enter a string and use a loop to find the ASCII Number of its characte

Posted 05 February 2007 - 10:36 AM

Hello guys. I am in a C++ class and I'm having trouble with this assignment. The assignment is to Enter a string and use a loop to find the ASCII Number of its characters.

We've been dealing with "For Loops" so basically this code needs to contain at least one.

Heres all I can think to figure out:


cout<<"Enter a word: ";
cin>>word;

for(int i=0; i>word.length; i++)

word.find()





Yeah, I dont really understand this and Im not sure what exactly needs to be in it but Im pretty sure Ill need the substr, length, and find functions.

Any help is greatly appreciated. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Enter a string and use a loop to find the ASCII Number of its characte

#2 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Enter a string and use a loop to find the ASCII Number of its characte

Posted 05 February 2007 - 11:17 AM

Quote

The assignment is to Enter a string and use a loop to find the ASCII Number of its characters.

I dont really know what your wanting... something you need to fix is to use the length function you need to add () ex word.length() as for getting the ASC-II value one of the ways to do it is a static cast

static_cast<int>(word[i])
Was This Post Helpful? 0
  • +
  • -

#3 Ryan747  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 16-October 06

Re: Enter a string and use a loop to find the ASCII Number of its characte

Posted 05 February 2007 - 06:39 PM

cout<<"Enter a word: ";
cin>>word;

for(int i=0; i<word.length(); i++){
   cout << int(word[i]);
}



i think this is what you meant....
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Enter a string and use a loop to find the ASCII Number of its characte

Posted 06 February 2007 - 09:25 AM

Yes, Ryan747 is right.
Internally, everything is a number. There's no such thing as a character. Characters are stored as integers in memory where each integer value corresponds to a character according to ASCII.

So typecasting a character into an integer does the trick.
Was This Post Helpful? 0
  • +
  • -

#5 ctcoyote16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 07

Re: Enter a string and use a loop to find the ASCII Number of its characte

Posted 06 February 2007 - 07:00 PM

alright thanks guys. I got to finish this code up tonight, its for homework. hope to learn all this stuff eventually, lol :crazy:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1