I am having problems with this little program. It should output something like 33 p1, 29 p1, each one properly positioned inside a cell, but because of the line with strcat, ) it is getting messed up and I get the output as here:
<pre>
| - |
+----------+
| - |
+----------+
| |
+----------+
| 33 p1 |
+----------+
|22 p133 p1 |
+----------+
</pre>
Code:
/* Crie um programa que implemente duas pilhas em um único vetor, que deve
* receber no máximo 10 elementos e a pilha 1 (p1) deve ser implementada da
* esquerda para a direita, já a pilha 2 (p2) deve ser implementada da
* direita para a esquerda. */
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#define STACKSIZE 10
struct StackStruct {
short int top1;
short int bot1;
short int top2;
short int bot2;
char nums[STACKSIZE][5]; // Números de no máximo 5 caracteres.
};
short int showMenu(void);
void iniStruct(struct StackStruct *p);
void insIntoStackOne(struct StackStruct *p);
void insIntoStackTwo(struct StackStruct *p);
void showStack(struct StackStruct *p);
int main(void) {
struct StackStruct Pilha;
short int opt;
iniStruct(&Pilha);
do {
opt = showMenu();
switch (opt) {
case 0:
break;
case 1:
insIntoStackOne(&Pilha);
break;
case 2:
insIntoStackTwo(&Pilha);
break;
case 3:
showStack(&Pilha);
break;
default:
printf("Something went wrong.\n");
} // fim switch().
} while (opt != 0);
return 0;
}
void iniStruct(struct StackStruct *p) {
p->bot1 = -1;
p->top1 = -1;
p->bot2 = 10;
p->top2 = 10;
for (short int k = 0; k < STACKSIZE; k++) {
strcpy(p->nums[k], "-"); // 45 é o char '-'.
}
}
short int showMenu(void) { // {{{
short int opt;
printf("+-------------------------------+\n");
printf("| ---- SATCK STUDIES IN C ----- |\n");
printf("+-------------------------------+\n");
printf("| %24s -- %d |\n", "Insert in Stack ONE", 1);
printf("| %24s -- %d |\n", "Insert in Stack TWO", 2);
printf("| %24s -- %d |\n", "Show Stack Content", 3);
printf("| %24s -- %d |\n", "Remove From Stack ONE", 4);
printf("| %24s -- %d |\n", "Remove From Stack TWO", 5);
printf("| %24s -- %d |\n", "Quit Program", 0);
printf("+-------------------------------+\n");
printf("--> CHOOSE ONE OPTION: ");
__fpurge(stdin);
do {
scanf("%hd", &opt);
__fpurge(stdin);
} while (opt < 0 || opt > 3);
return opt;
} // showMenu() fim. }}}
void insIntoStackOne(struct StackStruct *p) {
if ((p->top1 + 1) < p->top2) {
printf("top1: %hd, top2: %hd\n", p->top1, p->top2);
p->top1++;
printf("Type a number for stack one: ");
scanf("%s", p->nums[p->top1]);
__fpurge(stdin);
strcat(p->nums[p->top1], " p1");
printf("%s\n", p->nums[p->top1]);
}
} // fim insIntoStackOne().
void insIntoStackTwo(struct StackStruct *p) {
if ((p->top2 - 1) > p->top1) {
printf("top2: %hd, top1: %hd\n", p->top2, p->top1);
p->top2--;
printf("Type a number for stack two: ");
scanf("%s", p->nums[p->top2]);
__fpurge(stdin);
}
}
void showStack(struct StackStruct *p) {
//for (short int i = p->top1; i > p->bot1; i--) {
for (short int i = STACKSIZE - 1; i > p->bot1; i--) {
printf("\t+----------+\n");
printf("\t|%8s |\n", p->nums[i]);
}
printf("\t+----------+\n");
getchar();
}
/*
* vim: set foldmethod=marker foldmarker={{{,}}}:
*/