Please support our C++ advertiser: Programming Forums
Views: 8305 | Replies: 4
![]() |
Hi Blitzer,
I've compiled this with Dev-C++ and it worked correctly.
Good luck.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char a_char[10];
cin >> a_char;
cout << "As an integer: " << atoi(a_char);
}Good luck.
Well you can create a short string with
Then pass that to atoi, though strtol etc are better long term bets.
A quick answer is to do
char s[2]; s[0] = ch; s[1] = '\0';
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.
•
•
Join Date: Jul 2006
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
I have some ploblem
I want to make calculator for postfix number
example input : 45+
answer is 9
and I use
and this is my postfix fuction
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
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.
Please post the code using [code] tags.
Hope it helped,
Bye.
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.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode