Debugged
Date: 03/30/08
(Algorithms) Keywords: programming
Edit: Error was located and corrected. I love how programming can turn programmers temporarily blind :) I'll leave it here in the hope that perhaps others with the same problem can find it and think "OOOOooooOooh" before publically admitting to a community that they can't keep track of their pointers. :)
Thank you all for your help to my previous post! I've started to pick up C syntax really quickly since, and I've been integrating my two programs; the one with the functions to push, pop and create arrays, with the program lightning_rose suggested that I should write. So far my array seems to work and I'm sure that my push function is working, but when I ask it to print the contents of the stack, it prints 0. My aim isn't to iterate down the entire array, but to print that they've been pushed once they've been pushed.
Here is the buggy bit - where I expect the error is:
/* else, it will be an integer.*/
else
{
int temp = atoi(argv[i]);
push (temp);
//int i = 0;
printf("Stack: %d", stack[top]);
//printf("Integer: %d\n",atoi(argv[i]));
}
/*
* **** HISTORY *****
*
* Filename: test.c
* Synopsis: test
*
* This is a simple program that will take input from the keyboard and then
* print each line of input to screen.
* Version 0.1 - Program simply returned a string of characters that had been
* entered using printf.
* version 0.2 - program differentiated between a string of characters and an
* integer.
* version 0.3 - Fixed a bug in the code that caused a 0 integer to be printed
* after characters. Originally believed it to be a null byte, later discovered
* that it was, in fact, a flaw in my if statements since I hadn't used else.
* the use of else and else if in the testing area of the program are important
* so that characters aren't printed as characters then passed down to be converted
* into integers.
* version 0.4 - program began to differentiate between characters, an operand
* and the specific operators +, -, /, x and =. I was able to tell that the
* differentiation had been made by asking the program to print out which each
* was, for example: If I had entered: test 1 a + the program would output:
* Integer: 1, Character: a, Symbol +.
* version 0.5 - Array was defined from code that had been written in p3.c which was
* written at an earlier date. This is where p3.c and test.c began to merge as
* the final solution to p3.c with p3.c merging into test.c.
*/
#include
#include
/*
* define is used to make modification of each easier. It is also good for readability.
* Characters A - D will be assigned values that the user wishes to store so that they
* can later be used by, for example, 2 A +.
*/
#define stack empty
#define MemoryA A
#define MemoryB B
#define MemoryC C
#define MemoryD D
/*
* I need to define my array that will be used in a similar way to how I understand a
* stack. The type of array that I will use is a fixed-length array so that the program
* size will have a limit of 20 elements.
* See Page 112 - C In a Nutshell
*/
int top = 0; /*Pointer to keep track of where the top of the stack/array is*/
//int maxstack = 20; /*Allocates the maximum value of the stack-array */
int stack[50]; /*sets the array stack equal of the maximum value of the stack*/
void push(int x);
/*
* The push function will push a new entry onto the stack at the stack pointer "top".
* Once the entry (passed in as x) has been assigned as an element to the stack the
* "stack pointer" top will then increment by one so that it is pointing to the next
* free space.
*/
void push(int x)
{
stack[top] = x;
top ++;
}
int main (int argc, char *argv[])
{
int i;
int j = 0;
for (i = 1; i < argc; i++)
{
for(j=0; j < strlen(argv[i]); j++)
{
/*This is a test to see if a the input is a
* alphabetical number. If it is, then the
* program will then check to see if it is
* specifically A, B, C or D.
*/
if (isalpha(argv[i][j]))
{
printf("Character: %s\n", argv[i]);
}
/* These tests will test to see if input is an operand*/
else if (strcmp(argv[i], "+") == 0)
{
printf("Symbols: %s\n", argv[i]);
}
else if (strcmp(argv[i], "-") == 0)
{
printf("Symbols: %s\n", argv[i]);
}
else if (strcmp(argv[i], "/") == 0)
{
printf("Symbols: %s\n", argv[i]);
}
else if (strcmp(argv[i], "x") == 0){
printf("Symbols: %s\n", argv[i]);
}
/* else, it will be an integer.*/
else
{
int temp = atoi(argv[i]);
push (temp);
//int i = 0;
printf("Stack: %d", stack[top]);
//printf("Integer: %d\n",atoi(argv[i]));
}
}
}
}
Source: https://algorithms.livejournal.com/98764.html