Home > Back-end >  C language had something wrong with small projects, every brother please give directions
C language had something wrong with small projects, every brother please give directions

Time:11-20

Problem is mainly in the purchase of goods, I am number one player to directly purchase goods number three, why gold is enough to show a 0, and the quantity also reduced the
Please answer, thank you


# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; String. H>
/* */commodity structure
Typedef struct _prop {
Int id;//item no.
Char name [50].//item name
Double price.//items unit price
Int stock;//inventory
Char desc [200].//item description
} Prop;
/* */backpack structure
Typedef struct _bag {
Int playerId;//player number
Int count;//in the backpack item number
Int Max;//can accommodate the total number of props
Prop props [];//props array
} Bag;
/* * player structure/
Typedef struct _player {
Int id;//player number
Char name [50].//user name
Char pass [50];//password
Bag bags;//players backpacks
Double gold;//player gold
Double sysee;//wing number
} Player;

Prop * props;
Player * players;
Int propsCount playersCount;

Void ShowProps ();
Void ShowPlayers ();
Void Init ();
Void Trade ();

Int main ()
{
The Init ();
Printf (" \ n trading before * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
ShowProps ();
ShowPlayers ();
Trade (& amp; Players [0], 3);
Printf (" \ n \ n trading after * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
ShowProps ();
ShowPlayers ();

return 0;
}


Void Trade (Player * Player, int propId)
{
int i;
Prop * tradeProp=NULL;
for(i=0; i {
If (propId==props [I] id)
{
TradeProp=& amp; Props [I];
break;
}
}
If (tradeProp - & gt; Stock & lt;=0)
{
Printf (" stores have been short!" );
return;
}
If (player - & gt; Gold & lt; TradeProp - & gt; Price)
{
Printf (" the purse is empty!" );
return;
}
If (player - & gt; Bags. Count & gt;=player - & gt; Bags. Max & amp; & Player - & gt; Bags. The count!=0)
{
Printf (" backpack is full, transaction failed!" );
}
TradeProp - & gt; Stock -;
Players - & gt; Gold -=tradeProp - & gt; Price;
for(i=0; i {
If (propId==player - & gt; Bags. The props [I]. Id)
{
Player - & gt; Bags. The props [I] stock++;
break;
}
}
If (I==player - & gt; Bags. The count)
{
Int newIndex=player - & gt; Bags. The count;
Player - & gt; Bags. Props [newIndex]. Id=tradeProp - & gt; Id;
Player - & gt; Bags. Props [newIndex]. Price=tradeProp - & gt; Price;
Player - & gt; Bags. Props [newIndex]. Stock=1;
Strcpy (player - & gt; Bags. Props [newIndex]. Name, tradeProp - & gt; Name);
Strcpy (player - & gt; Bags. Props [newIndex]. Desc, tradeProp - & gt; Desc);
Player - & gt; Bags. Count++;
}

}

Void Init ()
{
//1 - initialized data
The static Prop the propArray []={
{1, "experience card", 3000, 10, double click on the "666"},
{2, "god knife cut", 113000, 2, "nb"},
{3, "shadow service", 8000, 8, "couldn't see my"},
{4, "hydra", 10000, 3, "scattering"},
{5, "light armor", 9000, "hard defense"},
};
PropsCount=sizeof (the propArray)/sizeof (Prop);
Props=the propArray;
The static Player playerArray []={
{1, "a few things", "123456", the gold=10000, the bags. The Max=8},
{2, "boring", "123456", the gold=100000, the bags. The Max=8},
{3, "first", "123456", the gold=150000, the bags. The Max=8}
};
PlayersCount=sizeof (playerArray)/sizeof (Player);
Players=playerArray;
}
Void ShowProps ()
{
int i;
If (props==NULL) return;
Printf (" number name \ \ t t unit price goods inventory \ \ t t introduce \ n ");
for(i=0; i {
Printf (" % s \ \ % 5 d t t t % d % 7.0 lf \ \ t % s \ n ", props [I] id, props [I]. Name, props [I] price, props [I]. Stock, props [I] desc);
}
}
Void ShowPlayers ()
{
int i;
If (players==NULL) return;
Printf (" \ t % - 14 s COINS \ n ", "name");
for(i=0; i {
Printf (" % d \ t % - 14 s % 0 lf \ n ", the players [I] id, players [I]. Name, players [I] gold);
}

}

CodePudding user response:

 Prop props [];//props array 

Problem here, didn't the Init application space not assignment, props is a pointer, here in the Init value is NULL, no point to what,

CodePudding user response:

After the purchase, print it out again, part of the goods without error, is the player's gold

CodePudding user response:




This is not the point to the defined array
  • Related