473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

atoi problem

This is a piece of code that I have left to complete my project. I
have hopefully one small error that needs to be fixed. This portion of
the code evaluates the postfix notation that is passed to it. I have
marked the error line. Thank you very much for your help.
void evaluates(char *postfix)
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;

for(position=0; postfix[position] !='\0'; position++)
{
symbol=postfix[position];
if(symbol=='0'||symbol=='1'||symbol=='2'||symbol== '3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol =='7'||symbol=='8'||symbol=='9')
{
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
}
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}

again thank you very much for your help.
Jul 22 '05 #1
6 3428
Henry Jordon wrote:
This is a piece of code that I have left to complete my project. I
have hopefully one small error that needs to be fixed. This portion of
the code evaluates the postfix notation that is passed to it. I have
marked the error line. Thank you very much for your help.
void evaluates(char *postfix)
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;

for(position=0; postfix[position] !='\0'; position++)
{
symbol=postfix[position];
if(symbol=='0'||symbol=='1'||symbol=='2'||symbol== '3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol =='7'||symbol=='8'||symbol=='9')
{
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
}
else if(symbol=='^')
{
number3=number2*number2; I don't think this is what you mean here. }
number_stack.push(number3);
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}

again thank you very much for your help.


The variable `answer' is of type `char'. The function atoi() takes a
parameter of type `const char *'. What's not to understand? ;-)

[To get the number designated by a char that's a digit, subtract '0';
for example, '3' - '0' = 3.]

HTH,
--ag

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."
Jul 22 '05 #2
On 12 Jul 2004 16:14:45 -0700, bu*******@hotmail.com (Henry Jordon)
wrote in comp.lang.c++:
This is a piece of code that I have left to complete my project. I
have hopefully one small error that needs to be fixed. This portion of
the code evaluates the postfix notation that is passed to it. I have
marked the error line. Thank you very much for your help.
void evaluates(char *postfix)
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;

for(position=0; postfix[position] !='\0'; position++)
{
symbol=postfix[position];
if(symbol=='0'||symbol=='1'||symbol=='2'||symbol== '3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol =='7'||symbol=='8'||symbol=='9')


In addition to Artie's correct answer, do you realize that you could
include <cctype> and replace the line above with:

if (std::isdigit(symbol))

???

Then replace the rest with a switch() statement?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
Henry Jordon wrote:
[snip]
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
This brings up a question immediatly.
You are dealing with numbers here. So why not have a stack
of int and do all calculations with int instead of char. This
way you get the desired result without any problems. This would
also deal easily with the problem of eg. doing 3 + 4 + 5 where
you would need more then 1 digit to represent the result.
All you need to do would be to convert the digit characters from
your symbol stack to an integer before you push it onto the number
stack.

BTW: Have you learned about the function isdigit()?

if(symbol=='0'||symbol=='1'||symbol=='2'||symbol== '3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol =='7'||symbol=='8'||symbol=='9') {
number_stack.push(symbol);
}


This would become simpler by using isdigit

if( isdigit( symbol ) )
{
number_stack.push( symbol );
}

or if you change to the proposed strategy of using an int stack for the
numbers:

if( isdigit( symbol ) )
{
number_stack.push( symbol - '0' );
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Henry Jordon wrote:
This is a piece of code that I have left to complete my project. I
have hopefully one small error that needs to be fixed. This portion of
the code evaluates the postfix notation that is passed to it. I have
marked the error line. Thank you very much for your help.
void evaluates(char *postfix)
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;

for(position=0; postfix[position] !='\0'; position++)
{
symbol=postfix[position];
if(symbol=='0'||symbol=='1'||symbol=='2'||symbol== '3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol =='7'||symbol=='8'||symbol=='9')
{
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
}
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}

again thank you very much for your help.


Another strategy is to use a table or map of
<symbol, function pointer> pairs:

typedef int (*Function_Pointer)(int number1, int number2);
int Add(int, int);
int Subtract(int, int);
int Multiply(int, int);
int Divide(int, int);
int Power(int, int);

struct Operator_Record
{
char symbol;
Function_Pointer function;
};

Operator_Record operator_table[] =
{
{'+', Add}, {'-', Subtract},
{'*', Multiply}, {'/', Divide},
{'^', Power}
};
const unsigned int MAX_OPR_FUNCS =
sizeof(operator_table) / sizeof(operator_table[0]);

int Add(int a, int b)
{
return a + b;
}

int evaluate(std::string text)
{
// ...
if (isdigit(text[position]))
{
number_stack.push(text[position])
}
else
{
unsigned int i;
for (i = 0; i < MAX_OPR_FUNCS; ++i)
{
if (operator_table[i].symbol == text[position])
{
int num2 = atoi(number_stack.pop());
int num1 = atoi(number_stack.pop());
int result;
result = (operator_table[i].function)(num1, num2);
}
}
}
// ...
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5
Thomas Matthews wrote:
[snip]

Not to question the idea of a function table
(it is a good one).

But: To the OP

The posted code contains some problems. I don't know
if Thomas left them in intenionally (after all it
is your assignement), but beware, eg.:
He doesn't do anything with the result of the computation,
and your atoi() problem is still there :-)

int evaluate(std::string text)
{
// ...
if (isdigit(text[position]))
{
number_stack.push(text[position])
}
else
{
unsigned int i;
for (i = 0; i < MAX_OPR_FUNCS; ++i)
{
if (operator_table[i].symbol == text[position])
{
int num2 = atoi(number_stack.pop());
int num1 = atoi(number_stack.pop());
int result;
result = (operator_table[i].function)(num1, num2);
}
}
}
// ...
}


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6
bu*******@hotmail.com (Henry Jordon) wrote in message news:<aa**************************@posting.google. com>...
void evaluates(char *postfix)
'postfix' should have type 'const char *'. Also, 'evaluates' (why
"s"?) should either return the answer (probably as a double, but not
necessarily), or it should take a 'result' parameter and return a
status code: 0 for success, and a non-zero value otherwise.
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;
Some consider it bad practice to use 'stack' since the STL defines a
'std::stack' type. If you import the 'std' namespace (at some point),
you might run into problems. It's better to use 'Stack' or 'my_Stack'
or some namespace-qualified stack type. Also, since you are
*evaluating* the expression, I would expect 'number_stack' to be a
stack of 'int' values, and similarly, your temporary variables should
have type 'int' as well.
for(position=0; postfix[position] !='\0'; position++)
'position' is local to this loop. Therefore, you may declare it inside
the 'for' statement. However, since 'postfix' is a null-terminated
string, a common idiom is to increment and dereference 'postfix':

while(char symbol = *postfix)

Likewise, 'number1', 'number2', and 'number3' are local to the for
loop.
{
symbol=postfix[position]; [rearranged]
if(symbol=='0'
|| symbol=='1'
|| symbol=='2'
|| symbol=='3'
|| symbol=='4'
|| symbol=='5'
|| symbol=='6'
|| symbol=='7'
|| symbol=='8'
|| symbol=='9')

More concisely expressed as

if(std::strchr("0123456789", symbol) != 0) {
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
What if 'number2' is 0? Do you want to assert? }
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
You don't really need 'number3' since you can just 'push' the result
back onto the stack in every operator branch.
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}


Printing the result to standard output makes this function very
difficult to programmatically verify. How are you planning to test
this function? If you returned the result in a pararmeter (and
returned a status code from the function), you can easily test each
operand branch as well as correct execution for invalid expressions.
For example, assuming the function

int evaluate(const char *postfix, double *result);
// Evalute the specified 'postfix' mathematical expression
// and store the result in the specified 'result' parameter.
// Return 0 on success, and a non-zero value otherwise. In
// particular, if 'postfix' is an invalid or incomplete
// expression, a non-zero value is returned, and the value
// of 'result' is indeterminate. The behavior is undefined
// unless 'postfix' is a null-terminated string and 'result'
// is a valid pointer.

and a working (tested) function

std::string infix2postfix(const char *infix);

You can test (portions of) 'evaluate' as follows:

// Basic Evaluation Test
const struct {
const char *d_infix; // valid infix expression
double d_result; // result of 'infix' evaluation
} DATA[] =
{
// infix result
//------------------ -----------------
{ "0.0", 0.0 },
{ "1.0", 1.0 },
{ "12.0", 12.0 },
{ "123.0", 123.0 },
{ "0.1", 0.1 },
{ "0.12", 0.12 },
{ "0.123", 0.123 },
{ "1.0 + 2.0", 1.0 + 2.0 },
{ "3.0 - 4.0", 3.0 - 4.0 },
{ "5.0 * 6.0", 5.0 * 6.0 },
{ "7.0 / 8.0", 7.0 / 8.0 },
{ "1.0 + 2.0 - 3.0", 1.0 + 2.0 - 3.0 },
{ "1.0 + 2.0 * 3.0", 1.0 + 2.0 * 3.0 },
{ "1.0 + 2.0 / 3.0", 1.0 + 2.0 / 3.0 },
{ "1.0 - 2.0 * 3.0", 1.0 - 2.0 * 3.0 },
{ "1.0 - 2.0 / 3.0", 1.0 - 2.0 / 3.0 },
{ "1.0 * 2.0 / 3.0", 1.0 * 2.0 / 3.0 },
{ "1.0 * 2.0 + 3.0", 1.0 * 2.0 + 3.0 },
{ "1.0 / 2.0 - 3.0", 1.0 / 2.0 - 3.0 },
};
enum { DATA_SIZE = sizeof DATA / sizeof *DATA };

for(int i=0; i < DATA_SIZE; ++i){
std::string postfix = infix2postfix(DATA[i].d_infix);
const double RESULT = DATA[i].d_result;
double result;
int rc = evaluate(postfix.c_str(), &result);

if(veryVerbose){
std::cout << '\t'
<< "i = " << i << ' '
<< "rc = " << rc << ' '
<< "infix = " << infix << ' '
<< "RESULT = " << result << ' '
<< "result = " << result
<< std::endl;
}
assert(0 == rc && RESULT == result);
}

HTH, /david
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
6
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
2
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; ...
4
by: sachinahuja82 | last post by:
Hi I am using atoi and strtoul in my code. I have read from MSDN that both functions return 0 if they are unsuccessful. i.e strtoul returns 0 if no conversion can be performed. Now the string...
13
by: ptq2238 | last post by:
Hi, I have written this code to help me learn C but I'm not sure why gcc - Wall is giving me an error when I compile Basically I want to read in a character then a number and then manipulate...
9
by: Would | last post by:
Hey, hopefully one of you can help me... I keep getting an unresolved external 'atoi(char)' and I dont know why.. here is the code #include <iostream> #include <stdlib.h> using namespace std; ...
23
by: tolkien | last post by:
Hi,My problem is this: I have : char matrix=.... int x; x=atoi(matrix); This doesn't work.Any help? I don't have any errors.Just a warning" passing argument 1 of 'atoi' makes pointer from...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
50
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.