Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > atoi problem

Reply
Thread Tools

atoi problem

 
 
Henry Jordon
Guest
Posts: n/a
 
      07-12-2004
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.
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      07-13-2004
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."
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-13-2004
On 12 Jul 2004 16:14:45 -0700, (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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-13-2004
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

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      07-13-2004
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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-13-2004
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

 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      07-13-2004
(Henry Jordon) wrote in message news:<. 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Minor Problem With atoi() And Negative Numbers Marcelo De Brito C Programming 12 03-09-2010 10:35 AM
Problem with atoi and strlod Sanchit C Programming 6 04-14-2008 04:25 PM
Problem with atoi() tolkien C Programming 25 09-23-2007 10:01 PM
java equivalent function of c atoi() function lonelyplanet999 Java 8 11-03-2003 01:07 AM
int to char[]: What's the reciprocal of std::atoi? Magig Boatman C++ 1 07-11-2003 07:00 PM



Advertisments