Thread: Program Help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    26

    Program Help

    Hi,
    I was wondering if I could get some help on slighly modifying a code. For my C class we have to find a code online and try to modify it with better features, but try to preserve the same structure. I found this one for a wheel of fortune game and I was wondering if someone could help me fix it and make it do the following things.
    1) Open a text file and randomly get a line containing a clue for the word and the word itself
    example: in text file will be
    Food % Hot Fudge Sundae
    Person% Ghandi
    (i think I need to use string tokens somehow)
    2) randomly select which player will go first
    If you have any other suggestions it would be great. This assignment isn't due for a week, but I don't want to be doing it last minute.

    Code:
    #define PHRASE_SIZE 81
    #define NUM_PLAYERS 3
    #define BANKRUPT 0
    #define LOSE_TURN -1
    #define VOWEL_COST 250
    
    /***************************************/
    /* to_upper */
    /***************************************/
    char to_upper(char aLtr)
    {
    char ltr = aLtr;
    if (ltr >= 'a' && ltr <= 'z')
    {
    ltr = (char)((int)ltr - 32);
    }
    return ltr;
    }
    
    /***************************************/
    /* spin_wheel */
    /***************************************/
    int spin_wheel()
    {
    int wheel[WHEEL_SIZE] = {50, 100, 150, 200, 250, 300, 350, 400, 450, 
    500, 1000, 1500, 2500, BANKRUPT, LOSE_TURN};
    int spin = (int)(((float)rand() / RAND_MAX) * WHEEL_SIZE);
    return wheel[spin];
    }
    
    /***************************************/
    /* isvowel */
    /***************************************/
    int isvowel(char guess)
    {
    int istrue = 0;
    if (guess == 'a' || guess == 'A' ||
    guess == 'e' || guess == 'E' ||
    guess == 'i' || guess == 'I' ||
    guess == 'o' || guess == 'O' ||
    guess == 'u' || guess == 'U')
    {
    istrue = 1;
    }
    return istrue;
    }
    
    /***************************************/
    /* print_puzzle */
    /***************************************/
    void print_puzzle (int size, char puzzle[])
    {
    int i;
    printf("===============================\n");
    printf(" Puzzle\n");
    printf("===============================\n\n"); 
    for (i = 0; i < size; ++i)
    {
    printf("%c",puzzle[i]);
    }
    printf("\n\n===============================\n\n");
    }
    
    /***************************************/
    /* print_score */
    /***************************************/
    void print_score(int players, int score[])
    {
    int p;
    printf("\n");
    for (p = 0; p < players; ++p)
    {
    printf("Player %d ", p+1);
    }
    printf("\n");
    for (p = 0; p < players; ++p)
    {
    printf("======== ");
    }
    printf("\n");
    for (p = 0; p < players; ++p)
    {
    printf(" %d ", abs(score[p]));
    }
    printf("\n\n");
    }
    
    /***************************************/
    /* spin */
    /***************************************/
    int spin(int player, int scores[ ], char phrase[ ], char puzzle[ ], int size)
    {
    int player_continues = 1;
    int spin_value;
    printf("You chose to spin the wheel!\n\n");
    spin_value = spin_wheel();
    if (spin_value == -1) /* lose turn */
    {
    printf("Sorry Player %d, you spun LOSE A TURN\n", player+1);
    player_continues = 0;
    }
    else if (spin_value == 0) /* bankrupt */
    {
    printf("Sorry Player %d, you spun BANKRUPT\n", player+1);
    scores[player] = 0;
    player_continues = 0;
    }
    else
    {
    char ltr = ' ';
    int i;
    int cnt = 0;
    printf("You spun %d\n\n", spin_value);
    
    /* Get the consonant */
    while (ltr < 'B' || ltr > 'Z')
    {
    printf("Enter a letter ->");
    scanf("%c", &ltr);
    printf("\n\n");
    ltr = to_upper(ltr);
    if (isvowel(ltr))
    {
    printf("Incorrect input. You cannot pick a vowel when you've chosen to spin\n\n");
    }
    }
    
    /* Check the consonant against the string */
    for (i = 0; i < size; ++i)
    {
    if (phrase[i] == ltr)
    {
    if (puzzle[i] == ltr) /* already revealed */
    {
    printf("Sorry, %c has already been revealed",ltr);
    cnt = -1;
    break;
    }
    else {
    ++cnt;
    puzzle[i] = phrase[i];
    }
    }
    }
    if (cnt > 0)
    {
    int value = cnt * spin_value;
    printf("Congratulations player %d!\n\n",player+1);
    printf("There are %d %cs in the puzzle.\n\n", cnt, ltr);
    printf("Your current score is %d.\n\n",value);
    scores[player] += value;
    printf("Your total score is %d.\n\n", scores[player]);
    }
    else if (cnt == 0)
    {
    printf("Sorry, there aren't any %cs in the puzzle.\n\n",ltr);
    player_continues = 0;
    }
    }
    return player_continues;
    }
    
    /***************************************/
    /* buy_vowel */
    /***************************************/
    int buy_vowel(int player, int scores[], char phrase[], char puzzle[], int size)
    {
    int player_continues = 0;
    printf("You chose to buy a vowel!\n\n");
    if (scores[player] < VOWEL_COST) {
    printf("Sorry, you don't have enough money yet.\n\n");
    player_continues = 1;
    }
    else {
    char ltr = ' ';
    int i;
    int cnt = 0;
    while (isvowel(ltr) == 0) {
    printf("Player %d, pick a vowel -> ",player+1);
    scanf("%c", &ltr);
    ltr = to_upper(ltr);
    printf("\n\n");
    }
    
    for (i = 0; i < size; ++i)
    {
    if (phrase[i] == ltr)
    {
    if (puzzle[i] == ltr) /* already revealed */
    {
    printf("Sorry, %c has already been revealed",ltr);
    cnt = -1;
    player_continues = 0;
    break;
    }
    else {
    ++cnt;
    puzzle[i] = phrase[i];
    }
    }
    }
    scores[player] -= VOWEL_COST;
    if (cnt > 0)
    {
    printf("Congratulations player %d!\n\n",player+1);
    printf("There are %d %cs in the puzzle.\n\n", cnt, ltr);
    player_continues = 1;
    }
    else if (cnt == 0)
    {
    printf("Sorry, there aren't any %cs in the puzzle.\n\n",ltr);
    player_continues = 0;
    }
    printf("Total cost for your guess is %d.\n\n",VOWEL_COST);
    printf("Your total score is %d.\n\n", scores[player]);
    }
    return player_continues;
    }
    
    /***************************************/
    /* solve */
    /***************************************/
    int solve(int player, int score[], char phrase[], int size)
    {
    char soln[256];
    int i;
    int solved = 1;
    printf("You chose to solve the puzzle!\n\n");
    printf("DIRECTIONS: Type in your solution on one line. Separate each word with one space.\n\n"); 
    printf("Solution -> ");
    fflush(stdin);
    gets(soln);
    printf("\n\n");
    for (i = 0; i < size; ++i)
    {
    soln[i] = to_upper(soln[i]);
    if (soln[i] != phrase[i]) 
    {
    solved = 0;
    break;
    }
    }
    if (solved == 1) 
    {
    printf("Congratulations Player %d!\n\n",player+1);
    print_puzzle(size, phrase);
    printf("You won the round!!!! Your final score is %d\n\n",score[player]);
    printf("Thanks for playing Wheel of Fortune!!!\n\n");
    }
    else 
    {
    printf("Sorry Player %d\n\n",player+1);
    printf("Your solution does not match\n\n");
    }
    return solved;
    }
    
    /***************************************/
    /* player_turn */
    /***************************************/
    int player_turn(int player, int score[], char phrase[], char puzzle[], int size)
    {
    int solved = 0;
    char player_continues = 1;
    while (player_continues == 1)
    {
    char choice = ' ';
    print_puzzle(size, puzzle);
    printf("Player %d, what do you want to do?\n\n", player+1);
    
    while (choice != 'S' && choice != 'O' && choice != 'B') {
    printf("(S)pin, S(o)lve or (B)uy a vowel -> ");
    scanf("%c", &choice);
    choice = to_upper(choice);
    printf("\n");
    }
    if (choice == 'S')
    {
    player_continues = spin(player, score, phrase, puzzle, size);
    }
    else if (choice == 'O')
    {
    solved = solve(player, score, phrase, size);
    player_continues = 0;
    }
    else /* buy a vowel */
    {
    player_continues = buy_vowel(player, score, phrase, puzzle, size);
    }
    }
    return solved;
    }
    
    /***************************************/
    /* initialize_array */
    /***************************************/
    int initialize_array(char phrase[], char puzzle[])
    {
    FILE* phraseFile = NULL;
    char filename[256];
    int cnt = 0, x;
    char word[100];
    x=time(NULL);
    while (!phraseFile) 
    {
    printf("Enter the name of the file containing the secret phrase -> ");
    scanf("%s", filename);
    // phraseFile = fopen("d:/test.txt", "r");
    phraseFile = fopen(filename, "r");
    if (!phraseFile) printf("\nSorry, I could not open %s, lets try again\n",filename);
    }
    while (!feof(phraseFile))
    {
    char c = ' ';
    fscanf(phraseFile, "%c", &c);
    if ((int)c < 15) break;
    if (c >= 'a' && c <= 'z') /* convert lower case to upper case */
    {
    c = to_upper(c);
    }
    phrase[cnt] = c; /* place the solution character into the array */
    if (c >= 'A' && c <= 'Z')
    {
    puzzle[cnt] = '*';
    }
    else /* it's not a letter */
    {
    puzzle[cnt] = c;
    }
    ++cnt;
    }
    return cnt;
    }
    
    /***************************************/
    /* main */
    /***************************************/
    int main(int argc, char* argv[])
    {
    char phrase[NAME_LENGTH];
    char puzzle[NAME_LENGTH];
    int puzzle_length;
    int num_players = 0;
    int solved = 0;
    int current_player;
    int player_scores[NUM_PLAYERS] = {0, 0, 0};
    
    printf("\n");
    printf("Over 50,000 in cash and prizes just waiting to be won!\n");
    printf(" Here on Wheel ... of ... Fortune!\n");
    printf(" (loud drum roll and music...)\n");
    printf("\n");
    
    /* Get the number of players */
    while (num_players < 1 || num_players > 3)
    {
    printf("\nHow many players will be playing the game (1-3)? ");
    scanf("%d",&num_players);
    printf("\n");
    }
    
    /* Get the puzzle */
    puzzle_length = initialize_array(phrase, puzzle);
    
    current_player = 0;
    
    while (solved == 0) {
    print_score(num_players, player_scores);
    solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
    solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
    solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
    
    if (solved == 0) 
    {
    ++current_player;
    if (current_player == num_players) 
    {
    current_player = 0;
    }
    }
    }
    
    return(0); 
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    First thing would probably be to indent where required, reading code like that is a nightmare.
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    ltr = (char)((int)ltr - 32)
    Can be written as :
    Code:
    ltr -= 32;
    or more portably:
    Code:
    char to_upper(char aLtr)
    {
        return toupper(aLtr);
    }
    (or just replace to_upper with toupper() in itself).

    And considering the to_upper() function is checking if it's lower case, do you really need it here too:
    Code:
    if (c >= 'a' && c <= 'z') /* convert lower case to upper case */
    {
    c = to_upper(c);
    }

    More comments in a bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    Thanks matsp for correcting that. Every little bit of help is appreciated. I really need help though with the getting the phrase and category randomly part though....Can someone please guide me on preserve, but fix this code to implement these features. Thank you.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should indent the code first, because it's unreadable in this state and that makes it dramatically more difficult to read it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    I hope this is better. I'm not using the best text editor right now...
    Code:
    #define PHRASE_SIZE 81
    #define NUM_PLAYERS 3
    #define BANKRUPT 0
    #define LOSE_TURN -1
    #define VOWEL_COST 250
    
    /***************************************/
    /* to_upper */
    /***************************************/
    char to_upper(char aLtr)
    {
    	char ltr = aLtr;
    	if (ltr >= 'a' && ltr <= 'z'){
    		ltr = (char)((int)ltr - 32);
    	}
    	return ltr;
    }
    
    /***************************************/
    /* spin_wheel */
    /***************************************/
    int spin_wheel()
    {
    	int wheel[WHEEL_SIZE] = {50, 100, 150, 200, 250, 300, 350, 400, 450, 
    			   	    500, 1000, 1500, 2500, BANKRUPT, LOSE_TURN};
    	int spin = (int)(((float)rand() / RAND_MAX) * WHEEL_SIZE);
    
    	return wheel[spin];
    }
    
    /***************************************/
    /* isvowel */
    /***************************************/
    int isvowel(char guess)
    {
    	int istrue = 0;
    		if (guess == 'a' || guess == 'A' ||
    		guess == 'e' || guess == 'E' ||
    		guess == 'i' || guess == 'I' ||
    		guess == 'o' || guess == 'O' ||
    		guess == 'u' || guess == 'U'){
    			istrue = 1;
    			}
    	return istrue;
    }
    
    /***************************************/
    /* print_puzzle */
    /***************************************/
    void print_puzzle (int size, char puzzle[])
    {
    	int i;
    	printf("===============================\n");
    	printf(" Puzzle\n");
    	printf("===============================\n\n"); 
    	for (i = 0; i < size; ++i){
    		printf("%c",puzzle[i]);
    		}
    	printf("\n\n===============================\n\n");
    }
    
    /***************************************/
    /* print_score */
    /***************************************/
    void print_score(int players, int score[])
    {
    	int p;
    	printf("\n");
    	for (p = 0; p < players; ++p){
    		printf("Player %d ", p+1);
    		}
    	
    	printf("\n");
    	
    	for (p = 0; p < players; ++p){
    		printf("======== ");
    		}
    	
    	printf("\n");
    
    	for (p = 0; p < players; ++p){
    		printf(" %d ", abs(score[p]));
    		}
    	
    	printf("\n\n");
    }
    
    /***************************************/
    /* spin */
    /***************************************/
    int spin(int player, int scores[ ], char phrase[ ], char puzzle[ ], int size)
    {
    	int player_continues = 1;
    	int spin_value;
    	printf("You chose to spin the wheel!\n\n");
    	spin_value = spin_wheel();
    	if (spin_value == -1) /* lose turn */{
    		printf("Sorry Player %d, you spun LOSE A TURN\n", player+1);
    		player_continues = 0;
    	}
    	else if (spin_value == 0) /* bankrupt */{
    		printf("Sorry Player %d, you spun BANKRUPT\n", player+1);
    		scores[player] = 0;
    		player_continues = 0;
    	}
    	else{
    		char ltr = ' ';
    		int i;
    		int cnt = 0;
    		printf("You spun %d\n\n", spin_value);
    
    		/* Get the consonant */
    		while (ltr < 'B' || ltr > 'Z'){
    			printf("Enter a letter ->");
    			scanf("%c", &ltr);
    			printf("\n\n");
    			ltr = to_upper(ltr);
    			if (isvowel(ltr)){
    				printf("Incorrect input. You cannot pick a vowel when you've chosen to spin\n\n");
    			}
    		}
    
    /* Check the consonant against the string */
    	for (i = 0; i < size; ++i){
    		if (phrase[i] == ltr)
    	{
    		if (puzzle[i] == ltr) /* already revealed */
    	{
    		printf("Sorry, %c has already been revealed",ltr);
    		cnt = -1;
    		break;
    	}
    		else {
    		++cnt;
    		puzzle[i] = phrase[i];
    	}
    	}
    	}	
    	if (cnt > 0){
    		int value = cnt * spin_value;
    		printf("Congratulations player %d!\n\n",player+1);
    		printf("There are %d %cs in the puzzle.\n\n", cnt, ltr);
    		printf("Your current score is %d.\n\n",value);
    		scores[player] += value;
    		printf("Your total score is %d.\n\n", scores[player]);
    	}
    	else if (cnt == 0){
    		printf("Sorry, there aren't any %cs in the puzzle.\n\n",ltr);
    		player_continues = 0;
    		}
    	}
    	return player_continues;
    }
    
    /***************************************/
    /* buy_vowel */
    /***************************************/
    int buy_vowel(int player, int scores[], char phrase[], char puzzle[], int size)
    {
    	int player_continues = 0;
    	printf("You chose to buy a vowel!\n\n");
    	if (scores[player] < VOWEL_COST) {
    		printf("Sorry, you don't have enough money yet.\n\n");
    		player_continues = 1;
    		}
    	else {
    		char ltr = ' ';
    		int i;
    		int cnt = 0;
    		while (isvowel(ltr) == 0) {
    		printf("Player %d, pick a vowel -> ",player+1);
    		scanf("%c", &ltr);
    		ltr = to_upper(ltr);
    		printf("\n\n");
    		}
    
    	for (i = 0; i < size; ++i){
    		if (phrase[i] == ltr){
    			if (puzzle[i] == ltr) /* already revealed */{
    				printf("Sorry, %c has already been revealed",ltr);
    				cnt = -1;
    				player_continues = 0;
    				break;
    			}
    		else {
    			++cnt;
    			puzzle[i] = phrase[i];
    			}
    		}
    	}
    	scores[player] -= VOWEL_COST;
    	if (cnt > 0)
    	{
    		printf("Congratulations player %d!\n\n",player+1);
    		printf("There are %d %cs in the puzzle.\n\n", cnt, ltr);
    		player_continues = 1;
    	}
    	else if (cnt == 0)
    	{
    		printf("Sorry, there aren't any %cs in the puzzle.\n\n",ltr);
    		player_continues = 0;
    	}
    		printf("Total cost for your guess is %d.\n\n",VOWEL_COST);
    		printf("Your total score is %d.\n\n", scores[player]);
    	}
    	return player_continues;
    }
    
    /***************************************/
    /* solve */
    /***************************************/
    int solve(int player, int score[], char phrase[], int size){
    	char soln[256];
    	int i;
    	int solved = 1;
    	printf("You chose to solve the puzzle!\n\n");
    	printf("DIRECTIONS: Type in your solution on one line. Separate each word with one space.\n\n"); 
    	printf("Solution -> ");
    	gets(soln);
    	printf("\n\n");
    
    	for (i = 0; i < size; ++i){
    		soln[i] = to_upper(soln[i]);
    		if (soln[i] != phrase[i]) {
    			solved = 0;
    			break;
    		}
    	}
    	if (solved == 1) {
    		printf("Congratulations Player %d!\n\n",player+1);
    		print_puzzle(size, phrase);
    		printf("You won the round!!!! Your final score is %d\n\n",score[player]);
    		printf("Thanks for playing Wheel of Fortune!!!\n\n");
    		}
    	else {
    		printf("Sorry Player %d\n\n",player+1);
    		printf("Your solution does not match\n\n");
    	}
    	return solved;
    }
    
    /***************************************/
    /* player_turn */
    /***************************************/
    int player_turn(int player, int score[], char phrase[], char puzzle[], int size){
    	int solved = 0;
    	char player_continues = 1;
    	
    	while (player_continues == 1){
    		char choice = ' ';
    		print_puzzle(size, puzzle);
    		printf("Player %d, what do you want to do?\n\n", player+1);
    		while (choice != 'S' && choice != 'O' && choice != 'B') {
    			printf("(S)pin, S(o)lve or (B)uy a vowel -> ");
    			scanf("%c", &choice);
    			choice = to_upper(choice);
    			printf("\n");
    			}
    		if (choice == 'S'){
    			player_continues = spin(player, score, phrase, puzzle, size);
    			}
    		else if (choice == 'O'){
    			solved = solve(player, score, phrase, size);
    			player_continues = 0;
    			}
    		else /* buy a vowel */{
    			player_continues = buy_vowel(player, score, phrase, puzzle, size);
    			}
    		}
    	return solved;
    }
    
    /***************************************/
    /* initialize_array */
    /***************************************/
    int initialize_array(char phrase[], char puzzle[]){
    	FILE* phraseFile = NULL;
    	char filename[256];
    	int cnt = 0;
    	char word[100];
    	
    	while (!phraseFile) {
    		printf("Enter the name of the file containing the secret phrase -> ");
    		scanf("%s", filename);
    	// phraseFile = fopen("d:/test.txt", "r");
    		phraseFile = fopen(filename, "r");
    		if (!phraseFile) printf("\nSorry, I could not open %s, lets try again\n",filename);
    		}
    
    	while (!feof(phraseFile)){
    		char c = ' ';
    		fscanf(phraseFile, "%c", &c);
    		if ((int)c < 15) break;
    		if (c >= 'a' && c <= 'z') /* convert lower case to upper case */{
    			c = to_upper(c);
    			}
    		phrase[cnt] = c; /* place the solution character into the array */
    		if (c >= 'A' && c <= 'Z'){
    			puzzle[cnt] = '*';
    			}
    		else /* it's not a letter */{
    			puzzle[cnt] = c;
    			}
    			++cnt;
    		}
    		return cnt;
    }
    
    /***************************************/
    /* main */
    /***************************************/
    int main(int argc, char* argv[]){
    	char phrase[NAME_LENGTH];
    	char puzzle[NAME_LENGTH];
    	int puzzle_length;
    	int num_players = 0;
    	int solved = 0;
    	int current_player;
    	int player_scores[NUM_PLAYERS] = {0, 0, 0};
    
    	printf("\n");
    	printf("Over 50,000 in cash and prizes just waiting to be won!\n");
    	printf(" Here on Wheel ... of ... Fortune!\n");
    	printf(" (loud drum roll and music...)\n");
    	printf("\n");
    
    	/* Get the number of players */
    	while (num_players < 1 || num_players > 3){
    		printf("\nHow many players will be playing the game (1-3)? ");
    		scanf("%d",&num_players);
    		printf("\n");
    		}
    
    /	* Get the puzzle */
    	puzzle_length = initialize_array(phrase, puzzle);
    
    	current_player = 0;
    
    	while (solved == 0) {
    		print_score(num_players, player_scores);
    		solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
    		solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
    		solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
    
    		if (solved == 0) {
    			++current_player;
    			if (current_player == num_players){
    				current_player = 0;
    			}
    		}
    	}
    
    		return(0); 
    }

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Yes, that is better. If you are using Windows, use Context:

    http://contexteditor.org/

    Easiest editor I've ever seen

    Also if it's an assignment, you probably aren't allowed to or it's probably not a good idea to post your whole code up. If someone copies you, you could be in trouble for plagiarism. Posting small parts of code is generally allowed. Taking a quick look you might to want srand a seed first based on time, or otherwise everytime you run it it will generate the same values until recompiled
    Last edited by JFonseka; 04-17-2008 at 03:14 PM.
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who needs editors these days? IDEs are far better, and in that sense, Visual Studio is one of the best.
    The indentation is much better, but to nitpicky, this part is still very messed up:

    Code:
    	for (i = 0; i < size; ++i){
    		if (phrase[i] == ltr)
    	{
    		if (puzzle[i] == ltr) /* already revealed */
    	{
    		printf("Sorry, %c has already been revealed",ltr);
    		cnt = -1;
    		break;
    	}
    		else {
    		++cnt;
    		puzzle[i] = phrase[i];
    	}
    	}
    	}
    It should be:
    Code:
    	for (i = 0; i < size; ++i){
    		if (phrase[i] == ltr)
    		{
    			if (puzzle[i] == ltr) /* already revealed */
    			{
    				printf("Sorry, %c has already been revealed",ltr);
    				cnt = -1;
    				break;
    			}
    			else {
    				++cnt;
    				puzzle[i] = phrase[i];
    			}
    		}
    	}
    As you see, it immedietely becomes easier to read. Indentation is important.
    You are also using two big flaws which are very prone to buffer overruns. Namely reading strings with scanf and gets.
    http://cpwiki.sf.net/Gets
    http://cpwiki.sf.net/Buffer_overruns
    Do take a look.
    You need to fix those.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    Could someone help me with converting the scanf to sscanf or whatever it needs to be converted to please. Especially in the spin function.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's wrong with it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM