首页 > 解决方案 > 我试图允许用户将成员添加到结构数组中。我对 c 很陌生,所以我不确定要去做。代码链接如下

问题描述

这是我正在尝试构建的功能

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;
}

这是整个代码

#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;
}

标签: c

解决方案


推荐阅读