2

私は、答えが与えられると私のプログラムが最初からやり直すようにしようとしています。一度実行すると、再び機能しなくなります。ユーザーがプログラムを再起動する必要がないところまで機能させたいです。ありがとう!

#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;

}

4

3 に答える 3

2

【エンター】キーです。最初scanfは、前の反復を終了するために押したエンター キーを読み取ります。

したがって、改行を食べるには、別のscanf("%c", &function);またはgetchar();直前に追加する必要があります。goto

数値を読み取る場合、scanf最初の空白はすべて消費されます。しかし、文字を読むときはそうではありません。ストリームの次のバイトを提供します。


おそらくより良い方法は、`scanf` にすべての改行を期待する場所を伝えることでしょう。この方法では、何もしていないように見えるがコメントされていない (!) 奇妙な謎の行は必要ありません。今から数か月後にこのコードをもう一度いじると、問題が発生するからです。
//scanf("%c\n", &function); /* read a character followed by newline DOESN'T WORK */
...
//scanf("%f\n", &secondnum); /* read a number followed by newline DOESN'T WORK */

このようにして、末尾の改行が消費されます。これは、(ユーザー側から)より直感的な動作だと思います。 いいえ。うまくいきません。そうすればよかったのに。


私は動揺していませんgoto。古くからの友人に会えてうれしいです。もしあったとしても、これは適切な使い方です。フォームとまったく同じwhileです。したがって、ほとんどのwhile(1)人はlabel:. しかし、画面よりも小さい関数で無限ループが発生するのはなぜでしょうか? 楽しむ。アザラシの赤ちゃんに害はありません。:)

于 2013-02-21T03:03:46.933 に答える
1

これが、ループを使用する理由です。(そして、これにはgotoを使用しないようにしてください)。

#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;
     }

これは無限ループになるはずなので、ユーザーからbreak;ループへの入力を使用してプログラムを終了します


注:scanf%cを文字列の入力を示す%sに置き換え、バッファーを使用しました。

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

(コメントの議論に従って更新)

于 2013-02-21T03:11:39.877 に答える
0

One "best practise" regarding scanf is to check it's return value. In regards to the return value of scanf, I suggest reading this scanf manual carefully and answering the following questions:

  1. int x = scanf("%d", &foo); What do you suppose x will be if I enter "fubar\n" as input?
  2. Where do you suppose the 'f' from "fubar\n" will go?
  3. If it remains in stdin, would you expect a second scanf("%d", &foo); to be successful?
  4. int x = scanf("%d", &foo); What do you suppose x will be if I run this code on Windows and press CTRL+Z to send EOF to stdin?
  5. Would it be safe to use foo if x is less than 1? Why not?
  6. int x = scanf("%d %d", &foo, &bar); What would you expect x to be if I enter "123 456\n" as input?
  7. Do you suppose the '\n' will still be on stdin? What value would char_variable hold following scanf("%c", &char_variable);?

EOF can be sent through stdin in Windows by CTRL+Z, and in Linux and friends by CTRL+D, in addition to using pipes and redirection to redirect input from other programs and files.

By using code like int function; for (function = getchar(); function >= 0 && isspace(function); function = getchar()); assert(function >= 0); or char function; assert(scanf("%*[ \n]%c", &function) == 1); you can discard leading whitespace before assigning to function.

于 2013-02-21T04:09:26.703 に答える