PDA

View Full Version : Help w/ C Programming Assignment


youngfab59
04-15-2011, 10:13 PM
Hey everyone I really need help with my C programming assignment. I'm having trouble with my void Menu function getting my main function to read the inputs correctly. I've been trying for almost an hour and a half and still no luck with getting it to work. Also I need some help with my winOrLose function since its supposed to tell the user if they guessed correctly or if they used 4 guesses they lose. I really would appreciate any help. Here's my code

/*
Mario Gutierrez
COP 2220
Prof. Slavik
Assignment #4 - Scramble

*/

#define _CRT_SECURE_NO_DEPRECATE
//Maximum amount of guesses is 4
#define MAX_GUESSES 4
#define SIZE 20
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

//Gives the user a hint if the user is having trouble with the word
void Hint(char word[], char scramble[]);
//Reads the users word guess
void aGuess(char word[], char scramble[]);
//Determines if the user wins or loses
void WinOrLose(char word[], char scramble[]);
//Retrieve the scrambled and unscrambled word from the text file
void Words(FILE * fileptr, char word[], char scramble[]);
//Menu will appear and ask the user to enter a selection
void Menu();
//Plays an entire game of scramble
void Play(char word[], char scramble[], int *selection, char again);
//Ask the user if they want to play again
void Quit(char *again);
//If the user chooses "3" this will give the user the word and tell the user they lose.
void GiveUp(char word[], char again);

int main()
{
//variables
char again ='y';
char word[SIZE], scramble [SIZE];
int selection;
FILE *fileptr;
//Open the text file with the words
fileptr = fopen("words.txt", "r");
//Instructions
printf("Welcome to Scramble\n");
printf("You will be given a scrambled word which you will try to guess.\nYou will be given four chances to guess the word\nand you'll be able to get hints if you feel stumped.\nGood Luck!!\n\n");
Quit(&again);
//Will continue to run the game if user chooses (Y)
while (again == 'y' || again == 'Y')
{
Words(fileptr, word, scramble);
Menu();
Play(word, scramble, &selection, again);
Quit(&again);
}
return 0;
}

void Words(FILE *fileptr, char word[], char scramble[])
{
int i;
char sSize, wSize;
fscanf(fileptr, "%s %s", word, scramble);
sSize = strlen(scramble);
wSize = strlen(word);
for (i=0; i<sSize; i++)
{
scramble[i] = tolower(scramble[i]); //change the letters in the scrambled word to lowercase
}
for (i=0; i<wSize; i++)
{
word[i] = tolower(word[i]); //change the letters in the actual word to lowercase
}
printf("Here's the scrambled word: %s\n\n", scramble);
return;
}

void Menu(int *selection)
{
selection = 0;
printf("Select one of the following:\n");
printf("1 - Enter the entire word\n");
printf("2 - Get a hint before you guess\n");
printf("3 - I give up, show me the answer\n");
scanf(" %d", &selection);
return;
}

void Play(char word[], char scramble[], int *selection, char again)
{
if (*selection == 1)
{
aGuess(word,scramble);
}
else if (*selection == 2)
{
Hint(word, scramble);
}
else
{
GiveUp(word, again);
}
}

void aGuess(char word[], char scramble[])
{
char guess[SIZE];
int Guesses;
Guesses = 0;
printf("Enter your guess: ", guess);
while (Guesses <= MAX_GUESSES)
{

if (strcmp (guess, word) == 0)
{
Guesses += 0;
}
else
{
Guesses += 1;
}

}

}

void Hint(char word[], char scramble[])
{
int i; int length; char letter;
printf("Which letter do you need help with?: ");
scanf(" %c", &letter);
length = strlen (word);
printf("\nThis is where %c is located in the word\n", letter);
for (i=0; i<length; i++)
{
if (word[i] == letter)
{
printf("%c", letter);
}
else
{
printf("*");
}


}
printf("\n\n");
}

void WinOrLose(char word[], char scramble[])
{
int win;
win = 0;
while (win == 0)
{
strcmp(word, scramble);
}
}

void Quit(char *again)
{
printf("Do you want to play? ");
scanf(" %c", &again);
}

void GiveUp(char word[], char again)
{
printf("The word you were trying to guess was: %s", word);
Quit(&again);
}

KiLLeR2001
04-15-2011, 10:37 PM
First problem I see is you have a Menu(); yet your function is calling an int pointer based on how you set it up "void Menu(int *selection)" instead you need to send the selection value to the Play function.

edit: I actually see a handful of problems but I don't even know where to begin X_X