在c primer plus上做题的时候卡住了,求大佬帮忙

AcodingDog 2018-01-27 01:40:13

#include <stdio.h>

float add(float a,float b){
return a + b;
}

float subtract(float a, float b){
return a - b;
}

float multiply(float a, float b){
return a * b;
}

float divide(float a, float b){
return a / b;
}

void printMenu(){
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}

int main(){
char option = 'a';
int is_correct;
char ch;
float f_num,s_num;

while(option != 'q'){
printMenu();
scanf("%1c",&option);
while(getchar() != '\n' ){
continue;
}
if(option == 'q'){
break;
}
else if(option == 'a' || option == 's' || option == 'm' || option == 'd' ){
if(option == 'd'){
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
while(s_num == 0){
printf("Enter a number other than 0 ");
scanf("%f",&s_num);
}
printf("%f / %f = %f\n",f_num,s_num,divide(f_num,s_num));
}
else{
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
switch(option){
case 'a':
printf("%f + %f = %f \n",f_num,s_num,add(f_num,s_num) );
break;
case 's':
printf("%f - %f = %f \n",f_num,s_num,subtract(f_num,s_num) );
break;
case 'm':
printf("%f * %f = %f \n",f_num,s_num,multiply(f_num,s_num) );
break;
}
}
}
else{
is_correct = 0;
printf("Your choice is %d\n",option);
printf("Please input a correct choice\n");
}
}
printf("Bye");

return 0;
}

问题是不知道为何,当输入为【a,s,m,d】的时候,需要输入两次,明明已经跳过回车换行了啊.....
...全文
504 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
择yi 2018-01-29
  • 打赏
  • 举报
回复
发错了
#include <stdio.h>
char  get_choice(void);
int check_first_input (void);
int check_second_input (void);
float add(void);
float subtract(void);
float multiply(void);
float divide(void);
int main(void)
{
    char choice;


    while ((choice = get_choice()) != 'q')
      {
        switch(choice)
        {
            case 'a':  add(); break;
            case 's':  subtract(); break;
            case 'm':  multiply(); break;
            case 'd':  divide(); break;
            default :printf("please enter the right word.\n");continue;
        }
      }
    printf("done!");

    return 0;
}

char get_choice(void)
{
    char choice;

    printf("Enter the operation of your choice: \n");
    printf("a. add           s. subtract \n");
    printf("m. multiply      d. divide\n");
    printf("q. quit\n");
    choice = getchar();
    while(getchar() !='\n')
        continue;

    return choice;
}

float add(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    result = first_number + second_number;
    printf("%.1f + %.1f = %.1f\n",first_number,second_number,result);

    return result;
}

float subtract(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    result = first_number - second_number;
    printf("%.1f - %.1f = %.1f\n",first_number,second_number,result);

    return result;
}

float multiply(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    result = first_number * second_number;
    printf("%.1f * %.1f = %.1f\n",first_number,second_number,result);

    return result;
}
float divide(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    while (second_number == 0)
    {
        printf("enter a number other than 0: ");
        second_number = check_second_input();
    }
    result = first_number / second_number;
    printf("%.1f / %.1f = %.1f\n",first_number,second_number,result);

    return result;
}
int check_first_input (void)
{
    int  input;
    char ch;

    printf("please enter the first number: ");
    while ((scanf("%d",&input)) == 0)
    {
        while ((ch = getchar()) != '\n')   //这里的getchar()获得是上次input的值,并不是再从键盘获取新的值
            putchar(ch);
        printf(" is not an number.\n");
        printf("please enter a number,such as 2.5,-1.78E8,or 3 : ");
    }
    while(getchar() !='\n')  //我之前想法是这边的不等于改成等于,但是一旦这样修改那么Enter将会进入while
        continue;            // 那么这个enter键将被使用而不会将输入送入缓存区。
    return input;
}

int check_second_input (void)
{
    int input;
    char ch;

    printf("please enter the second number: ");
    while ((scanf("%d",&input)) == 0)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an number.");
        printf("please enter a number,such as 2.5,-1.78E8,or 3  :");
    }
    while(getchar() !='\n')
        continue;
    return input;
}
择yi 2018-01-29
  • 打赏
  • 举报
回复
#include <stdio.h>

float add(float a,float b){
	return a + b;
}

float subtract(float a, float b){
	return a - b;
}

float multiply(float a, float b){
	return a * b;
}

float divide(float a, float b){
	return a / b;
}

void printMenu(){
	printf("Enter the operation of your choice: \n");
	printf("a. add               s. subtract\n");
	printf("m. multiply          d. divide\n");
	printf("q. quit\n");
}

int main(){
	char option = 'a';
	int is_correct;
	char ch;
	float f_num,s_num;

	while(option != 'q'){
		printMenu();
		scanf("%1c",&option);
		while(getchar() != '\n' ){
			continue;
		}
		if(option == 'q'){
			break;
		}
		else if(option == 'a' || option == 's' || option == 'm' || option == 'd' ){
			if(option == 'd'){
				printf("Enter first number: ");
				while(scanf("%f",&f_num) != 1){
					printf("It is not a number\n");
					printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
				}
				printf("Enter second number: ");
				while(scanf("%f",&s_num) != 1){
					printf("It is not a number\n");
					printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
				}
				while(s_num == 0){
					printf("Enter a number other than 0 ");
					scanf("%f",&s_num);
				}
				printf("%f / %f = %f\n",f_num,s_num,divide(f_num,s_num));
			}
			else{
				printf("Enter first number: ");
				while(scanf("%f",&f_num) != 1){
					printf("It is not a number\n");
					printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
				}
				printf("Enter second number: ");
				while(scanf("%f",&s_num) != 1){
					printf("It is not a number\n");
					printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
				}
				switch(option){
					case 'a':
					printf("%f + %f = %f \n",f_num,s_num,add(f_num,s_num) );
					break;
					case 's':
					printf("%f - %f = %f \n",f_num,s_num,subtract(f_num,s_num) );
					break;
					case 'm':
					printf("%f * %f = %f \n",f_num,s_num,multiply(f_num,s_num) );
					break;
				}
			}
		}
		else{
			is_correct = 0;
			printf("Your choice is %d\n",option);
			printf("Please input a correct choice\n");
		}
	}
	printf("Bye");

	return 0;
}
择yi 2018-01-29
  • 打赏
  • 举报
回复
因为scanf函数只有在在转换格式使用%d 的时候 会自动筛掉回车和空格 , 也就是说 在使用 %d 转化格式的时候 上一次的sanf所留下来的回车字符对下一个sanf没有影响。但是如果不是使用 %d 的转化格式,在你按下【enter】键时 ‘\n’ 也会被送入缓冲区, 下一次的getchar()就会接收这个‘\n’, 可以在两个输入之间加上 while(getchar()!= ‘\n’) continue; 来将这个‘\n’ “用掉” ,这样就不会影响到下一次的输入了 。附上我之前写的一起交流学习:
hondely 2018-01-27
  • 打赏
  • 举报
回复
输入的问题 字符用getch()好一点,头文件conio.h,但是不回显
自信男孩 2018-01-27
  • 打赏
  • 举报
回复
#include <stdio.h>

float add(float a,float b){
    return a + b;
}

float subtract(float a, float b){
    return a - b;
}

float multiply(float a, float b){
    return a * b;
}

float divide(float a, float b){
    return a / b;
}

void printMenu(){
    printf("Enter the operation of your choice: \n");
    printf("a. add               s. subtract\n");
    printf("m. multiply          d. divide\n");
    printf("q. quit\n");
}

int main()
{
    char option = 'a';
    int is_correct;
    char ch;
    float f_num,s_num;

    while(option != 'q'){
        printMenu();
        scanf("%c", &option);
        while (getchar() != '\n' ) {
            continue;
        }
        if(option == 'q'){
            break;
        }
        else if(option == 'a' || option == 's' || option == 'm' || option == 'd' ){
            if(option == 'd'){
                printf("Enter first number: ");
                while(scanf("%f",&f_num) != 1){
                    printf("It is not a number\n");
                    printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
                }
                printf("Enter second number: ");
                while(scanf("%f",&s_num) != 1){
                    printf("It is not a number\n");
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                while(s_num == 0){
                    printf("Enter a number other than 0 ");
                    scanf("%f", &s_num);
                }
                printf("%f / %f = %f\n", f_num, s_num, divide(f_num,s_num));
            } else {
                printf("Enter first number: ");
                while(scanf("%f", &f_num) != 1) {
                    printf("It is not a number\n");
                    printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
                }
                printf("Enter second number: ");
                while(scanf("%f", &s_num) != 1){
                    printf("It is not a number\n");
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                switch(option){
                    case 'a':
                        printf("%f + %f = %f \n",f_num,s_num,add(f_num,s_num) );
                        break;
                    case 's':
                        printf("%f - %f = %f \n",f_num,s_num,subtract(f_num,s_num) );
                        break;
                    case 'm':
                        printf("%f * %f = %f \n",f_num,s_num,multiply(f_num,s_num) );
                        break;
                }
            }
            getchar();    /*接收第二个输入后残留在内存里的换行符'\n' */
        } else{
            is_correct = 0;
            printf("Your choice is %d\n",option);
            printf("Please input a correct choice\n");
        }
    }
    printf("Bye\n");

    return 0;
}
因为输入s_num后,会有换行符'\n'残留在输入缓存里了,所以再次输入的内容会被getchar();接收。因此,需要再次输入; 结局办法如上代码,加一句getchar()即可,这句就是为了接收输入s_num后的换行符。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧