arrays - 代码在为彼此切换字母时出错
问题描述
当我尝试进行简单的字符交换时,代码出现故障。下面列出了代码以及错误的屏幕截图。
#include <stdio.h>
int main()
{
char Ghost[12] = "SimonGhost";
int ghostdeathdate = 5;
char Price;
int Soap;
while (ghostdeathdate > 0)
{
scanf("%c %d", &Price, &Soap);
Ghost[Soap] = Price;
printf("%s\n", Ghost);
--ghostdeathdate;
}
return ghostdeathdate;
}
解决方案
问题是每次调用时缓冲区中留下的新行scanf
,您需要使用空格来消耗每个新行:
scanf("%c %d", &Price, &Soap);
应该
scanf(" %c %d", &Price, &Soap); // Notice a space before %c
推荐阅读
- r - 将 R for 循环转换为应用函数
- python - 我将如何矢量化 pandas 中返回列表的函数?
- jquery - 如何在 Bootstrap 4 中从 Bootstrap 3 编写可折叠导航栏?
- c - 使用命名信号量在进程之间进行同步
- docker - Redmine cron 作业在 docker 中获取电子邮件 (redmine:email:receive_imap)
- python-3.x - 绘制不同 CNN 分类器的多重验证的准确性
- javascript - 如何测试承诺是否返回正确的值
- spring - 如何捕获来自 POST 的值以执行存储在 Spring Boot 中的过程
- c# - 如何正确读取 C# 中 .Any 函数的方法签名
- python - 摄氏到华氏方法不起作用