我得到了一个学校项目,其中我们必须使用Dev C ++编写某种计算器,并且必须具有一个选项,用户可以选择天气来尝试使用其他输入或另一个键再次进行计算退出程序。
我的问题是,在重试或退出程序的选项上,当我输入要再次执行的选项时,它只是自动执行所有操作,而没有给出输入字符或任何内容的选项。
这是我的代码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<unistd.h>
int main(){
int a,b,choice;
using namespace std;
system("cls");
printf("\t===============================================================\n"
);
printf("\n\t\t\t\tFinals Project I\n\n");
printf("\t\t\t Program Status : Complete\n\n");
printf("\t===============================================================\n\
n");
printf("Loading libraries...");
sleep(3);
printf("\tSuccess\n");
printf("Binding program libs...");
sleep(2);
printf("\tSuccess\n");
printf("Executing program...");
sleep(2);
printf("\tSuccess\n\n");
printf("Program Started!\n\n");
do{
printf("Please select valid operation (+ - / *):\t");
char operation;
scanf("%c", &operation);
switch(operation){
case '+':
printf("\nEnter 1st number:\t");
scanf("%d",&a);
printf("\nEnter 2nd number:\t");
scanf("%d",&b);
printf("\nAnswer is %d",a+b);
break;
case '-':
printf("Enter 1st number:\n");
scanf("%d",&a);
printf("Enter 2nd number:\n");
scanf("%d",&b);
printf("Answer is %d",a-b);
break;
case '/':
printf("Enter 1st number:\n");
scanf("%d",&a);
printf("Enter 2nd number:\n");
scanf("%d",&b);
printf("Answer is %d",a/b);
break;
case '*':
printf("Enter 1st number:\n");
scanf("%d",&a);
printf("Enter 2nd number:\n");
scanf("%d",&b);
printf("Answer is %d",a*b);
break;
default : printf("Incorrect! Operation not Valid...\n"); break;
}
printf("\nDo you want to try again? :\n[1] YES\n[0] NO\t :\t>>");
scanf("%d",&choice);
}while(choice!=0);
printf("\n\nExiting Program...[Press any Key]");
getch();
}
/*
((operation=='+')||(operation=='-')||(operation=='/')||(operation=='*'))
*/
答案 0 :(得分:0)
我明白了。原来我必须在第一个scanf语法的“%c”之前加上“空格”。感谢大家的评论,对不起我没有具体说明该代码示例。我复制并粘贴了所有内容。