我正在尝试开发一个简单的基于文本的刽子手游戏,并且主游戏循环以提示输入每个字母的猜测开始,然后继续检查字母是否在单词中并且如果它生命关闭不是.但是,当我运行游戏时,每次提示两次,程序不会等待用户的输入.它也会夺去生命(如果它是正确的输入就会有一个生命,如果没有,则为两个生命),所以无论它采取的是什么都与之前的输入不同.这是我的游戏循环,简化了一下:
while (!finished)
{
printf("Guess the word '%s'\n",covered);
scanf("%c", ¤tGuess);
i=0;
while (i<=wordLength)
{
if (i == wordLength)
{
--numLives;
printf("Number of lives: %i\n", numLives);
break;
} else if (currentGuess == secretWord[i]) {
covered[i] = secretWord[i];
secretWord[i] = '*';
break;
}
++i;
}
j=0;
while (j<=wordLength)
{
if (j == (wordLength)) {
finished = 1;
printf("Congratulations! You guessed the word!\n");
break;
} else {
if (covered[j] == '-') {
break;
}
}
++j;
if (numLives == 0) {
finished = …Run Code Online (Sandbox Code Playgroud) 我在C std99中使用bool数据类型,其定义在中定义<stdbool.h>.现在我希望用户给我输入.我必须在scanf中使用什么格式说明符来从用户输入1字节的布尔值,然后在我的程序中操作它.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,提示符要求我两次输入(我只scanf在代码中使用一次).这是为什么?
(我输入'jo';它要求更多输入,所以我再次输入'jo'.然后它只打印出'jo'一次.)
scanf(" %[^\n]",line);
Run Code Online (Sandbox Code Playgroud)
我的一个朋友建议使用fgets()读取一行作为输入比使用scanf()上面的声明更好.他有道理吗?
我正试图long long从控制台使用标准IO功能scanf.我开始时%lld:
scanf("%lld", &rule);
Run Code Online (Sandbox Code Playgroud)
抛出:
error: unknown conversion type character 'l' in format [-Werror=format=]
Run Code Online (Sandbox Code Playgroud)
我发现了更多变通方法,但它们也会抛出错误:
scanf("%I64d", &rule);
->error: ISO C does not support the 'I64' ms_scanf length modifier [-Werror=format=]
scanf("%"SCNd64"", &rule);
->error: expected ')' before 'SCNd64'
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?还有另外一招吗?
我正在使用这些标志编译最新版本的MinGw GCC: -pedantic -Wall -Werror -std=c99 -g -D HOME=1
我需要从文件中读取和打印数据.
我写的程序如下,
#include<stdio.h>
#include<conio.h>
int main(void)
{
char item[9], status;
FILE *fp;
if( (fp = fopen("D:\\Sample\\database.txt", "r+")) == NULL)
{
printf("No such file\n");
exit(1);
}
if (fp == NULL)
{
printf("Error Reading File\n");
}
while(fscanf(fp,"%s %c",item,&status) == 1)
{
printf("\n%s \t %c", item,status);
}
if(feof(fp))
{
puts("EOF");
}
else
{
puts("CAN NOT READ");
}
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
database.txt文件包含
Test1 A
Test2 B
Test3 C.
当我运行代码时,它会打印出来
无法阅读.
请帮我找出问题所在.
我的老师刚刚用c ++给了我一个作业,我试图用scanf得到一个字符串,但它只能输入最后输入的字符.有人可以帮我吗?我在c ++中寻找相当于console.readline()的东西.
编辑:我还必须能够通过指针存储值.
所以图片显示当前在后台运行的代码,它应该停止在无保证maladie:并等待输入,但它跳过它.
getline(cin,ptrav-> nam); 有效,但由于某种原因它跳过一条线......
我错误地使用scanf("%d\n",&val);了我的一个程序,我无法理解行为,功能显示.
int main(){
int val;
scanf("%d\n", &val);
printf("%d\n", val);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在程序需要2个整数输入,并打印输入的第一个输入.额外\n带来的不同之处是什么?
我试图搜索,但找不到答案,即使通过手册scanf.
我可以使用这种技术指定scanf要读取的最大字符数buffer:
char buffer[64];
/* Read one line of text to buffer. */
scanf("%63[^\n]", buffer);
Run Code Online (Sandbox Code Playgroud)
但是如果我们在编写代码时不知道缓冲区长度怎么办?如果它是函数的参数怎么办?
void function(FILE *file, size_t n, char buffer[n])
{
/* ... */
fscanf(file, "%[^\n]", buffer); /* WHAT NOW? */
}
Run Code Online (Sandbox Code Playgroud)
此代码容易受到缓冲区溢出的影响,因为fscanf不知道缓冲区有多大.
我记得以前见过这个,并开始认为这是解决问题的方法:
fscanf(file, "%*[^\n]", n, buffer);
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是,*in "%*[*^\n]"意味着最大字符串大小传递一个参数(在这种情况下n).这就是*in 的含义printf.
当我检查文档时,scanf我发现它意味着scanf应该丢弃结果[^\n].
这让我有些失望,因为我认为能够动态传递缓冲区大小是一个非常有用的功能scanf.
有没有办法可以scanf动态地传递缓冲区大小?
我有相当于以下代码:
const int* const n = new int;
printf("input: ");
scanf("%d", n);
delete n;
Run Code Online (Sandbox Code Playgroud)
现在,因为n是指向CONSTANT整数的指针,所以这不应该工作(我期待编译器错误).但是,这似乎可以正常工作,甚至可以将输入值存储到*n中.
我想知道,为什么这不会给我一个错误; 它为什么有效?扫描不能改变*n的值吗?