Question

Conditional statements, loops, reading from file, user defined functions.

# in C 


Project objective: Conditional statements, loops, reading from file, user defined functions.

**Submit source code (prog3.c) through Canvas

  • One source code file(unformatted text) will be submitted

  • Here is INCOMPLETE code to get started: prog3.c

  • Here is input.txt file: input.txt

    • The file name must match the assignment

    • The code should be tested and run on a Microsoft compiler before it is uploaded onto Canvas

    • The code must be submitted on time in order to receive credit (11:59PM on the due date)

    • Late submissions will not be accepted or graded

    • All programming assignments are individual work,sharing code is considered cheating

  • ASCII (American Standard Code for Information Interchange)

    You will need to use functions from:

    #include//printf, scanf, fopen, fscanf, fclose

    #include//toupper and tolower

    #include//absolute value

    //EXAMPLE: result = abs(number1 – number2);

    Be sure to save the input.txt file into the same directory as source code file (remember to use your Z drive on portal.eng.fau.edu)

How to Submit:

Submit your source code (prog3.c) by clicking the link, attaching your source code (prog3.c), and clicking submit.

This should be one file and MUST be the file with the .c extension. Submitting the incorrect file may result in no credit for the assignment.

Instructions:

Write a fully executable code.

  1. Include the following libraries in the preprocessor directives: stdio.h, ctype.h, stdlib.h

  2. Open the input file. Define steps 3-6 below in a function called PlayLetterGame. Remember to declare the function prototype and place the function call at the appropriate location in the main function.

  3. Read a letter from the input file

  4. Ask the user to guess the letter from the input file

  5. Calculate the distance of the user's guessed letter from the letter of the input file (hint: make both letters the same case (upper or lower) and use the ASCII codes of the letters). Ignore the case of the letters in assessing correctness of the guess (i.e. if the letter in the input file was a 'b' and the user's guess was a 'B' then the user guessed the letter).

  6. If the user guessed the letter, display a congratulatory message, if the user's guess was less than 3 letters off, display an encouraging message. In any case display the distance between the user's guessed letter and the letter from the input file and reveal the letter from the input file.

  7. Ask the user if (s)he wants to play again and if yes (and the input file is not read until the end) repeat steps 3.-6.

  8. Close the file.


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

Please find the answer below, I have mentioned all the details in the comments.

#include //printf, scanf, fopen, fscanf, fclose
#include //toupper and tolower
#include //absolute value
//EXAMPLE: result = abs(number1 – number2);

void PlayLetterGame(FILE *file){
//variables
char fromFile;
char guess;
//check if the file is valid
if(file!=NULL){
//read the char from file
if(fscanf(file,"%c",&fromFile) != EOF){
}
}

//ask the user to guess the letter
getchar();
printf("Guess the Letter form the file:");
scanf("%c",&guess);
printf("\nYour Guess: %c\n",guess);

//calculate the distance
int distance = abs(toupper(fromFile) - toupper(guess));

//print the message according to the distance
if(distance == 0){
printf("Congratulations!! You guessed it Correct.\n");
}
else if(distance<3){
printf("Ohh!! Very close to the correct Guess.\n");
}
else{
printf("Sorry! You guessed it wrong.\n");
}

//print the details for the user.
printf("Distance to the Letter from the file is: %d\n",distance);
getchar();
printf("Secret Letter from the File is: %c\n",fromFile);
}

int main(){
//get the file object
FILE *file;
//open the file
file = fopen("input.txt", "r");

//call the function to play the game
char choice;
while(1){
PlayLetterGame(file);
printf("Do you want to play again? Enter (Y/N): ");
//getchar();
scanf("%c",&choice);
if(toupper(choice) != 'Y')
break;
}

return 0;
}

Output:

Guess the Letter form the file:B Your Guess: E |Sorry! You guessed it wrong. Distance to the Letter from the file is: 4 Secre

Guess the Letter form the file:F Sorry! You guessed it wrong. Distance to the Letter from the file is: 5 Secret Letter from the File is: A Oco

Add a comment
Know the answer?
Add Answer to:
Conditional statements, loops, reading from file, user defined functions.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • SOLVE USING C!!! Project objective: Conditional statements, loops, reading from file, user defined functions. **Submit source...

    SOLVE USING C!!! Project objective: Conditional statements, loops, reading from file, user defined functions. **Submit source code (LargeProg1.c) through Canvas One source code file(unformatted text) will be submitted Here is INCOMPLETE code to get started: LargeProg1.c The file name must match the assignment The code should be tested and run on a Microsoft compiler before it is uploaded onto Canvas The code must be submitted on time in order to receive credit (11:59PM on the due date) Late submissions will...

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

  • Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1....

    Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...

  • a. Ask the user for the input of a letter (char type). Write a function that...

    a. Ask the user for the input of a letter (char type). Write a function that takes a char as the argument (parameter) and returns the ASCII value of that char back to the caller. Call the function using the char variable and taking input from the user (no validation required). Display the value in ASCII. b. Write a program that takes a char as the argument (parameter) and uses a loop to interrogate the char, calling a function that...

  • Hangman using while and fo loops, if's, and functions.

    I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.MORE INFO: Player 1 will enter word(of any length / i have been using "Testing") for Player 2...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Using Python INST-FS-IAD-PROD.INS LAB1 Lab: Create User Account 2. get-password() #promt the user and create password, check the password fits the requirement USE the EXACT file names! Create a user...

    Using Python INST-FS-IAD-PROD.INS LAB1 Lab: Create User Account 2. get-password() #promt the user and create password, check the password fits the requirement USE the EXACT file names! Create a user login system. Your code should do the following: 3, create-user_name() #use this function to create the user name 1.Create your user database called "UD.txt", this should be in CSV format 4, write-file() #user this function to save the user name and password into "UD.txt" Deliverables: Sample: the data you saved...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

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