13 Replies - 55327 Views - Last Post: 04 May 2006 - 07:19 PM Rate Topic: -----

#1 Lydia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

C++ how to convert ascii to intergers

Post icon  Posted 03 May 2006 - 02:53 PM

I need to write the function charToInt and it should return as an interger twice as big as the ASCII its self (as my output)

what would my main funcion look like?How do I convert char to int? :rolleyes:

This is what I have to work with but its not working? :crazy:

#include <iostream>
using namespace std;

int Main () {
char c;
int j;
cout << "Type in any character: ";
cin >> c;
j = c*2
cout << "Changed to " << j << endl;

return 0;
}

This post has been edited by Dark_Nexus: 04 May 2006 - 01:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ how to convert ascii to intergers

#2 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 03:19 PM

#include <iostream>
using namespace std;

int main()
{
for(int i = 0; i < 255; i++)
{
cout << i << " = " << static_cast<char>(i) << endl;
}
return 0;
}


basicly what you do is cast the int to a char or the char to int

static_cast<NEWTYPE>(variable); //is one way
(TYPE)variable; //2nd way
TYPE(variable); //3rd way

This post has been edited by Mrafcho001: 03 May 2006 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 Lydia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 03:31 PM

I dont get it :huh:

I am a beginner in this C++ as I have only been doing it for like 2 months in the evenings after work so you will need to explain if thats ok? :(

All I need to do is type in an ASCII code in eg *
My out put will be the ASCII code for * Which so happens to be 42 But my out put must be 84


I want to take * Which happens to be 42 times it with 2 to get 84

How do I get that right? :rolleyes:

This post has been edited by Lydia: 03 May 2006 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 03:34 PM

int integer = 2 * static_cast<int>(charVariable);

integer will now contain what you need :)
Was This Post Helpful? 0
  • +
  • -

#5 Lydia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 04:00 PM

ok so If I follow what you are saying I should have my programm of follows

#include <iostream>
using namespace std;

int Main () {
char character;
int interger;
cout << "Type in any character: ";
cin >> character; 


int integer = 2 * character
cout << "Changed to " << interger << endl;

return 0;
} 


But its still not working as its now complaining about no match for `int * std::ostream&' operator

Where is my mistake? Sorry I am a real numpty :stupid:

This post has been edited by Dark_Nexus: 04 May 2006 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 stuart   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-February 06

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 04:30 PM

Hi Lydia,

A couple of little things i noticed were that main needs to be lower case
e.g. int main() NOT int Main()

and you are missing a semi colon on the line
int integer = 2 * character; // yours does not end with a semicolon

Combine this with what Mrafcho001 has told you and you should be
on your way.
Was This Post Helpful? 0
  • +
  • -

#7 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 06:56 PM

again you are forgeting to cast the variable.

Think of the type of the variable as its shape and the data inside the variable as its volume. When you are casting a variable as something else you are changing the shape of it, but the volume stays the same.

If you do not cast the variable you are basicly telling the compiler to add a cube and a sphere (int and char) together. What you need to do is fool, if you will, the compiler into thinking the sphere is a cube.

You do that by the ways i showed you above

typeToCastTo(variableToCast);
(typeToCastTo)variableToCast;
static_cast<typeToCastTo>(variableToCast); //I suggest this one.

as stuart stated you need to change your Main() to main(), and cast the character to integer before multiply it by another integer.

here is the corrected code:

#include <iostream>
using namespace std;

int main () {
char character;
cout << "Type in any character: ";
cin >> character;


int integer = 2 * static_cast<int>(character);
cout << "Changed to " << integer << endl;

return 0;
}


EDIT:

Thanks to stuart i fixed the code above, I'm not at a compiler right now, so I cant really test the code.

This post has been edited by Mrafcho001: 03 May 2006 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 stuart   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-February 06

Re: C++ how to convert ascii to intergers

Posted 03 May 2006 - 07:11 PM

There is also an error in the use of variable names.

You are declaring a new variable integer in your calculation,
yet displaying the variable interger (notice the extra r) which
is declared at the top of the code. Its contents could be anything.

Also could you let us know the environment and compiler you are
using, might also help.

Let us know how you go.
Was This Post Helpful? 0
  • +
  • -

#9 frog   User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C++ how to convert ascii to intergers

Posted 04 May 2006 - 04:08 AM

i dont think you are getting what type casting is, i mean what it means .
or you would have by now got the problem from mrafcho001 and struats reply.if you have then its good but if you havent or would like some extra information i'll say you go to the link below.no need to understand the whole article now .just the introduction would do

http://www.cplusplus...ypecasting.html

i think this would help :)



edit:
ive found another link :
http://en.wikipedia....ki/Type_casting


maybe a simpler explanation would help if you are new to the language

This post has been edited by frog: 04 May 2006 - 04:57 AM

Was This Post Helpful? 0
  • +
  • -

#10 Lydia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: C++ how to convert ascii to intergers

Posted 04 May 2006 - 07:27 AM

:^: Stuart and Frog - I do see my program has some gramatical errors i did right it in a informal content sorry for that I did not assume you took every ; and capital so serriously. My appologies.

Frog - yes you are right I dont understand what type castings is - I am just a beginner and you all seem to be talking past my head.

Stuart - thank you for the added function
int integer = 2*static_cast<int>(character)
When I get home I will run the programm and see if it does indead double the characters value!

what does the static_cast<int>(character) acutaly mean? :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#11 frog   User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C++ how to convert ascii to intergers

Posted 04 May 2006 - 09:04 AM

i think you would get the idea about static_int from the link i posted

i would also like to tell you that i like the colour in your post :)
Was This Post Helpful? 0
  • +
  • -

#12 Lydia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: C++ how to convert ascii to intergers

Posted 04 May 2006 - 09:27 AM

Thanks frog - Yes I will have a look at the sight when I get home after work.

I am a real stress bag with this C++. I have only been exposed to it for like a month or so and its a subject towards a degree. I am a long distant learner and I have no one to help.

I need to be spoon fead the basics and then I am normaly fine to continue on my own and the appreciating the basic functions in C++ and making good use to there values is hard to wrap my head around.

I dont recall seen that int integer = 2*static_ectectectectect as an option in my study books but I will give it ago and definaly let you know how I get on.

Thank you for your help :P
Was This Post Helpful? 0
  • +
  • -

#13 Lydia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: C++ how to convert ascii to intergers

Posted 04 May 2006 - 11:17 AM

Mrafcho001 :D you so cleaver it worked. :wub:

Looking at it now its so simple :splat:
however
int integer = 2 * static_cast<int>(character);

i can see that you are saying take the integer and times it with 2

But I dont/ not familur with the rest of the line
can you please explain :blush:


Why not just have as I originaly did? And the int integer = ect really bothers me. I though all int where delcared under int main()?
Was This Post Helpful? 0
  • +
  • -

#14 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: C++ how to convert ascii to intergers

Posted 04 May 2006 - 07:19 PM

To answer your question, No.

You can declare any variables anywhere in a function (thats what main() is), the only thing you have to keep in mind is that you need to delcare it before you use it. Or you can use it on the declaration line, as I did.

Again I'll try to explain casting. What I did on that line static_cast<int>(character);, is i told the compiler to conver the character to a number, otherwise I'd be telling it multiply, for example, 2 * 'a'. Which does not make sence neither to a human nor a compiler. You need to multiply numbers (2 * 48, 2 * 50, etc..) and thats what static_cast<int> does, it converts the character to a number. So then the program can freely multiply them.

Typecasting tutorial:
http://www.cprogramm...l/lesson11.html

This post has been edited by Mrafcho001: 04 May 2006 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1