Hi so I have recently been having a problem trying to open a file, that is entered by the user.
Here is the assignment I have to do:
Your friend has recently come into possession of an old treasure chest. They are convinced there is great treasure inside. They may not even need to go to the Caribbean to get treasure! The treasure chest is locked with seven locks and your friend has found 100 keys that might fit these locks. Your friend and their crew members will have to try different combinations of keys to try and get the treasure chest open. There’s one other thing. The treasure chest is haunted! The pirate who previously owned this treasure is still around, reminding other pirates that each lock has a unique key and letting them know if any of their current guesses are correct. Your program will need to read in a file of the correct combination of keys, numbered 1-100. Then, prompt the user for the seven numbers indicating the seven keys they wish to use. They cannot use the same key twice in one attempt. If they have all the correct keys in the exactly correct order, they can open the chest. If they have some correct keys, regardless of order, let the user know how many keys are correct. Input File Format The input file will contain 7 unique integers from 0 to 100. Program Specification You must use arrays to solve the problem. Your program should first prompt the user for the name of the input file. Then, your program should process the input file and copy the correct order of keys into the program. Then you can prompt the user for their first guess. If the user attempts to use a key more than once in a single guess, tell them they can only use each key once. If the user exactly matches the keys and the order, let them open the chest. If the user identifies some of the correct keys, let them know how many keys are correct but tell them they may not be in the right order.
And here is the code I currently have:
Code:
01 #include <stdio.h>
02 #include <string.h>
03 int main()
04 {
05 FILE * ifp;
06 char filename[20];
07 int i=0;
08 int keyval[7];
09 int key[7];
10 int counter;
11 printf("What is the name of the text file?\n");
12 gets(filename);
13 ifp = fopen(filename, "r");
14
15 if (ifp == NULL){
16 printf("Error the file could not be opened.\n");
17 }
18 else{
19 while(!feof(ifp)){
20 fscanf(ifp, "%d",keyval[i] );
21 printf("%d", keyval[i]);
22 i++;
23 }
24 printf("In order to get my treasure you'll have to figure out which of my 100 keys are used in the 7 locks!\n");
25 printf("Which keys will you use?");
26 for(counter=0; counter < 7; counter++){
27 scanf("%d", key[counter]);
28 }
29 if(keyval[1]= key[1] && keyval[2]= key[2] && keyval[3]= key[3] && keyval[4]=key[4] && keyval[5]=key[5] && keyval[6]=key[6] && keyval[7]=key[7]){
30 printf("You've opened my treasure and found a map to the rest of the treasure on the island! Haha!\n");
31 }
32 }
33 fclose(ifp);
34 return 0;
35 }
So for some reason when I try to open the file it is always NULL, and just prints the error message that I put in. The name of the file is keylist.txt. I could also use some advice on how to go about checking if any keys are matched and how to print out the number of matches. I'm also not sure how to check to see if they use the same key more than once. Any help would be appreciated, thanks!