简体   繁体   English

Objective-C scanf空间问题

[英]Objective-C scanf spaces issue

I am learning objective-C and for the life of me can't figure out why this is happening. 我正在学习Objective-C,而且我一生无法弄清楚为什么会这样。 When the user inputs when the code is: 当用户输入的代码是:

scanf("%c %lf", &operator, &number);

For some reason it messes with this code: 由于某种原因,它与以下代码混淆:

    doQuit = 0;
    [deskCalc setAccumulator: 0];
    while (doQuit == 0) {
        NSLog(@"Please input an operation and then a number:");
        scanf("%c %lf", &operator, &number);

        switch (operator) { 
            case '+':
                [deskCalc add: number];
                NSLog (@"%lf", [deskCalc accumulator]);
                break;
            case '-':
                [deskCalc subtract: number];
                NSLog (@"%lf", [deskCalc accumulator]);
                break;
            case '*':
            case 'x':
                [deskCalc multiply: number];
                NSLog (@"%lf", [deskCalc accumulator]);
                break;
            case '/':
            if (number != 0) 
                [deskCalc divide: number];
                NSLog (@"%lf", [deskCalc accumulator]);
            else 
                NSLog(@"You can't divide by zero.");
                break; 
            case 'S':
                [deskCalc setAccumulator: number];
                NSLog (@"%lf", [deskCalc accumulator]);
                break;
            case 'E':
                doQuit = 1;
                break;
            default:
                NSLog(@"You did not enter a valid operator.");
                break;
        }
    }

When the user inputs for example "E 10" it will exit the loop but it will also print "You did not enter a valid operator." 当用户输入例如“ E 10”时,它将退出循环,但还会显示“您没有输入有效的运算符”。 When I change the code to: 当我将代码更改为:

scanf(" %c %lf", &operator, &number);

It all of a sudden doesn't print this last line. 突然之间,它没有打印出最后一行。 What is it about the space before %c that fixes this? 解决此问题的%c之前的空格是什么?

White-space characters in the format string let scanf() read and ignore any whitespace character up to the next non-whitespace character. 格式字符串中的空格字符使scanf()读取并忽略任何空格字符,直到下一个非空格字符。 In your case this takes care of the newline characters that are left over from the previous input. 在您的情况下,这将照顾到先前输入中剩余的换行符。

As for the / case, the indentation is misleading you - the statements in the if branch are not correct. 至于/的情况下,压痕是在误导你-在语句if分支是不正确的。 If you want to have multiple statements there, you need to put them in one block using curly braces: 如果要在那里有多个语句,则需要使用花括号将它们放在一个块中:

if (number != 0) {
    [deskCalc divide: number];
    NSLog (@"%lf", [deskCalc accumulator]);
} else {
    NSLog(@"You can't divide by zero.");
}

Note that the curly braces around the else branch are not needed here (it contains only one statement), but help readability. 请注意,此处不需要else分支周围的花括号(它仅包含一个语句),但有助于提高可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM