RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 8305 | Replies: 4
Reply
Join Date: Jul 2006
Posts: 4
Reputation: Blitzer is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Blitzer Blitzer is offline Offline
Newbie Poster

how to convert char to integer???

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 138
Reputation: mostafadotnet is on a distinguished road 
Rep Power: 3
Solved Threads: 8
mostafadotnet's Avatar
mostafadotnet mostafadotnet is offline Offline
Junior Poster

Re: how to convert char to integer???

  #2  
Jul 25th, 2006
Hi Blitzer,
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
  char a_char[10];
  cin >> a_char;
  cout << "As an integer: " << atoi(a_char);
}
I've compiled this with Dev-C++ and it worked correctly.

Good luck.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,923
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 452
Colleague
Salem's Avatar
Salem Salem is offline Offline
Senior Poster

Re: how to convert char to integer???

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

A quick answer is to do
intVal = ch - '0';
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jul 2006
Posts: 4
Reputation: Blitzer is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Blitzer Blitzer is offline Offline
Newbie Poster

Re: how to convert char to integer???

  #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

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){ // I don't know this is right??
int i1;
for(int i=0;i<20;i++){
if(item[i]='+'){
i1 = atoi(item[i-1]); //<<< this is error T_T
item[i]=item[i-2]+item[i-1];
top=top-2;
i=i-2;
stack[top] = item[i];
}
else{
stack[top] = item[i];
top++;
}
}
}

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  
Join Date: Jun 2006
Location: India
Posts: 7,054
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 372
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: how to convert char to integer???

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 7:13 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC