Results 1 to 9 of 9
  1. #1

    Default [C] need help with array


    hello ninyo.
    nag-lisod ko sa amo project sa skul.. naa koy 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:
    Code:
    #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;
    		}
    	}
    }

  2. #2

    Default Re: [C] need help with array

    header file na bro??

  3. #3

    Default Re: [C] need help with array

    Quote Originally Posted by jerx d great
    header file na bro??
    dili.. command line executable.. mao na ang working code. suwayi daw save and compile bsin mka-tabang ka nko. hehe. thanks for looking.
    here is a pic of what i'm talking about.. (right-click, view image)
    [img width=359 height=500]http://img244.imageshack.us/img244/9748/errorhd3.jpg[/img]

    -------------
    and i have another question.
    say i have 2 functions. one is ADDITEM(), the one i posted, and the other is DISPLAYITEM(). so they need to be able to access the same arrays right? unsaon man na?

  4. #4

    Default Re: [C] need help with array

    gi uwayan ninyo fflush(stdin);
    man basin to...

  5. #5

    Default Re: [C] need help with array

    their is a problem on the way you dynamically allocate the size of your array..use malloc or calloc

  6. #6

    Default Re: [C] need help with array

    // int code[x], price[x];
    // char ans, name[x][30];


    int code *x,*price;
    char ans,**name;


    sample use of malloc
    malloc(sizeof(int) *x);
    malloc(sizeof(int) *x*30);

  7. #7

    Default Re: [C] need help with array

    thanks everyone for the suggestions.. pero ni-ana man amo sir nga dili nalang toh ang increment2x sa array, so wla na ni nga problem.
    pero unsa man nang 'malloc()' nga function? pwede simple explanation? hehe.

  8. #8

    Default Re: [C] need help with array

    Quote Originally Posted by ares623
    thanks everyone for the suggestions.. pero ni-ana man amo sir nga dili nalang toh ang increment2x sa array, so wla na ni nga problem.
    pero unsa man nang 'malloc()' nga function? pwede simple explanation? hehe.
    malloc() actually allocate memories space for a particular variable!!

  9. #9

    Default Re: [C] need help with array

    malloc = a function that calls a block of memory from the heap

    visit http://computer.howstuffworks.com/c29.htm or http://cprogramming.com

    naay dghan ani dd2 asta mga quiz cgro sa imu teacher naa dd2

    peace!!


  10.    Advertisement

Similar Threads

 
  1. need help with p900
    By kloos in forum Gizmos & Gadgets (Old)
    Replies: 8
    Last Post: 09-04-2006, 01:55 PM
  2. NEED HELP WITH SOME SONGS..
    By DaNZ|G in forum Music & Radio
    Replies: 1
    Last Post: 01-08-2006, 08:44 PM
  3. need help with wordpress
    By gaily_girl in forum Websites & Multimedia
    Replies: 10
    Last Post: 12-19-2005, 08:11 PM
  4. NEED HELP WITH THIS CODE
    By saminoske in forum Websites & Multimedia
    Replies: 1
    Last Post: 12-05-2005, 11:27 AM
  5. Need help with CSS png transparency.
    By silent-kill in forum Websites & Multimedia
    Replies: 1
    Last Post: 07-01-2005, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top