944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 65459
  • C++ RSS
May 11th, 2007
0

how to convert char to int

Expand Post »
hi there, i just want to know what function to use in converting a char into an int
like:

C++ Syntax (Toggle Plain Text)
  1. char temp;
  2. cout << "enter something";
  3. cin >> temp;
  4.  
  5. atoi(temp);

is this the right way?
Similar Threads
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
May 11th, 2007
0

Re: how to convert char to int

>>is this the right way?
No because atoi() takes a pointer to a null-terminated string and your variable temp is just a single character.

The quickest way is to simply subtract the ascii value of '0' from it like this: int n = temp - '0';. Why does that work? Its because a char is a small integer and if you look at an ascii chart you will see that all characters have an ascii numerical value. Internally, programs know nothing at all about characters, only numeric values as shown in that ascii chart.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
May 11th, 2007
0

Re: how to convert char to int

thank you for that detailed explanation..
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
May 11th, 2007
0

Re: how to convert char to int

Why atoi is bad...

strtol is what you should be looking for.
Last edited by ~s.o.s~; May 11th, 2007 at 2:19 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 25th, 2008
-1

Re: how to convert char to int

Click to Expand / Collapse  Quote originally posted by jaepi ...
hi there, i just want to know what function to use in converting a char into an int
like:

C++ Syntax (Toggle Plain Text)
  1. char temp;
  2. cout << "enter something";
  3. cin >> temp;
  4.  
  5. atoi(temp);

is this the right way?
yes that is a correct way u can proceed.........
but the only thing u need to do is that u should use char array in place of char variable, and the function atoi is return an int value so u should store the result in some integer variable.
like
C++ Syntax (Toggle Plain Text)
  1. char temp[10]="10";
  2. int i;
  3. i=atoi(temp)*2;
  4. cout<<i; \\ the output = 20;
Reputation Points: 9
Solved Threads: 1
Newbie Poster
rajenpandit is offline Offline
13 posts
since Aug 2008
Nov 25th, 2008
0

Re: how to convert char to int

> yes that is a correct way u can proceed.........
Over a year late, and still you missed the better answer from ~s.o.s~
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 25th, 2008
0

Re: how to convert char to int

It seems this zombi thread was started by incorrect and unclear question then proceeded with answers to another hypothetical questions and now start the next bla bla bla loop...

That's my deposit in the discussion of char to int promotion...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 7th, 2011
-1
Re: how to convert char to int
Or you can download this header and include it, and to use the functions, do this:
C++ Syntax (Toggle Plain Text)
  1. int i;
  2. string s;
  3. cin>>s;
  4. i=c2a<int>(s)
Last edited by Mahkoe; Aug 7th, 2011 at 6:56 pm.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
Mahkoe is offline Offline
9 posts
since Nov 2010
Aug 8th, 2011
0
Re: how to convert char to int
Quote originally posted by Salem; ...
> yes that is a correct way u can proceed.........
Over a year late, and still you missed the better answer from ~s.o.s~
Let's add another 3 years to that.
Last edited by WaltP; Aug 8th, 2011 at 12:35 am.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Polynomial Class Assertion Failed
Next Thread in C++ Forum Timeline: Unable to execute code and pass parameters in NETBEANS or ECLIPSE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC