Question

I know this is wrong but I have no clue where to go from here. The...

I know this is wrong but I have no clue where to go from here. The difficulty is from 0-2. I have files for the game board labeled "10.txt" to "39.txt". This function needs to randomize which of the 10 boards is chosen then returns the open file address.

FILE* open_game_file(int difficulty) {

   game_choice(difficulty);
   difficulty += 1;
   difficulty *= 10;
   int i;
   i = rand() % 10;
       i += difficulty;
       infile = fopen("", "r");
   return 0;
}

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

I'm assuming you used C for this. Here is the complete code for operating the function. Explanation's in the comments above each line.

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

FILE* open_game_file(int difficulty){
//game_choice(difficulty); //some function ???
difficulty+=1;
difficulty*=10;
//seed it with time
srand (time(NULL));

int i=rand()%10;
i+=difficulty;
FILE *infile;

//create a string pointer with some dummy data, but right format
char fname[7]="xy.txt";
//replace the 0th index of the fname char array with tens place digit of i
fname[0]=(i/10)+48;
//replace the 0th index of the fname char array with ones place digit of i
fname[1]=(i%10)+48;
//+48 to convert into ASCII characters for digits (48-57)
//check the value of fname out by printing, to see if it's OK
printf("%s opened",fname);
//initialize the file pointer with fname. (Mode has been set to w to show you it can write to any valid file)
infile=fopen(fname,"w");
//return that file pointer
return infile;

}

int main(){
//declare and initialize a file pointer with the function
FILE *game=open_game_file(2);
//Just show that you can write to a valid file pointed to by our pointer. You can do whatever you want regarding to the file now that you have the pointer.
fprintf(game,"Hello Gameboard");
//properly close it
fclose(game);
}

Output in terminal:

36.txt opened

In 36.txt

Hello Gameboard

Add a comment
Know the answer?
Add Answer to:
I know this is wrong but I have no clue where to go from here. The...
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
  • Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am...

    Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am lost on how to complete the main function (play_card_match) without the sub functions complete. The program runs fine as is, but does not actually have a working turn system. It should display question marks on unflipped cards when the player is taking a turn and should also clear the screen so the player can't scroll up to cheat. Thank you #include "cardMatch.h" //main function /*...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • Question 8) Suppose a program is supposed to merge two files in the following manner: write...

    Question 8) Suppose a program is supposed to merge two files in the following manner: write two lines from the first file, write two lines from the second file, and repeat the process until all of the lines have been written to the new merged file. For example suppose we have the following two input files: File1.txt File2.txt AAA BBB CCC DDD 000 111 222 333 Then the merged output file would be: File3.txt AAA BBB 000 111 CCC DDD...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify m...

    This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify my code if anything return statement seems misplaced or inaccurately called. Here is my code: int write_sudoku_board(const char file_name[ ], int board[9][9]) { int number; int i,j; int a = 9; FILE* fp = fopen("sudoku.txt", "w"); int count = 0; for(i =...

  • This question has been asked before but the responses have been wrong. Please do not copy...

    This question has been asked before but the responses have been wrong. Please do not copy and paste their answers. Currently the program opens a file and reads every byte in the file and write both the ASCII hex value for that byte as well as it’s printable character to standard output with non-printable characters printing a "." Now, I want have an option where the program prints in binary instead of hex by typing "-b" at the command line....

  • I need help finding what is wrong with this code, it is for a CS course...

    I need help finding what is wrong with this code, it is for a CS course I am taking which codes in C (I am using XCode on Mac to code). This is the assignment: "Write a program that performs character processing on 10 characters read in from a file, and writes the results to output files. The program should read from “input.dat”. The program should write the ASCII values of the characters to “output_ascii.dat”. The program should print statistics...

  • I need help with the following code... Instructions: This lab builds on the skills from Lab...

    I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...

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