1

this is the function i am trying to build

int addequip(){

    printf("\t\t***Add Equipment***");  
    int Eqicount=0;
    int equip_in;

    printf("\n\nDo you want to add a Item?");//asks the user if they would like to add an item
    scanf("%d",equip_in);

    if(equip_in= 1);{
        printf("Enter Item:\n");
        Eqicount++;
        Inven[Eqicount].itemdes;
        printf("Do you want to add another?");      
    }
    return 0;
}

this is the entire code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct lionsequip
{
    char itemdes[30];
    int amt;
}equipment;

equipment Inven[20];

int loadlist();//loads list from a text file
int addequip();//adds equipment to list
int equipreport();//prints equipment report to a text file
int submenu();


int main(){
    loadlist(); 
    submenu();

    return 0;   
}

int submenu() {//allows the selection of functions

    int select; 

    printf("***Welcome to the Moka Lions System***\n\n\n");
    printf("Choose an option below\n\n");
    printf("Equipment Report:1\nAdd Equipment:2\nExit:0");
    printf("\n\nSelection:");
    scanf("%d",&select);
    while(select!=0){

        switch(select)
        {
        case 1: equipreport();
            break;

        case 2: addequip();
            break;

        default:
            printf("Invalid Selection!");
        }

    return 0;       
    }
}

int loadlist(void){

    char itemdes[30];
    int amt;
    int i=0;

    FILE*fp;
    fp=fopen("EquipList.txt","r");

    fscanf(fp,"%s",itemdes);
    while(strcmp(itemdes,"END")!=0){

        fscanf(fp,"%d",&amt);
        Inven[i].amt=amt;
        strcpy(Inven[i].itemdes,itemdes);
        printf("Item:%s\n",itemdes);
        printf("Amount:%d\n\n",amt);

        fscanf(fp,"%s",itemdes);
        printf("***Your List has been loaded***\n\n");

    }
    return 0;   
}

int equipreport(){

    FILE*inn=fopen("EquipList.txt","r");
    FILE*out=fopen("Report.txt","w");

    char itemdes[30];
    int  amt;
    int i;

    for(i=0; i<12; i++){
        fscanf(inn,"%s %d",itemdes,&amt);
        fprintf(out,"%-15s \n%5d \n",itemdes,amt);
    }
    fclose(inn);
    fclose(out);
    printf("Your Equipment Report has been Loaded");
}

int addequip(){

    printf("\t\t***Add Equipment***");  
    int Eqicount=0;
    int equip_in;

    printf("\n\nDo you want to add a Item?");//asks the user if they would like to add an item
    scanf("%d",equip_in);

    if(equip_in= 1);{
        printf("Enter Item:\n");
        Eqicount++;
        Inven[Eqicount].itemdes;
        printf("Do you want to add another?");      
    }
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `Equicount` is a local variable that gets set to `0` every time you call `addequip`. It needs to be a global variable. – Barmar Jun 16 '20 at 18:28
  • Note on title semantics, It is possible to allow a user to add an additional array element to a dynamically allocated _array_ of `struct`, (or once during initialization using a [VLA](https://www.google.com/search?channel=tus2&client=firefox-b-1-d&q=VLA+in+C)) but it is not feasible to add a new member to a `struct` at run-time. – ryyker Jun 16 '20 at 18:38
  • 1
    `if(equip_in= 1);` is wrong on two counts: `=` should be `==` and the `;` should not be there. Before then, `scanf("%d", equip_in);` should be `scanf("%d", &equip_in);`. Also `Eqicount++;` should be moved down below the next line. – Weather Vane Jun 16 '20 at 18:38
  • 1
    Consider using [linked lists](https://www.geeksforgeeks.org/linked-list-set-1-introduction/). They are naturally designed to be added to, deleted from and are easily searchable. A good fit for something like an equipment list. – ryyker Jun 16 '20 at 18:53
  • @ryyker im not to verse in the jargon so what your'e saying is that I need to have a larger array size in order for a user to input more data? – Jayden Jacob Jun 16 '20 at 19:02
  • Your title question suggests you want to add struct members during run-time. This [is not possible in C](https://stackoverflow.com/questions/35397415/how-to-add-a-structure-member-dynamically-in-c). However, it is possible to create a struct, the create a pointer to that struct, and create several instances of that struct into what amounts conceptually to as _an array of struct_. – ryyker Jun 16 '20 at 20:21

0 Answers0