简体   繁体   中英

C Programming: A Simple Countdown Program

I'm attempting to create a simple program related to the Countdown TV show. I've only started it so my problem is from the beginning. When i ask the user to enter c or v for a consonant or a vowel, I'm just checking if it returns yes, no or please try again. This works but it directly repeats itself, with the return of the please try again. I feel like it's a simple issue but I don't know what it is.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int test(int count, char lett);

int main()
{
    int i, counter;
    char letter;
    //char *fileName = "webster.txt";

    printf("Welcome to Countdown!\n\n");
    printf("The objective of the game is to produce the largest word from the nine letter(consonants and/or vowels) that are chosen at random by you. ");
    printf("The computer will then find the longest word available from these letters to compare with your word.\n");
    printf("Let's begin!\n");

    counter = 0;

    while (counter < 9) {
        printf("\nWould you like a consonant or a vowel(Enter either c or v)? ");
        scanf_s("%c", &letter);

        test(counter, letter);

    }

    return 0;
}

int test(int count, char letter)
{
    if (letter == 'c') {
    printf("yes\n");
    count++;
    }
    else if (letter == 'v') {
        printf("no\n");
        count++;
    }
    else {
        printf("Please try again\n");
    }

    return count, letter;
}

It would seem to me that you are not accounting for the new line character on your input, and it is getting sent through your subroutine as well. Add an else if clause to test for \\n , and deal with it accordingly.

There are multiple other issues with your C above, but that is the one related to the question, as far as I can tell.

NUIGProbz

You need to basically create an array of 9 letters, initialize to NULL, create arr of v and c, have the user pick their letters until the 9 letter array is full, print out the letters, have the user enter a word, cross ref the dictionary, and if the word exists, print out their score.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM