数字不会从循环内增加?

时间:2014-10-14 21:05:55

标签: objective-c loops increment

简单的程序和简单的监督。生成两个随机数(0-20)并对它们执行数学运算。检查用户的答案。一切正常,除了跟踪正确答案的数量。

已初始化int correctAnswers = 1并在循环外尝试int correctAnswers作为占位符。正确的答案应该增加一个。 ++correctAnswerscorrectAnswers++correctAnswers += 1都给出完全相同的答案1,无论五次尝试中有多少次都是正确的。

的main.m

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // MathTutor's myMath libraries for creating two random numbers, each between 1 and 20.
        myMath *myMathStuff;
        myMathStuff = [[myMath alloc] init];
        int randomNumberOne = [myMathStuff getRandomNumber];
        int randomNumberTwo = [myMathStuff getRandomNumber];

        // Calculate the expected answers and create a scoring variable.
        float answerMultiplication = (randomNumberOne * randomNumberTwo);
        float answerDivision = (randomNumberOne / randomNumberTwo);
        float answerAddition = (randomNumberOne + randomNumberTwo);
        float answerSubtraction = (randomNumberOne - randomNumberTwo);
        int correctAnswers = 0;

        // Initialize the string and answer variables for scanf().
        char operation;
        float answer;

        // Loop for five iterations.
        for (int i = 0; i < 5; i++) {
            // Ask what type of operation.
            NSLog(@"Please select an operation: \n M. Multiplication \n D. Division \n A. Addition \n S. Subtraction");
            scanf("%s", &operation);
            // Check for the selected operation.
            switch (operation) {
                case 'M':
                    NSLog(@"What is: (%i x %i)?", randomNumberOne, randomNumberTwo);
                    scanf("%f", &answer);
                    if (answer == answerMultiplication) {
                        NSLog(@"Correct!");
                        ++correctAnswers;
                    }
                    else { NSLog(@"Incorrect! The correct answer is: %f!", answerMultiplication); }
                    break;
                case 'D':
                    NSLog(@"What is: (%i / %i)?", randomNumberOne, randomNumberTwo);
                    scanf("%f", &answer);
                    if (answer == answerDivision) {
                        NSLog(@"Correct!");
                        ++correctAnswers;
                    }
                    else { NSLog(@"Incorrect! The correct answer is: %f!", answerDivision); }
                    break;
                case 'A':
                    NSLog(@"What is: (%i + %i)?", randomNumberOne, randomNumberTwo);
                    scanf("%f", &answer);
                    if (answer == answerAddition) {
                        NSLog(@"Correct!");
                        ++correctAnswers;
                    }
                    else { NSLog(@"Incorrect! The correct answer is: %f!", answerAddition); }
                    break;
                case 'S':
                    NSLog(@"What is: (%i - %i)?", randomNumberOne, randomNumberTwo);
                    scanf("%f", &answer);
                    if (answer == answerSubtraction) {
                        NSLog(@"Correct!");
                        ++correctAnswers;                    }
                    else { NSLog(@"Incorrect! The correct answer is: %f!", answerSubtraction); }
                    break;
                default:
                    NSLog(@"Sorry, but that is not a valid operation.");
                    // Reduce the loop counter by one to offset the invalid choice.
                    i--;
                    break;
            }
        }
        // Calculate the score percentage and display the score.
        NSLog(@"You correctly answered %i out of 5 questions!", correctAnswers);
        NSLog(@"Your grade for this challenge is %i%%", correctAnswers * 20);
        // Reset the answers counter.
        correctAnswers = 0;
    }
    return 0;
}

感谢。

2 个答案:

答案 0 :(得分:3)

YarGnawh打败了我正确的答案,但为了提供更全面的细节,请:

scanf("%s", &operation);

要求在operation的地址存放一个以NULL结尾的完整字符串。所以,如果你输入字母“M&#39;然后将两个字节写入该地址:字母的字符代码&#39; M&#39;然后终止NULL

由于您已将操作声明为char operation;,因此缓冲区溢出。所以那里只有一个字节的存储空间。因此,终止NULL将被写入不安全的存储空间。

实际上,它似乎写入correctAnswers的最低字节。这样变量就变为零。编译器可以按照他们喜欢的任何顺序自由安排变量,因此没有理由猜测correctAnswers会受到影响或者对它感到惊讶。

所以:你的代码有不确定的行为。在这种特定情况下,未定义的行为相对无害地发生,将其他一个变量的一部分归零。

答案 1 :(得分:2)

问题在于scanf的操作。而是扫描角色。

            scanf(" %c", &operation);