Thread: Problem with structured array.

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    4

    Problem with structured array.

    Hi, I have problem with my coding. I'm using structured array.

    For the first loop, everything is fine. Until, I enter 'Y' for second.
    I enter the input as usual, and it is stuck at quantity. Suppose to be a runtime error. Anyone, could please enlighten the problem to me?

    Thank you in advance.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    
    
    
    
    struct product
    {
        int id, quantity, reorder, i;
        char name[20];
        float price;
    };
    
    
    void gotoxy( int column, int line );
    void addPRODUCT();
    void displayPRODUCT();
    
    
    COORD coord = {0, 0};
    
    
    
    
    void gotoxy (int x, int y)
    {
    coord.X = x; coord.Y = y; // X and Y coordinates
    
    
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    
    
    
    void main()
    {
    addPRODUCT();
    //displayPRODUCT();
    }
    
    
    
    
    void addPRODUCT()
    {
    
    
        int i=0;
        struct product a[i];
        system("cls");
    
    
        char checker;
    
    
    
    
        do
        {
            gotoxy(20,5);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 ADD PRODUCTS \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
    
    
            gotoxy(20,15);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
    
    
            gotoxy(20,7);
            printf("Enter product ID : ");
            scanf(" %d", &a[i].id);
    
    
            gotoxy(20,9);
            printf("Enter product name : ");
            scanf(" %s", a[i].name);
    
    
            gotoxy(20,11);
            printf("Enter product quantity : ");
            scanf(" %d", &a[i].quantity);
    
    
            gotoxy(20,13);
            printf("Enter product price : ");
            scanf(" %f", &a[i].price);
    
    
            gotoxy(20,17);
            printf("Record saved!\n\n");
    
    
            gotoxy(20,19);
            printf("Do you want to enter new product? Y / N : ");
            scanf(" %c", &checker);
            checker = toupper(checker);
    
    
            i++;
    
    
            system("cls");
        }
        while(checker=='Y');
    
    
        }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You have misunderstood how variable-length arrays work. In a nutshell, changing "i" won't change the size of the array after it was declared. You will have to use "realloc()" for that.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,540
    Having a member variable called i, and a loop variable called i, will surely lead to confusion.

    Also, main returns and int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    Quote Originally Posted by Salem View Post
    Having a member variable called i, and a loop variable called i, will surely lead to confusion.

    Also, main returns and int, not void.
    Quote Originally Posted by GReaper View Post
    You have misunderstood how variable-length arrays work. In a nutshell, changing "i" won't change the size of the array after it was declared. You will have to use "realloc()" for that.
    Alright. Now I am using the FILE to store the data.
    Problem came after 1st loop where, run time error occur, after I put : fprintf(fp, "%d %s %d %f", a[i].id, a[i].name, a[i].quantity, a[i].price);

    What is the problem actually ? Thanks in advance.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    #include <conio.h>
    
    
    void gotoxy( int column, int line );
    void addPRODUCT();
    int displayPRODUCT();
    
    
    struct product
    {
        int quantity, reorder, i;
        char name[20];
        float price, id ;
    };
    
    
    
    
    COORD coord = {0, 0};
    
    
    void gotoxy (int x, int y)
    {
    coord.X = x; coord.Y = y; // X and Y coordinates
    
    
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    
    
    
    /*void main()
    {
    addPRODUCT();
    displayPRODUCT();
    }*/
    
    
    
    
    int main()
    {
        FILE * fp;
        
        int i=0;
        struct product a[i];
        system("cls");
        
        char checker;
    
    
        do
        {
            gotoxy(20,5);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 ADD PRODUCTS \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
    
    
            gotoxy(20,15);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
    
    
            gotoxy(20,7);
            printf("Enter product ID : ");
            scanf(" %d", &a[i].id);
    
    
            gotoxy(20,9);
            printf("Enter product name : ");
            scanf(" %s", a[i].name);
    
    
            gotoxy(20,11);
            printf("Enter product quantity : ");
            scanf(" %d", &a[i].quantity);
    
    
            gotoxy(20,13);
            printf("Enter product price : ");
            scanf(" %f", &a[i].price);
    
    
            fp = fopen("addproduct.txt","a");
            fprintf(fp, "%d %s %d %f", a[i].id, a[i].name, a[i].quantity, a[i].price); //Without this line. No problem with looping.
    
    
            gotoxy(20,17);
            printf("Record saved!\n\n");
    
    
            fclose(fp);
    
    
            gotoxy(20,19);
            printf("Do you want to enter new product? Y / N : ");
    
    
    
    
    
    
            scanf(" %c", &checker);
            checker = toupper(checker);
    
    
    
    
            i++;
    
    
    
    
    
    
            system("cls");
        }
        while(checker=='Y');
    
    
    return(0);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,540
    > int i=0;
    > struct product a[i];
    You mis-understood what GReaper was saying.

    This creates an array of ZERO length.

    Simply doing i++ later in the program doesn't make the array any bigger!

    Start simple.
    struct product a[20];
    and make sure your loop exits when you reach 20 entries.

    > gotoxy(20,5);
    > printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2
    Here's a tip.
    Remove ALL the eye-candy pretty formatting until the rest of the program is FULLY debugged and working.
    You'll waste no end of time trying to keep it pretty while you're still trying to get the basics sorted out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    Quote Originally Posted by Salem View Post
    > int i=0;
    > struct product a[i];
    You mis-understood what GReaper was saying.

    This creates an array of ZERO length.

    Simply doing i++ later in the program doesn't make the array any bigger!

    Start simple.
    struct product a[20];
    and make sure your loop exits when you reach 20 entries.

    > gotoxy(20,5);
    > printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2
    Here's a tip.
    Remove ALL the eye-candy pretty formatting until the rest of the program is FULLY debugged and working.
    You'll waste no end of time trying to keep it pretty while you're still trying to get the basics sorted out.
    Alright. Thank you for replying.
    May I ask, what if I want the input expandable ? More than 20.

    I'm trying to let the user enter the data, and exit whenever they want.
    Do you have any tips ?

    Thank you in advance.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Alif Khair View Post
    May I ask, what if I want the input expandable ? More than 20.
    Like I said, "realloc()" should be used for that. It can change the size of a dynamically allocated array.

    Your approach as I see it though is to save each entry to a file. That's fine, albeit inefficient, but you have to choose a method. There's no point in allocating all that memory when you need only a single struct variable.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    Quote Originally Posted by GReaper View Post
    Like I said, "realloc()" should be used for that. It can change the size of a dynamically allocated array.

    Your approach as I see it though is to save each entry to a file. That's fine, albeit inefficient, but you have to choose a method. There's no point in allocating all that memory when you need only a single struct variable.
    Hi Greaper, can I use this code as an example ?
    But, I can see the code need to input N value. Can i just use loop and counter for N value inside

    ptr=(struct name*)malloc(n*sizeof(struct name)); ?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct name {
       int a;
       char c[30];
    };
    int main(){
       struct name *ptr;
       int i,n;
       printf("Enter n: ");
       scanf("%d",&n);
    
    /* Allocates the memory for n structures with pointer ptr pointing to the base address. */
       ptr=(struct name*)malloc(n*sizeof(struct name));
       for(i=0;i<n;++i){
           printf("Enter string and integer respectively:\n");
           scanf("%s%d",&(ptr+i)->c,&(ptr+i)->a);
       }
       printf("Displaying Infromation:\n");
       for(i=0;i<n;++i)
           printf("%s\t%d\t\n",(ptr+i)->c,(ptr+i)->a);
       return0;}
    Output
    Enter n: 2 <------ I need this to be dynamic.
    Enter string and integer respectively:
    Programming
    22
    Enter string, integer and floating number respectively:
    Structure
    33

    Displaying Information:
    Programming 22
    Structure 33

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,540
    Array notation still works, even if you have a pointer.

    So
    scanf("%s%d",ptr[i].c,&ptr[i].a);
    printf("%s\t%d\t\n",ptr[i].c,ptr[i].a);



    Also, there is no need to cast the return result of malloc in a C program.
    FAQ > Casting malloc - Cprogramming.com
    If you get warnings about converting void*, then it probably means you're using a C++ compiler to compile your C code - you need to fix this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with structured lists from text
    By Sertaba in forum C Programming
    Replies: 2
    Last Post: 04-05-2013, 03:20 PM
  2. Replies: 11
    Last Post: 05-10-2012, 05:42 AM
  3. sorting structured array problem
    By matrix0978 in forum C Programming
    Replies: 7
    Last Post: 11-12-2009, 01:42 AM
  4. qsort( ) on a structured array
    By Thumper333 in forum C Programming
    Replies: 2
    Last Post: 10-21-2004, 07:39 PM

Tags for this Thread