为什么没有收到任何新的输入我的程序循环? [英] Why does my program loop without receiving any new input?

查看:132
本文介绍了为什么没有收到任何新的输入我的程序循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这样,一旦给出的答案是我的程序将重新来过。这只是一次我跑一次就不会再起作用。我想使它功能到用户不必重新启动该程序的。谢谢!

I am trying to make is so that my program will start over once the answer is given. It just won't function again once I run it once. I want to make it functional to where the user doesn't have to start the program up again. Thanks!

#include <stdio.h>
#include <math.h>


int main()
{
    float firstnum, secondnum, answer;
    char function;

    printf("\nHello and welcome to my calculator!\n");                                             //Intro

    start:                                                                                         //Area to loop to when program completes

    printf("\nPlease input the function you would like to use.  These include +, -, *, /.\n");     //Asking for function input

    scanf("%c", &function);                                                                        //Receiving Function Input

    printf("\nNow please input the two variables.\n");                                             //Asking for variables

    scanf("%f", &firstnum);

    scanf("%f", &secondnum);                                                                       //Receiving Input for Variables

    if (function == '+')                                                                           //Doing calculation
    {
            answer = firstnum+secondnum;
    }
    else if (function == '-')
    {
    answer = firstnum-secondnum;
    }
    else if (function == '*')
    {
    answer = firstnum*secondnum;
    }
    else if (function == '/')
    {
    answer = firstnum/secondnum;
    }
    else
    {
            printf("Sorry that was an incorrect function.  The correct inputs are +, -, *, /.");       //If they don't follow the directions
    }

    printf("Your answer is %f \n", answer);                                                        //Answer

    goto start;                                                                                    //Loop

return 0;

}

推荐答案

这就是为什么你使用循环。 (而且尽量不要使用转到此)。

This is why you use loops. (And try not to use goto for this).

#include <stdio.h>
#include <math.h>


int main() {

    float firstnum, secondnum, answer;
    char function, buffer[2];

    while(1) {      

        printf("\nHello and welcome to my calculator!\n");                                             

        printf("\nPlease input the function you would like to use.  These include +, -, *, /.\n");     
        scanf("%s", &buffer);   
        function = buffer[0];
        printf("\nNow please input the two variables.\n");  
        scanf("%f", &firstnum);
        scanf("%f", &secondnum);
        if (function == '+') answer = firstnum+secondnum;
        else if (function == '-') answer = firstnum-secondnum;
        else if (function == '*') answer = firstnum*secondnum;
        else if (function == '/') answer = firstnum/secondnum;
        else printf("Sorry that was an incorrect function.  The correct inputs are +, -, *, /.");    

        printf("Your answer is %f \n", answer);                                                               
        } 

     return 0;
     }

这应该在一个无限循环,所以使用来自用户的输入突破; 循环退出程序

This should go in an infinite loop, so use an input from the user to break; the loop to exit the program

注:我已经取代了scanf函数%C与%s表示字符串和放大器的输入;所使用的缓冲

Note : I have replaced the scanf %c with %s indicating an input of a string & used a buffer.

 scanf("%s",&buffer); function = buffer[0];

(更新按照注释中讨论)

这篇关于为什么没有收到任何新的输入我的程序循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆