gpt4 book ai didi

C编程: A Simple Countdown Program

转载 作者:行者123 更新时间:2023-11-30 15:09:34 24 4
gpt4 key购买 nike

我正在尝试创建一个与倒计时电视节目相关的简单程序。我才刚刚开始,所以我的问题从一开始就存在。当我要求用户输入 c 或 v 作为辅音或元音时,我只是检查它是否返回 yes、no 或请重试。这有效,但它直接重复自身,返回请重试。我觉得这是一个简单的问题,但我不知道它是什么。

#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;
}

最佳答案

在我看来,您没有考虑输入中的换行符,并且它也通过您的子例程发送。添加 else if 子句来测试 \n,并进行相应处理。

上面的 C 还有多个其他问题,但据我所知,这是与该问题相关的问题。

关于C编程: A Simple Countdown Program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36604684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com