I wrote out the code the diagnostics have no errors but i keep getting this logic error. the program is supposed to read in a line, put the line in a stack and reverse the line as it comes out of the stack.
I would highly appreciate it if anyone can give a general direction in solving this problem thanks in advance. I really appreciate it.
/* reversChar.c */
#include <stdio.h>
#include <stdlib.h>
#define N 81
typedef struct stack
{
int *array;
int size;
int top;
}STACK;
STACK *S;
STACK *StackCreate(int nSize);
void StackDestr(STACK *S);
char StackPush(STACK *S, int Value);
char StackPop(STACK *S, int Target);
int StackIsFull(STACK *S);
int StackIsEmpty(STACK *S);
int main (void)
{
char line[80], revline[80];
int p, i, ch;
StackCreate(N);
printf("Enter a line to be reversed: \n");
for (i = 0; (i <80 ) && ((ch = getchar()) !=EOF) && (ch != '\n'); i++ )
line [i] = (char)ch;
line[i] = '\0';
printf("Line input: %s\n", line);
for (p=0; p<N; p++)
{
putchar(StackPush(S, atoi(line+1))); /* My error seems to be here */
}
for (p=0; p<N; p++)
{
StackPop(S, line[i]);
}
revline[i] = '\0';
printf("Line Output: %s\n", revline);
return 0;
}
STACK *StackCreate(int nSize)
{
int k;
S = (STACK *)malloc(sizeof(STACK));
if (S == NULL) {perror("reversChar"); exit(1);}
S->array = (int *)malloc(nSize*sizeof(int));
if (S->array == NULL) { perror("reversChar"); exit(1);}
S->size = nSize;
S->top = -1;
for (k=0; k<S->size; k++)
S->array[k] = 0;
return S;
}
void StackDestr(STACK *S)
{
free(S->array);
free(S);
return;
}
char StackPush(STACK *S, int Value)
{
if (!StackIsFull(S) )
{
S->top = 1 + S->top;
S->array[S->top] = Value;
}
else puts("No room in Stack");
return 0;
}
char StackPop(STACK *S, int Target)
{
int firstchar;
firstchar = S->array[S->top];
S->array[S->top] = 0;
S->top--;
return firstchar;
}
int StackIsFull(STACK *S)
{
int next;
next = 1 + S->top;
if (next > -1 + S->size) return 1;
else return 0;
}
int StackIsEmpty(STACK *S)
{
if (S->top < 0) return 1;
else return 0;
}
Enter a line to be reversed:
pedro
Line input: pedro
Line Output: ¦¦¦¦¦
Press any key to continue