using c++ code
you will create a video game character management program.
We will be using a vector of structs as the basic data structure to manage our characters.
You will need to define a struct for your character with at least 5 fields. The fields
you choose should be relevent to characters in a game that you imagine. For example:
if you imagine a futuristic fish underwater rpg.. you may have a name field, a species field,
a attack type field, an HP field and a damage field among others.
Your project will also use basic file I/O with text files. Please download and
experiment with the fileIO.zip from doc share. It contains a basic file I/O example.
When your program begins you will ask the user if they would like to load data from a file,
enter it manually or generate it randomly. Entering it manually or generating it randomly
should be similar to project 3, however the data will now be stored in a vector of structs.
If you are reading your data in from a file, you should generate the vector of structs from
the inputted file data.
After your data is inputted, the user should be able to sort from any meaningful field. Your
user should also be able to select a single character to view, modify or delete. Your user
should also be able to add characters. The user should be able to continue to sort the characters
after the data has been changed.
When done the user should be able to save the data to a file to use next time.
Expert Answer
SOURCE CODE:
//Character of GTA
#include <iostream>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#include <time.h>
using namespace std;
struct rpg{
char name[50],weap[50];
int hp,ammo;
double money;
}a[50],lp[50];
void main()
{
srand (time(NULL));
int i=0;
int j,u,k,b,g;
char kl,jl;
char c=’y’;
fstream f;
f.open(“char”,ios::in|ios::binary|ios::app);
while(c==’y’)
{
cout<<“1. Load data 2. Write data 3. Random datan”;
cin>>g;
switch(g)
{
case 1:
{
while(f)
{
f.read((char*)&a[i],sizeof(rpg));
i++;
}
i–;
break;
}
case 2:
{
aman:
cout<<“Enter name,name of weapon,health points,ammo,moneyn”;
gets(a[i].name);gets(a[i].weap);cin>>a[i].hp>>a[i].ammo>>a[i].money;
break;
}
case 3:
{
for(u=0;u<10;u++)
{
k = rand() % (122-97+1) + 97;
kl=k;
j=rand() % (122-97+1) + 97;
jl=j;
a[i].name[u]=kl;
a[i].weap[u]=jl;
}
a[i].hp=rand()%100+1;
a[i].ammo=rand()%1000+1;
a[i].money=rand()%10000+1;
break;
}
default:
{
cout<<“Wrong choice”;
break;
}
}
cout<<“Do you want more character(y or n)”;
cin>>c;
i++;
}
int ql=0,v;
char gh[50];
int bb;
double cc;
int yon;
int am,an,d,ee;
int kk=0,ll;
char ch=’y’;
for(;ch==’y’;)
{
cout<<“1. View characters 2. Select to view, modify, details details 3. Add characters 4. Save”;
cin>>v;
switch(v)
{
case 1:
{
cout<<“nCharNumtNametNameofWeapontHealthPointstAmmotMoneyn”;
for(;ql<i;ql++)
{
cout<<“#”<<ql+1<<“. t”;
puts(a[ql].name);cout<<“tt”;
puts(a[ql].weap);cout<<“t”;
cout<<“ttt”<<a[ql].hp<<“tt”<<a[ql].ammo<<“t”<<a[ql].money<<“nn”;
}
break;
}
case 2:
{
clrscr();
cout<<“Enter character number”;
cin>>b;
b–;
cout<<“#”<<b+1<<“. t”;
puts(a[b].name);cout<<“tt”;
puts(a[b].weap);cout<<“t”;
cout<<“ttt”<<a[b].hp<<“tt”<<a[b].ammo<<“t”<<a[b].money<<“nn”;
cout<<“1. Modify 2.Delete”;
cin>>kk;
switch(kk)
{
case 1:
{
cout<<“What to modify 1. Name 2. Name of weapon 3. Health point 4. Ammo 5. Money”;
cin>>ll;
switch(ll)
{
case 1:
{
cout<<“Enter name”;
gets(gh);
strcpy(a[b].name,gh);
break;
}
case 2:
{
cout<<“Enter name of weapon”;
gets(gh);
strcpy(a[b].weap,gh);
break;
}
case 3:
{
cout<<“Enter HP”;
cin>>bb;
a[b].hp=bb;
break;
}
case 4:
{
cout<<“Enter Ammo”;
cin>>bb;
a[b].ammo=bb;
break;
}
case 5:
{
cout<<“Enter Money”;
cin>>cc;
a[b].money=cc;
break;
}
default:
{
cout<<“Wrong choice”;
break;
}
}
break;
}
case 2:
{
cout<<“Are you sure you want to delete 1 for yes 2 for no”;
cin>>yon;
if(yon==1)
{
for(am=0,an=0;an<i;am++,an++)
{
if(an==b){am–; continue;}
strcpy(lp[am].name,a[an].name);
strcpy(lp[am].weap,a[an].weap);
lp[am].ammo=a[an].ammo;
lp[am].money=a[an].money;
lp[am].hp=a[an].hp;
}
strcpy(a[i].name,”