int main()
{
int program = 0;
while (program >= 0)
{
printf("\nChoose one of the following programs: \n\n (1) Fibonacci Sequence Calculator \n (2) Decimal and Binary Calculator \n (3) Prime Number Calculator \n \nIf you want to exit the program, press (e).\nYour choice: ");
scanf("%d", &program);
if (program == 0)
{
printf("Quitting the program...\n\n");
return 0;
}
else if(program==1)
{
printf ("FIBONACCI SEQUENCE CALCULATOR");
}
else if(program==2)
{
printf("DECIMAL AND BINARY CALCULATOR");
}
else if(program==3)
{
printf("PRIME NUMBER CALCULATOR");
}
else
{
printf("ERROR");
}
}
我想打印" ERROR"当用户输入除0,1,2和3之外的任何内容时,这是我输入时的结果:
也许这可以帮助我找出问题的答案:我已经知道%d用于扫描整数,%c用于扫描字符。但是当我想扫描它们时我还需要使用什么?
任何帮助将不胜感激:)
答案 0 :(得分:1)
Exapmple其中一种方式。
#include <stdio.h>
#include <stdlib.h>
void end_proc(void){
printf("Quitting the program...\n\n");
exit(0);
}
void input_error_proc(void){
int ch;
printf("ERROR\n");
while((ch = getchar()) != '\n' && ch != EOF);//clear input
}
int main(void){
int program;
while(1){
printf("\n"
"Choose one of the following programs: \n\n"
" (1) Fibonacci Sequence Calculator \n"
" (2) Decimal and Binary Calculator \n"
" (3) Prime Number Calculator \n"
" \n"
"If you want to exit the program, press (e or 0).\n"
"Your choice: ");
if(1==scanf("%d", &program)){//input number
if (program == 0){
end_proc();
} else if(program == 1){
printf ("FIBONACCI SEQUENCE CALCULATOR");
} else if(program==2) {
printf("DECIMAL AND BINARY CALCULATOR");
} else if(program==3) {
printf("PRIME NUMBER CALCULATOR");
} else {
input_error_proc();
}
} else {//input not number
char ch, check;
if(2 == scanf(" %c%c", &ch, &check) && ch == 'e' && check == '\n')
end_proc();
else
input_error_proc();
}
}
return 0;
}
答案 1 :(得分:0)
我添加了一个数字或字符的测试,我改变了你的程序流程。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
int program = 0;
char c;
for (; ;) {
printf("\nChoose one of the following programs: \n\n (1) Fibonacci Sequence Calculator \n (2) Decimal and Binary Calculator \n (3) Prime Number Calculator \n \nIf you want to exit the program, press (e).\nYour choice: ");
scanf(" %c", &c);
if (isalpha(c)) {
printf("ERROR");
continue;
}
else if (isdigit(c)) {
program = atoi(&c);
if (program == 0) {
printf("Quitting the program...\n\n");
return 0;
}
else if (program == 1) {
printf("FIBONACCI SEQUENCE CALCULATOR");
}
else if (program == 2) {
printf("DECIMAL AND BINARY CALCULATOR");
}
else if (program == 3) {
printf("PRIME NUMBER CALCULATOR");
}
else {
printf("ERROR");
}
}
}
}
测试
Debug/gnu
Choose one of the following programs:
(1) Fibonacci Sequence Calculator
(2) Decimal and Binary Calculator
(3) Prime Number Calculator
If you want to exit the program, press (e).
Your choice: 1
FIBONACCI SEQUENCE CALCULATOR
Choose one of the following programs:
(1) Fibonacci Sequence Calculator
(2) Decimal and Binary Calculator
(3) Prime Number Calculator
If you want to exit the program, press (e).
Your choice: q
ERROR
Choose one of the following programs:
(1) Fibonacci Sequence Calculator
(2) Decimal and Binary Calculator
(3) Prime Number Calculator
If you want to exit the program, press (e).
Your choice: