Hi, I'm currently a student given the task of programming a RPN calculator using C.
The following code is some test code that I made to solve the issue described in the thread title. It calls a custom getline() function(while loop) which is also shown and then should take in two inputs entered separately or togther separeted by a space. Then when the "=" sign is entered it should print out the sum of the two numbers that were entered. And when q is pressed it should quit the program however currently what it does is that it continues to the next getline() call and asks for another input even though I already told it to quit. And then if I type q once more it proceeds to quit even though it should have quit the first time.
The weird thing is that if I put the output file through gdb the code executes just like I want it to. However running the output file does the error described above.
The code was separate C files but I combined them to post here
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 100
float stack[STACKSIZE];
int pos=-1;
void print(float ans)
{
printf("ANS = %f", ans);
}
void add()
{
float temp1 = pop();
float temp2 = pop();
float sum=0;
sum = temp2 + temp1;
push(sum);
}
void stackdec(char s[])
{
float addtostack;
addtostack=atof(s);
push(addtostack);
}
float pop()
{
if(pos>=0)
{
float num=stack[pos];
pos--;
return num;
}
else {
printf("Error, No Numbers to Perform Function");
exit(0);
}
}
void push(float push)
{
pos++;
stack[pos]=push;
}
int main()
{
int resume=1;
char s[100];
int i=0;
memset(&s[0], 0, sizeof(s));
float ans=0;
while(resume==1)
{
printf("\nTesting Adding, Input 2 Numbers Separated By Space:\n");
getline(s,100);
if(strcmp(s,"q")==0)
{
resume=0;
exit(0);
}
else
{
stackdec(s);
getline(s,100);
if(strcmp(s,"q")==0)
{
resume=0;
exit(0);
}
else
{
stackdec(s);
add();
ans=pop();
print(ans);
}
}
}
return 0;
}