Question

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 (int)solution; A sample of an input file that you can use to test your program is included with the assignment. The player will enter a number between (65 and 90- the numerical ASCII values for capital letters A - Z) . . o YOU CAN ASSUME THE USER WILL ENTER A NUMBER BETWEEN 65 and 90 You will let the user know how close their guess is to the solution and print the solution letter onto the screen You will ask the user if they want to try again with a new letter, if yes get another letter from the file, if no, end the program * . You must have at least 4 user defined functions as follows: o No modifications may be made to the functions #define #include #include #include CRT. SECURE <stdio.h> <ctype.h> <stdlib.h> NO WARNINGS - - - //this function provides instructions to the user on how to play the game void GameRules ); //this function runs one game. //input: character from the file, void return type //all other functions to Play one round of a game //are called from within the PlayTheGame function void PlayTheGame (char solution); //this function prompts the player to make a guess and returns that guess //this function is called from inside the PlayTheGame() function described above int GetGuess(); //this function takes two arguments, the guess from the player //and the solution letter from the file. //This function lets the user know if how far their guess is from the solution and //also prints the solution onto the screen void CompareGuessAndSolution(int guess, char solution); dditional Requirements: Use function prototypes Write comments for each function that will appear in the file before each prototype and again before each function definition. . . . Be sure to comment your code adequately. . Be sure to indent properly. Check your textbook and lecture code examples to see how it should be done Use meaningful variable names

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

ans.c

/*
Copy the below code in C and save this file as ans.c
And run it using your compiler

*/


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>                // needed to use toupper()
#include <stdlib.h>

//this function provides instructions to the user on how to play the game
void GameRules();

//this function runs one game
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the PlayTheGame function
void PlayTheGame(char solution);

//this function prompts the player to make a guess and returns the guess
//this function is called from inside the PlayTheGame() function described above
int GetGuess();

//this function takes two arguments, the guess from the Player
//and the solution letter from the file
//This function lets the user know if how far their guess is from the solution and
//also prints the solution onto the screen
void CompareGuessAndSolution(int guess,char solution);

int main(){

    // show the game rules
    GameRules();

    // creating a FILE variable
    FILE *fptr;

    // creating a character variable
    char solution;
  
    // open the file in read mode
    fptr = fopen("letters.txt", "r");
  

    //if to_contine is 1 then game will be played
    int to_continue;
    do{
        if((solution = getc(fptr)) != EOF){
            //convert the read character to uppercase
            solution = toupper(solution);
            //call the PlayTheGame with solution character read from the file  
            PlayTheGame(solution);
        }
        printf("Would you like to try again with a new letter(1 for yes, 0 for no)? ");
        scanf("%d",&to_continue);
        printf("\n\n");
    }while(to_continue==1);  

    //close the file
    fclose(fptr);
}

void GameRules(){
    printf("Welcome to the Letter/number Game\n\n");
    printf("The goal is to try to guess the numerical value of a secret solution Letter \n");
    printf("You will be asked to enter a number between(65 and 90),\n");
    printf("------ the numerical values for uppercase letters A through Z\n\n");
    printf("You will be notified how close you were to the solution\n\n");
    printf("You will be able to continue to try again, each time with a new secret letter\n");
    printf("When you are ready to stop, you will enter the number (0) when prompted\n\n");
    printf("Let's begin:\n\n");
}

void PlayTheGame(char solution){
    //call GetGuess to get the input from the user
    int guess = GetGuess();

    //call CompareGuessAndSolution to check the correctness of guess and solutin
    CompareGuessAndSolution(guess,solution);
}

int GetGuess(){
    int guess;
    printf("Enter a number between 65 and 90: ");
    scanf("%d",&guess);
    printf("\n");
    //return the guess value
    return guess;
}

void CompareGuessAndSolution(int guess,char solution){
    //convert solution to get the ascii value
    int numberSolution = (int)solution;
  
    //calculate the difference in guess and numberSolution
    int difference = guess - numberSolution;

    printf("You were off by %d, the letter is %c, the ASCII value is %d\n\n",abs(difference),solution,numberSolution );
    //if difference is >0 then guess is high
    if(difference>0){
        printf("You Guessed too high\n\n");
    }
    //if difference is <0 then guess is low
    else if(difference<0){
        printf("You Guessed too low\n\n");
    }
    //if difference is 0 then guessed correctly
    else if(difference==0){
        printf("WOW! you guessed it!\n\n");
    }
}

letters.txt for this input test is:

hSZasfdfSsfdGSsAGHKLJSDFSasdDGFGSDsfdDFSFdfsa

image of letters.txt is:

code image is given below:

sample output image is given below:

If have any doubt in program feel free to ask in the comment section.

Add a comment
Know the answer?
Add Answer to:
General Requirements . . . Write a program that reads letters from a file called "letters.txt"....
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
  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • 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...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • Write a program that reads a string from the keyboard and computes the two arrays of...

    Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format: The frequency of the letter A= The frequency of the letter B= Assume the input string contains only...

  • 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...

  • Create a Python script file called hw.py. Then import the module random: from random import *...

    Create a Python script file called hw.py. Then import the module random: from random import * Thanks in advance! Ex. 1. Write a function called cleanLowerWord that receives a string as a paramcter and retums a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this,...

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