how to convert char to integer???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 4
Reputation: Blitzer is an unknown quantity at this point 
Solved Threads: 0
Blitzer Blitzer is offline Offline
Newbie Poster

how to convert char to integer???

 
0
  #1
Jul 25th, 2006
I have '1' and I want to convert to integer 1
I think I must use atoi but I don't know how to use this function..

int i1 = atoi(item[i]);
// item[i] is char

then it show ERROR in member fuction

how to use atoi????
how can I declare it??
Last edited by Blitzer; Jul 25th, 2006 at 2:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 155
Reputation: mostafadotnet is on a distinguished road 
Solved Threads: 10
mostafadotnet's Avatar
mostafadotnet mostafadotnet is offline Offline
Junior Poster

Re: how to convert char to integer???

 
0
  #2
Jul 25th, 2006
Hi Blitzer,
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char a_char[10];
  9. cin >> a_char;
  10. cout << "As an integer: " << atoi(a_char);
  11. }
I've compiled this with Dev-C++ and it worked correctly.

Good luck.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how to convert char to integer???

 
0
  #3
Jul 25th, 2006
Well you can create a short string with
  1. char s[2];
  2. s[0] = ch;
  3. s[1] = '\0';
Then pass that to atoi, though strtol etc are better long term bets.

A quick answer is to do
  1. intVal = ch - '0';
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: Blitzer is an unknown quantity at this point 
Solved Threads: 0
Blitzer Blitzer is offline Offline
Newbie Poster

Re: how to convert char to integer???

 
0
  #4
Jul 25th, 2006
I have some ploblem

I want to make calculator for postfix number
example input : 45+
answer is 9

and I use

  1. char* number = new char[20];
  2. cin>>number;
  3. c.postfix(number); // use fuction postfix to convert

and this is my postfix fuction

  1. void Calculator:postfix(char* item){ // I don't know this is right??
  2. int i1;
  3. for(int i=0;i<20;i++){
  4. if(item[i]='+'){
  5. i1 = atoi(item[i-1]); //<<< this is error T_T
  6. item[i]=item[i-2]+item[i-1];
  7. top=top-2;
  8. i=i-2;
  9. stack[top] = item[i];
  10. }
  11. else{
  12. stack[top] = item[i];
  13. top++;
  14. }
  15. }
  16. }

I think main problem is "item" I have 45+ and I want to change it to 4 and 5 and then I use atoi to convert to interger
but I don't know how to change
help me please
Last edited by Dave Sinkula; Jul 25th, 2006 at 8:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: how to convert char to integer???

 
0
  #5
Jul 25th, 2006
Please post the code using [code] tags.

char* number = new char[20];
cin>>number;
c.postfix(number); // use fuction postfix to convert
 
and this is my postfix fuction
 
void Calculator : Postfix(char* item)  
{
 // give variables meaningful names
// instead of "item" , "expression" would have been a better choice
// the same applies to i1
  int i1; 
  for(int i=0;i<20;i++)
  {
    if(item[i]='+')    // this should be if (item[i] == '+')
   {
      i1 = atoi(item[i-1]);  // if using C++ y use old C functions
      // also i think atoi works with only null terminated strings

      item[i]=item[i-2]+item[i-1];
      top=top-2;
      i=i-2;
      stack[top] = item[i];
    }
    else 
   {
      stack[top] = item[i];
      top++;
    }
 }
}
Hope it helped,
Bye.
Last edited by ~s.o.s~; Jul 25th, 2006 at 3:37 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 16667 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC