473,385 Members | 2,005 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.

can anyone find why is this program to solve postfix expression giving runtime error

i am trying to write a program to solve postfix expression but it is giving runtime error.i tried to debug but i am not getting why is it happening can anyone find out.

#include< stdio.h>

#include< string.h>

#include< stdlib.h>

struct stack{

int top;

int n[100];

}s;

int push(char a[]) {

int i,j;

int p=atoi(a);

s.n[s.top+1]=p;

s.top++;

for(i=0;i<100;i++)

a[i]=0;

j=0;

return(j);

}

void pop(char a[])

{

int c,b;

b=s.n[s.top];

c=s.n[s.top-1];

s.top--;

switch(a[0])

{

case '+':

s.n[s.top]=b+c;

break;

case '-':

s.n[s.top]=b-c;

break;

case '*':

s.n[s.top]=b*c;

break;

case '/':

s.n[s.top]=b/c;

break;

}
a[0]=0;
}

int main()

{

s.top=-1;

int m,i,k;

char a[100],c[100];

scanf("%d",&m);

for(i=0;i<m;i++)
{
int j=0;
while(1)
{
scanf("%c",&a[j]);
if(a[j]=='?')
break;
else if(a[j]==' ')
j=push(a);
else if(a[j]=='+'||'-'||'*'||'/')
pop(a[j]);
else
j++;
}
printf("%d",s.n[s.top]);
}
}
Feb 5 '15 #1
4 1417
weaknessforcats
9,208 Expert Mod 8TB
There is this code:

Expand|Select|Wrap|Line Numbers
  1. int push(char a[]) {
  2.  
  3.  int i,j;
  4.  
  5.  int p=atoi(a);   <----
Here a is an array. The name of an array is the address of the array. So this code converts the address of the array to an int. Is that what you wanted? It looks suspicious.

You have simply got to step through this code using your debugger. You can verify the values of your variables every step of the way.
Feb 5 '15 #2
Then how to convert an array of characters to integer.I have read that it was correct syntax.
Feb 5 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
Here is where you need to be accurate in your thinking. An argument of char a[] means a char array. Unfortunately, the size of the array is unknown. So char a[] is maybe an array of 1 char or maybe larger.

When this is passed to atoi, any leading whitespace characters are skipped. Then a + or - is processed for the sign of the int.

The next characters must be digits 0-9 and and contiguous. Conversion stops after the last decimal digit.

So, if atoi can't convert it returns 0. Now you can't tell if the array had a zero or that atoi failed. Not good. Further, atoi may run off the end of your array.

atoi does not change the array. So if you had 5-3, you end up with two ints 5 and -3, which then ADD to get 2. You can't parse the array, find the - sign and do 5 - (-3) or you will get 8.

As long as you know what's in the array atoi will work.

All of these issues can be resolved by using your debugger.
Feb 5 '15 #4
donbock
2,426 Expert 2GB
Some compilers can be set for different levels of pedantry and warning-verbosity. Set your compiler for maximum verbosity.
Do you have any compiler warnings?
What precisely is the nature of the run-time error?
These are clues that may lead to the source of the problem.
Feb 6 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: fanfromfla | last post by:
I am using a database that has worked for many years for a holiday project for needy families. My organization recently upgraded its server and changed everyone to Windows XP. I just mention that...
2
by: Massimo Soricetti | last post by:
Hello, I'm trying to fix this little class: <code> #include <ifstream> #include <iostream> #include <iomanip> using namespace std;
0
by: royal | last post by:
This problem is to simulate an airport landing and takeoff pattern. The airport has 3 runways, runway 1, runway 2 and runway 3. There are 4 landing holding patterns, two for each of the first two...
6
by: OldBirdman | last post by:
I've spent all day on this, and can't see what I'm doing wrong. I've Googled and all I find have the same mistake, using .Text when they should use .Value. In this case, .Text is correct. ...
3
by: razjafry | last post by:
Hi, I am trying to create a combo box to search record on the base of ID but it gives runtime error message 2501 "the openform action was cancelled" all the times. I posted this question before but...
1
by: b singh | last post by:
In iis 7 http:/site/x.aspx/pp%3f is giving runtime error Instaed of invalid virtual path. x.aspx is not existing in the server. What setting of IIS 7 is reuired so that it will give invalid...
1
by: karthik mca | last post by:
rs3.Move (Index) rs3!a_Votes = Val(Text2(Index).Text) + 1 rs3.Update here index is working... dont bother about that... but this is not working.. can any one edit this code???? it is...
1
by: krishhy | last post by:
I am trying to add data to my database using adodc my database is in ms access 2000-2003 but when i run my program its showing an error empty record-set cannot be added,and automatically ends the...
1
by: jinnejeevansai | last post by:
#include<stdio.h> #include<stdlib.h> struct stack{ int top; int n; }s; void push(int a) { s.n=a; s.top++;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.