Question

Help with my code: The code is suppose to read a text file and when u...

Help with my code:

The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file

#include <stdio.h>
#include <stdlib.h>

typedef struct KnightsBallLottoPlayer{

char firstName[20];
char lastName[20];
int numbers[6];
}KBLottoPlayer;

int main(){
//Declare Variables
FILE *fp;
int i,j,n,k;

fp = fopen("KnightsBall.in","r");
//
if(fp == NULL){
printf("File Unable to Open");
return 0;
} // End if statement

fscanf(fp,"%d",&n);
KBLottoPlayer *players = (KBLottoPlayer*) malloc(n*sizeof(KBLottoPlayer));

for(i=0; i<n; i++){
fscanf(fp, "%s %s", players[i].firstName, players[i].lastName);
for(j=0; j<6; j++)
fscanf(fp, "%d", &players[i].numbers[j]);
}
fclose(fp);

int lottoNumbers = (int)malloc(6*sizeof(int));

printf("Enter the Winning Lottery Numbers: ");
for(i=0; i<6; i++)
scanf("%d", &lottoNumbers);
fp = fopen("KnightsBall.out", "r");

for(i=0; i<n; i++){
int cnt = 0;

for (j=0; j<6; j++){
for(k=0; k<6; k++){
if(players[i].numbers[j] == lottoNumbers)
cnt++;
}
}
if(cnt <= 2)
continue;

fprintf(fp, "%s %s matched %d numbers and won $", players[i].lastName, players[i].firstName, cnt);

if(cnt==3)
fprintf(fp, "10");
else if(cnt==4)
fprintf(fp, "100");
else if(cnt==5)
fprintf(fp, "10000");
else if(cnt==6)
fprintf(fp, "1000000");
fprintf(fp, ". ");

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>
#include <stdlib.h>

typedef struct KnightsBallLottoPlayer{

char firstName[20];
char lastName[20];
int numbers[6];
}KBLottoPlayer;

int main(){
//Declare Variables
FILE *fp;
int i,j,n,k;

fp = fopen("KnightsBall.in","r");
//
if(fp == NULL){
printf("File Unable to Open");
return 0;
} // End if statement

fscanf(fp,"%d",&n);
KBLottoPlayer *players = (KBLottoPlayer*) malloc(n*sizeof(KBLottoPlayer));

for(i=0; i<n; i++){
fscanf(fp, "%s %s", players[i].firstName, players[i].lastName);
for(j=0; j<6; j++)
fscanf(fp, "%d", &players[i].numbers[j]);
}
fclose(fp);

int lottoNumbers = (int)malloc(6*sizeof(int));

printf("Enter the Winning Lottery Numbers: ");
for(i=0; i<6; i++)
scanf("%d", &lottoNumbers);
fp = fopen("KnightsBall.out", "w");

for(i=0; i<n; i++){
int cnt = 0;

for (j=0; j<6; j++){
for(k=0; k<6; k++){
if(players[i].numbers[j] == lottoNumbers)
cnt++;
}
}
if(cnt <= 2)
continue;

fprintf(fp, "%s %s matched %d numbers and won $", players[i].lastName, players[i].firstName, cnt);

printf("%s %s matched %d numbers and won $", players[i].lastName, players[i].firstName, cnt);

if(cnt==3){

fprintf(fp, "10");

printf("10");

}
else if(cnt==4){
fprintf(fp, "100");

printf("100");

}
else if(cnt==5){
fprintf(fp, "10000");

printf("10000");

}

else if(cnt==6){
fprintf(fp, "1000000");

printf("1000000");

}
fprintf(fp, ". ");

printf(".");

}

fclose(fp);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Help with my code: The code is suppose to read a text file and when u...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • create case 4 do Method 1:copy item by item and skip the item you want to...

    create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name. #include int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1:...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT