473,626 Members | 3,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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'||s ymbol=='2'||sym bol=='3'||symbo l=='4'||symbol= ='5'||symbol==' 6'||symbol=='7' ||symbol=='8'|| symbol=='9')
{
number_stack.pu sh(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.pu sh(number3);
}
}
answer=number_s tack.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 3447
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'||s ymbol=='2'||sym bol=='3'||symbo l=='4'||symbol= ='5'||symbol==' 6'||symbol=='7' ||symbol=='8'|| symbol=='9')
{
number_stack.pu sh(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.pu sh(number3);
}
}
answer=number_s tack.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*******@hotma il.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'||s ymbol=='2'||sym bol=='3'||symbo l=='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(s ymbol))

???

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.l earn.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.pu sh(number3);
}
}
answer=number_s tack.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'||s ymbol=='2'||sym bol=='3'||symbo l=='4'||symbol= ='5'||symbol==' 6'||symbol=='7' ||symbol=='8'|| symbol=='9') {
number_stack.pu sh(symbol);
}


This would become simpler by using isdigit

if( isdigit( symbol ) )
{
number_stack.pu sh( symbol );
}

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

if( isdigit( symbol ) )
{
number_stack.pu sh( 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'||s ymbol=='2'||sym bol=='3'||symbo l=='4'||symbol= ='5'||symbol==' 6'||symbol=='7' ||symbol=='8'|| symbol=='9')
{
number_stack.pu sh(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.pu sh(number3);
}
}
answer=number_s tack.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_Poin ter)(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_Pointe r 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::s tring text)
{
// ...
if (isdigit(text[position]))
{
number_stack.pu sh(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_sta ck.pop());
int num1 = atoi(number_sta ck.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.l earn.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::s tring text)
{
// ...
if (isdigit(text[position]))
{
number_stack.pu sh(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_sta ck.pop());
int num1 = atoi(number_sta ck.pop());
int result;
result = (operator_table[i].function)(num1 , num2);
}
}
}
// ...
}


--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #6
bu*******@hotma il.com (Henry Jordon) wrote in message news:<aa******* *************** ****@posting.go ogle.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.pu sh(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.pu sh(number3);
You don't really need 'number3' since you can just 'push' the result
back onto the stack in every operator branch.
}
}
answer=number_s tack.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 programmaticall y 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(c onst 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(D ATA[i].d_infix);
const double RESULT = DATA[i].d_result;
double result;
int rc = evaluate(postfi x.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
7434
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()" for more information. > > **************************************************************** > Personal firewall software may warn about the connection IDLE > makes to its subprocess using this computer's internal loopback
6
16040
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 PushUnique(int); for(y = 0; y < 9; y++)
2
2225
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; int check=1;
4
4790
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 that I want to convert is "0x0" The code that I am using is char *Temp = NULL; char sVal = "0x0";
13
5759
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 the number. I've tried scanf such as scanf("%c%d",&letter,&number); but when I type in say 2 letters instead of 1 letter and 1 number, I get a zero for the 2nd value as it doesn't match what scanf is expecting so I'm
9
4020
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; void main() { int atoi(char c);
23
2973
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 integer without a cast"
4
2509
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 will be a digit for which there are known characteristics which i will print.
50
5210
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 success or not. Am I wrong? Bill
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.