【发布时间】:2016-08-04 22:04:56
【问题描述】:
我正在尝试创建一个与倒计时电视节目相关的简单程序。我才开始,所以我的问题是从一开始。当我要求用户输入 c 或 v 作为辅音或元音时,我只是在检查它是否返回是、否或请再试一次。这有效,但它直接重复自身,并返回请重试。我觉得这是一个简单的问题,但我不知道它是什么。
#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;
}
【问题讨论】:
-
修复代码缩进。
-
C 不是 Python,你不能返回元组。 (
count, letter将只返回letter。) -
不要包含您要告诉我们忽略的任何内容。如果它无关紧要,请从你的问题中删除它。
-
当您使用
scanf_s时,请确保为所有%s、%c和%[格式指定最大缓冲区计数:scanf_s("%c", &letter, 1);。
标签: c if-statement while-loop char scanf