2017-09-14 20 views
-2

我创建了一个游戏平台,我想交易一些游戏。C - 将字符串添加到数组x并将其从数组中移除y

因此,我设法阐述了一个代码,用户可以将自己的游戏出售给市场,另一个用户可以随时购买,如果他有信用。为了做到这一点,我需要将已售出的游戏添加到买方的图书馆中,并将其从卖方的图书馆中删除,但我错过了一些东西。

我写的代码是或多或少:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <string.h> 
#include <unistd.h> 
#include <math.h> 

typedef struct { 
    char username [15]; 
    char email [25]; 
    char surname [20]; 
    char name [20]; 
    char sex; 
    int hours; 
    int level; 
    float credit; 
    char* library[20]; 
} User; 

typedef struct { 
    char id_code [7]; 
    char name [35]; 
    char developer [30]; 
    char category [15]; 
    float price; 
    int pegi; 
    char seller [15]; 
} Game; 

... 

int main() { 
    int max=1, z=0, u=0; 
    User signed_up [max]; 
    Game database [20], marketplace [20]; 
... 
system("pause"); 
return 0; 
} 

... 

void buy_game (int max, int *pz, int *pu, User signed_up[], Game database[], Game marketplace[]) { 
    int i=0, j=0, h=0, y=0, x=0, a=0, b=0, w=0, v=0, c=0; 
    bool game_found=false, user_found=false, not_owned_game=false; 
    char input_game [35], input_user[15]; 

    v=*pz; 
    w=*pu; 

    ... 

    printf("\n%*sInserisci username dell'utente con cui acquistare il gioco: ", 26, ""); 
    fflush(stdin); 
    scanf("%s", input_user); 
    printf("\n"); 

    for(i=0;i<max-1;i++) { 
     if (strcmp(input_user, signed_up[i].username)==0) { 
      user_found=true; 

      printf("%*sInserisci nome del gioco da acquistare: ", 26, ""); 
      fflush(stdin); 
      fgets(input_game, sizeof (input_game), stdin); 
      input_game[strlen(input_game)-1]='\0'; 
      printf("\n"); 

      for (j=0;j<w;j++) { 
       if (strcmp(input_game, marketplace[j].name)==0) { 
        game_found=true; 

        for (h=0;h<max-1;h++) { 
         if (strcmp(signed_up[h].username, marketplace[j].seller)==0) { 
          for (y=0;y<v;y++) { 
           if (strcmp(marketplace[j].name, signed_up[h].library[y])==0) { 
            for (a=0;a<v;a++) { 
             if (strcmp(marketplace[j].name, signed_up[i].library[a])!=0) { 
              not_owned_game=true; 

              if (marketplace[j].price<=signed_up[i].credit) { 

              printf("\n%*sOperazione avvenuta con successo!\n", 26, ""); 
              signed_up[i].credit=signed_up[i].credit-marketplace[j].price; 
              printf("\n%*sIl credito attuale dell'utente %s %c: EUR %.2f\n", 26, "", signed_up[i].username, 138, signed_up[i].credit); 
              signed_up[h].credit=signed_up[h].credit+marketplace[j].price; 
              printf("\n%*sIl credito attuale dell'utente %s %c: EUR %.2f\n\n\n", 26, "", signed_up[h].username, 138, signed_up[h].credit); 

              signed_up[i].library[y]=marketplace[j].name; 
              //add game to buyer's library 

              do { 
               signed_up[h].library[y]=signed_up[h].library[y+1]; 
               y+=1; 
              } while (y<v-1); 
              signed_up[h].library[v-1]=""; 
              //remove game from seller's library 

              do { 
               marketplace[j]=marketplace[j+1]; 
               j+=1; 
              } while (j<w-1); 
              *pu-=1; 
              //remove game from marketplace 
              } else { 
               printf("%*sCredito acquirente insufficiente! Si prega di ricaricare il conto.\n\n", 26, ""); 
              } 
             } 
            } if (not_owned_game==false) { 
             printf("%*sGioco inserito gi%c presente nella libreria dell'utente!\n\n\n", 26, "", 133); 
            } 
           } 
          } 
         } 
        } 
       } 
      } if (game_found==false) { 
       printf("%*sGioco inserito non presente nel marketplace!\n\n\n", 26, ""); 
      } 
     } 
    } if (user_found==false) { 
     printf("\n%*sUsername inserito non presente nel database!\n\n\n", 26, ""); 
    } 
} 

我的问题是程序有一个奇怪的行为,当我检查用户的图书馆事业不记住正确的买方和卖方的图书馆;实际上该计划拒绝在其图书馆展示任何游戏。

+0

您需要询问一个明确的问题 –

+0

“有些东西我错过了”。你错过了什么?你需要清楚地解释问题是什么。 – Barmar

+0

请使用比'h','v','pz','pu'等更有意义的变量名称。这个逻辑很难理解。 – Barmar

回答

-2

数组有固定大小,所以你不能删除条目。您可以覆盖,并且可以移动它们,其中任何一个都可能导致阵列中不再存在特定的值。

所以想想你想让你的固定大小的数组看起来像在逻辑上包含一个更少的项目,因为你实际上不能收缩它。然后考虑需要进行哪些更改以将阵列变为期望的状态,最后编写代码。

相关问题