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);
// item is char

then it show ERROR in member fuction

how to use atoi????
how can I declare it??:sad:

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.

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';

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 :'(

Please post the code using 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++)
  {
    [B]if(item[i]='+')    // this should be if (item[i] == '+')[/B]
   {
      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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.