Hi, im trying to create a program that will control a dynamic array .
The program have few functions to handle dynamic array.
* Add number to array
* Erase number from array
* Print the array
I have two problems with my code .
The first one is with the EraseNumber function .
I just cant understand why it doesnt work like it should ,
The second is that every time that the menu pops up , that text
Of the menu start to be really messy for some reason .
Maybe one of you would take a look at the code and tell me what he
Thinks that the problem is .
Btw. im using Turbo C , and im runing it on a win2k OS.
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <conio.h>
// Functions proto-type
void mainMenu();
void memoryTester(int *array);
int* addNumber(int *array,int arraysize,int var);
int* eraseNumber(int *array,int arraysize,int var);
void printfArray(int *array,int arraysize);
// Main function
int main()
{
int *array = NULL;
int arraysize = 0;
int var = 0;
int choice;
// Main loop
while (choice != 4)
{
mainMenu();
printf ("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1: printf ("Add number: ");
scanf ("%d",&var);
arraysize++;
array = addNumber(array,arraysize,var);
break;
case 2: printf ("Erase number: ");
scanf ("%d",&var);
arraysize--;
array = eraseNumber(array,arraysize,var);
break;
case 3: putchar('\n');
printf ("**** PRINTING ARRAY ****\n\n");
printfArray (array,arraysize);
getch();
break;
case 4: printf ("Press any key to quit...\n");
getch();
break;
default: printf ("Wrong choice!\n");
printf ("Press any key to continue...\n");
getch();
break;
}
}
free (array);
return 0;
}
// Main menu function
void mainMenu()
{
clrscr();
printf ("**** MAIN MENU ****\n\n");
printf ("1. Add number...\n");
printf ("2. Erase number...\n");
printf ("3. Print array...\n");
printf ("4. Quit...\n");
putchar ('\n');
}
// Memory test function
void memoryTest(int *array)
{
if (array == NULL)
{
printf ("Failed to allocate memory!\n");
printf ("Press any key to quit...\n");
getch();
exit(1);
}
}
// Add number function
int* addNumber(int *array,int arraysize,int var)
{
int *temp,i;
if (array == NULL)
{
array = (int *) malloc (arraysize * sizeof(int));
memoryTest(array);
array[0] = var;
}
else
{
temp = array;
array = (int *) malloc (arraysize * sizeof(int));
memoryTest(array);
for (i=0;i<arraysize-1;i++)
array[i] = temp[i];
array[i] = var;
}
free (temp);
return array;
}
// Erase number function
int* eraseNumber(int *array,int arraysize,int var)
{
int *temp,i,tmp=0;
if (arraysize == 0)
{
array = NULL;
return array;
}
for (i=0;i<arraysize+1;i++)
{
if (array[i] == var)
tmp = 1;
}
if (tmp == 0)
{
printf ("Error: No such number!\n");
printf ("Press any key...\n");
getch();
return array;
}
temp = array;
array = (int *) malloc (arraysize * sizeof(int));
memoryTest(array);
for (i=0;i<arraysize;i++)
{
if (temp[i] == var)
continue;
else
array[i] = temp[i];
}
free (temp);
return array;
}
// Print array function
void printfArray(int *array,int arraysize)
{
int i;
for (i=0;i<arraysize;i++)
printf ("%d,",array[i]);
putchar('\n');
}