PDA

View Full Version : [C] need help with array


johnlouis
03-08-2007, 06:55 AM
hello everyone..
i'm having difficulty with our project at school.. i have 3 arrays.. i assign values to them using for loops and scanf. then the program asks if i want to add another value to the array.
it works for the first 2 or 3 increments to the array but after that, one of the values of the array is messed up.. please help. :|
here is the code, i added some comments to try and clarify it a bit:
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int check(char ans,char big,char small){
if(ans == big || ans == small){
return TRUE;
}else{
return FALSE;
}
}
ADDITEM(){
/*initialize*/
int x,last,i;
printf("Enter # of items: ");
scanf("%d",&x);
int code[x], price[x];
char ans, name[x][30];
ans = 'y';
last = i = 0;
while(check(ans,'Y','y')){
/*ask for input*/
for(i = last;i < x;i++){
printf("Enter item code #%d: ",i+1);
scanf("%d",&code[i]);
printf("Enter name of item #%d: ",i+1);
scanf("%s",&name[i]);
printf("Enter item price #%d: ",i+1);
scanf("%d",&price[i]);
}
/*print list of values*/
printf("\n----------LIST-----------\n");
for (i = 0;i<x;i++){
printf("\nItem code #%d: %d",i+1,code[i]);
printf("\nItem name #%d: %s",i+1,name[i]);
printf("\nItem price #%d: %d",i+1,price[i]);
printf("\n");
}
last = x; /*assign last value of x to variable for reference*/
/*ask user to increment list*/
printf("\n\nAdd another item[Y/N]? ");
scanf("%s",&ans);
if(check(ans,'N','n')){
break;
}else if(check(ans,'Y','y')){
x++;
}else{
while(!check(ans,'N','n')||!check(ans,'Y','y')){
if(check(ans,'N','n')||check(ans,'Y','y')){
if(check(ans,'Y','y')){
x++;
}
break;
}else{
printf("Y or N?");
scanf("%s",&ans);
}
}
}
}
}
main(){
char choice;
choice = '\0';
printf("START [Y/N]? ");
scanf("%s", &choice);
while(!check(choice,'n','N')){
if(check(choice,'Y','y')){
ADDITEM();
}else if(check(choice,'n','N')){
break;
}
}
}attached is an image of what i am talking about..

and one more thing, suppose i am to make another function, DISPLAYITEMS(), it would display the items from the previous function. So it/they would probably need to refer to the same array right? how would i go about doing that? thanks for any help. :)

TheColonial
04-08-2007, 04:47 AM
Hi,

You have a fair few issues here:

You're using a non-standard C compiler (evident from the code which it's allowed you to write), which means you're not going to learn what you need to learn.
Not all of your functions specify a return type (which is a bad habit).
The variable x is used to store the number of items, then is incorrectly used to initialise the size of an array (you should be using malloc() instead).
The varaible x changes inside the loop, so even if you did allocate enough memory at the start, you are writing into memor past the bounds of your array - this is bad.
The reason you're getting garbled values is because your array is not big enough to store all the details you want, and as a result it's getting overwritten.I would recommend that you go back to your notes, and read up on memory allocation (malloc/calloc/realloc/free). Also, do some reading on buffer overflows and making sure that arrays are not indexed beyond its size.

Good luck.
OJ