I NEED HELP PLEAS
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <iostream>
using namespace std;
struct Player
{
int yearsPlaying;
float playerPay;
string playerLastname;
string playerAge;
string playerName;
char playerNumber[100];
};
const char fileName [] = "Players.txt";
void createFile(void);
void printPlayer(struct Player*pi);
void writePlayer(FILE *fp, struct Player *pi);
int readPlayer(FILE *fp, struct Player *pi);
void readFile(void);
int main(int argc, char* argv[])
{
int choice;
printf("Enter 1 to create a file or 2 to read a file: ");
scanf("%d",&choice);
switch (choice)
{
case 1 : createFile();
break;
case 2: readFile();
break;
default: printf("Not a valid choice\n");
break;
}
getchar();
getchar();
}
void createFile(void)
{
int count;
int i;
struct Player aPlayer;
FILE *fp;
fp = fopen(fileName, "wb");
if (fp != NULL)
{
printf("How many players? ");
scanf("%d", &count);
for (i = 0; i < count; ++i)
{
printf("player years of experience? ");
scanf("%d", &aPlayer.yearsPlaying);
printf("Player Pay? ");
scanf("%f", &aPlayer.playerPay);
getchar();
printf("Player Name? ");
scanf("%s", &aPlayer.playerName);
printf("Player Last Name? ");
scanf("%s", &aPlayer.playerLastname);
printf("Player Age?");
scanf("%s", &aPlayer.playerAge);
printf("player Number? \n");
gets(aPlayer.playerNumber);
writePlayer(fp, &aPlayer);
}
fclose(fp);
}
else
{
printf("\nError opening file.\n");
}
}
void readFile(void)
{
FILE *fp;
struct Player aPlayer;
fp = fopen(fileName, "r");
if (fp != NULL)
{
while (readPlayer(fp, &aPlayer))
{
printPlayer(&aPlayer);
}
fclose(fp);
} else
{
printf("\nError opening file.\n");
}
}
void writePlayer(FILE *fp, struct Player *pi)
{
fprintf(fp, " %d\n", pi-> yearsPlaying);
fprintf(fp, " %.2f\n", pi->playerPay);
fprintf(fp, " %s\n", pi->playerName);
fprintf(fp, " %s\n", pi->playerLastname);
fprintf(fp, "%s\n", pi->playerAge);
fprintf(fp, "%s\n", pi->playerNumber);
}
int readPlayer(FILE *fp, struct Player *pi)
{
int result;
result = fscanf(fp, "%d", &pi->yearsPlaying);
if (result == EOF)
{
return 0;
}
fscanf(fp, "%f", &pi->playerPay);
fgetc(fp);
fgets(pi->playerNumber,100, fp);
return 1;
}
void printPlayer(struct Player *pi)
{
printf("Player experience %d\n", pi->yearsPlaying);
printf("Player Pay $%.2f\n", pi->playerPay);
printf("player name: %s\n", pi->playerName);
printf("player Last Name: %s\n", pi->playerLastname);
printf("player age: %s\n", pi->playerAge);
printf("Player Number# %s\n", pi->playerNumber);
printf("\n\n");
}
Carlos_9 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.