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*);
printf("Enter name of item #%d: ",i+1);
scanf("%s",&name*);
printf("Enter item price #%d: ",i+1);
scanf("%d",&price*);
}
/*print list of values*/
printf("
----------LIST-----------
");
for (i = 0;i<x;i++){
printf("
Item code #%d: %d",i+1,code*);
printf("
Item name #%d: %s",i+1,name*);
printf("
Item price #%d: %d",i+1,price*);
printf("
");
}
last = x; /*assign last value of x to variable for reference*/
/*ask user to increment list*/
printf("
Add 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.