1
This code below compiles both in Codeblocks and Visual Studio, but the output is different in Codeblocks. By VS I do not delete anything from the code below, just run and ready, now in Codeblocks I took out these two libraries:
#include "stdafx.h"
#include "windows.h"
and added these:
#include <stdio.h>
#include <stdlib.h>
changed the scanf_s
for scanf
and compiled, compiles normally, only that the output is wrong, this in Codeblocks. In VS is quiet works normally.
I wanted to know what’s wrong with Codeblocks, I want to run with perfect output on Codeblocks.
// pilha.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#define TAM_PILHA 2
#define NUM_ENTRA 5
void empilha(int *pilha, int *topo, int valor)
{
if (*topo <= TAM_PILHA)
{
pilha[*topo] = valor;
(*topo)++;
}
else
{
printf("Pilha Cheia\n");
}
}
int desempilha(int *pilha, int *topo)
{
if (*topo > 0)
{
(*topo)--;
return pilha[*topo];
}
else
{
printf("Pilha Vazia\n");
}
}
int main()
{
int pilha_par[TAM_PILHA], pilha_impar[TAM_PILHA], topo_par = 0, topo_impar = 0;
int i = 0, num_do_usuario = 0;
for (i = 0; i < NUM_ENTRA; i++)
{
printf("\nEntre com um numero: ");
scanf_s("%d", &num_do_usuario);
if ((num_do_usuario % 2) == 0)
{
empilha(pilha_par, &topo_par, num_do_usuario);
}
else
{
empilha(pilha_impar, &topo_impar, num_do_usuario);
}
}
printf("\n\nNumeros pares sao: ");
while (topo_par > 0)
{
printf("%d ", desempilha(pilha_par, &topo_par));
}
printf("\n\nNumeros impares sao: ");
while (topo_impar > 0)
{
printf("%d ", desempilha(pilha_impar, &topo_impar));
}
printf("\n\n");
system("pause");
return 0;
}
Wrong:
Another error scenario:
And what went wrong?
– user1084