我创建了一个程序,从用户输入中接收一个字符。它会将字母转换为大写并将其显示给最终用户。
但是,我收到输出的问题,我得到了两倍的"请输入一个小写的字符:"以及下面的额外一行。我不太确定是什么导致它,但这是我到目前为止的程序:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
// Converts lowercase letter to uppercase
int main()
{
bool continue_program = true;
printf("Program Running \n");
int lower, upper;
while (continue_program){
printf("Please enter a character in lower case:");
printf("\n");
int lower = getchar();
if(lower=='q' || lower=='Q'){
printf("Goodbye \n");
break;
} else {
upper = toupper(lower);
putchar(upper);
printf("\n");
}
}
return 0;
}
输出示例:
./a.out
Program Running
Please enter a character in lower case:
a
A
Please enter a character in lower case:
Please enter a character in lower case:
b
B
Please enter a character in lower case:
Please enter a character in lower case:
p
P
Please enter a character in lower case:
Please enter a character in lower case:
Q
Goodbye
答案 0 :(得分:2)
只需改变一下:
int lower, upper;
为:
char lower, upper;
和此:
int lower = getchar();
为:
scanf(" %c", &lower);
//^ Space which 'eats' remaning '\n' in the buffer