Segmentation fault on my program
I tried to run my unfinishem program and I get a segmentation fault. It compiles without errors and warnings.
Code:
//Reverse Polish Notation Calc
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
void help();
int main(int argc, char *argv[]){
int i;
int u=0;
int j=0;
int z=0;
int st_stevk=0;
float sklad[20];
float plus[20];
if(argc<4)
{
help();
exit(1);
}
for(i=1;i<argc;i++)
{
if(isdigit(argv[i]))
{
sklad[j]=atof(argv[i]);
j++;
st_stevk++;
}
if((st_stevk>=2)&&(isalpha(argv[i])))
{
z=j-st_stevk;
switch(*argv[i]){ //ascii kode od operatorjev
case '+': for(u=0;u<st_stevk;u++)
{
plus[u]=sklad[z];
z++;
};
//case '-': for(i=
}
st_stevk=0;
}
}
return 0;
}
void help() {
printf("\n");
char hilfe[]="Vpisati moras vsaj tri argumente!\n"
"Primer: ./sedma 3 4 +, pri tem pa\n"
"mora biti operator vedno za zadnjim\n"
"operandom, ki ga operacija zajema\n";
printf("%s\n",hilfe);
}
Im trying to make a RPN calculator. That means that first the operands are inputed(im my prog as cmd arguments) and then the operator.
Example:
3 5 + would print 8 (3+5).
The reason Im complicating is because I would like to make this to work for more operands and operators:
Example :
input: 2 3 + returns 5
5 4 + returns 9
* returns 45
Anyway, can someone please explain what causes segmentation fault.
THanks!