scanf函数不等待输入我的内循环? [英] scanf is not waiting for input within my loop?

查看:169
本文介绍了scanf函数不等待输入我的内循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Objective-C,这真的是我的第一个程序,它是互动的。我一直在学习了大约2个星期了。

所以,我的问题是:通常,当你有我注意到多的一排,他们每人等待输入 scanf函数 - 然而,在这种情况下,在我问帐户所有者姓名和平衡 - 它激发双方的NSLog 函数而不是等待第一个输入

下面是我的主要:

  INT主(INT ARGC,CHAR *的argv []){
    NSAutoreleasePool *池= [[NSAutoreleasePool的alloc]初始化];    银行* columbiaBank = [[银行的alloc]初始化];    INT迭代= 0;
    而(真){
        INT选择= 0;
        的NSLog(@。\\ n1的添加帐户\\ N2删除帐户\\ N3修改帐户\\ nWhat你想干什么?:。);
        scanf函数(%i的,&安培;选择);        如果(选择== 1){            的NSLog(@\\ n输入帐户持有人:);
            焦炭accountOwner;
            scanf函数(%C,&安培; accountOwner);            的NSLog(@\\ n输入期初余额:);
            浮openingBalance;
            scanf函数(%F,&安培; openingBalance);            //创建和添加新帐号
            *的BankAccount = newAccount [的BankAccount页头] initWithProps:[的NSString stringWithFormat:@%C,accountOwner]:[的NSString stringWithFormat:@%i的,迭代]:openingBalance];
            [columbiaBank addAccount:newAccount];
            [newAccount发布]
            的NSLog(@\\ nAccount添加成功!);       }否则如果(选择== 2){            的NSLog(@\\ n输入帐户ID:);
            INT帐户ID;
            scanf函数(%i的,&安培;帐户ID);
            // 删除帐户
            [columbiaBank removeAccount:[的NSString stringWithFormat:@%i的,帐户ID]];
            的NSLog(@\\ nAccount删除成功!);        }否则如果(选择== 3){            的NSLog(@\\ n该银行目前有%i个账户。columbiaBank.totalAccounts);
            的NSLog(@\\ n此所有银行账户的当前余额为$%F,columbiaBank.totalBankBalance);
            的NSLog(@\\所有N--输出帐户信息 - );
            [columbiaBank printAccounts]        }其他{            的NSLog(@你没有输入一个有效的行动。);        }
        迭代++;
    }    [columbiaBank发布]
    [池排水]
    返回false;
}


解决方案

* [注意:如果你要使用的Objective-C,你可能希望使用的输入转换方法从可可,而不是混合可可(的NSLog )和标准输入输出( scanf函数)。但是,这并不回答你的问题...]

当解析整数,浮点数甚至字符串 scanf函数跳过空白 - 例如空格,制表符,线的端的等 - 和每个输入线与至少线的一端(其可以是一个回车,换行,或两者取决于系统)结束。这意味着,读出第一个整数后仍有至少线的输入端和读取一个字符将返回它的尝试 - 因此没有等待输入。要放弃剩余的,未使用的,输入可以使用 fpurge 。例如:

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    INT选择= 0;
    的fputs(\\ n1的添加帐户\\ N2删除帐户\\ N3修改帐户\\ nWhat你想干什么?:。标准输出);
    scanf函数(%i的,&安培;选择);    如果(选择== 1)
    {        的fputs(\\ n输入帐户持有人:标准输出);
        fpurge(标准输入); //跳过留在缓冲区内的任何输入作为%C需要在第二天的性格和不跳过空白
        焦炭accountOwner;
        scanf函数(%C,&安培; accountOwner);        的fputs(\\ n输入期初余额,由于输出);
        浮openingBalance;
        scanf函数(%F,&安培; openingBalance);        的printf(%C - %F \\ N,accountOwner,openingBalance);
    }
}

请注意,阅读在字符串中没有跳过空白,因此,如果您的帐户的主人是一个字符串,你就不需要在 fpurge

I'm new to Objective-C, and this is really my first program that is interactive. I've been learning for about 2 weeks now.

So, my question is: typically I've noticed when you have multiple scanf's in a row, they each wait for input - however in this situation, where I ask for account owner name, and balance - it fires both NSLog functions instead of waiting for the first input.

Here is my main:

int main(int argc, char* argV[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    bank *columbiaBank = [[bank alloc] init];

    int iteration = 0;
    while (true) {
        int selection = 0;
        NSLog(@"\n1. Add Account \n2. Remove Account \n3. Modify Account \nWhat would you like to do?:");
        scanf("%i", &selection);

        if (selection == 1) {

            NSLog(@"\nEnter account owner:");
            char accountOwner;
            scanf("%c", &accountOwner);

            NSLog(@"\nEnter opening balance:");
            float openingBalance;
            scanf("%f", &openingBalance);

            // create and add new account
            bankAccount *newAccount = [[bankAccount alloc] initWithProps:[NSString stringWithFormat:@"%c", accountOwner] :[NSString stringWithFormat:@"%i", iteration] :openingBalance];
            [columbiaBank addAccount:newAccount];
            [newAccount release];
            NSLog(@"\nAccount successfully added!");

       } else if (selection == 2) {

            NSLog(@"\nEnter account id:");
            int accountId;
            scanf("%i", &accountId);
            // remove account
            [columbiaBank removeAccount:[NSString stringWithFormat:@"%i", accountId]];
            NSLog(@"\nAccount successfully removed!");

        } else if (selection == 3) {

            NSLog(@"\nThe bank currently has %i accounts.", columbiaBank.totalAccounts);
            NSLog(@"\nThe bank's current balance from all accounts is $%f", columbiaBank.totalBankBalance);
            NSLog(@"\n-- Output of all account info --");
            [columbiaBank printAccounts];

        } else {

            NSLog(@"You did not enter a valid action.");

        }
        iteration++;
    }

    [columbiaBank release];
    [pool drain];
    return false;
}

解决方案

*[Note: If you are going to use Objective-C you might wish to use input conversion methods from Cocoa rather than mix Cocoa (NSLog) and stdio (scanf). But that doesn't answer your question...]

When parsing integers, floats and even strings scanf skips whitespace - e.g. spaces, tabs, end of line, etc. - and every input line ends with at least an end of line (which may be a carriage return, line feed, or both depending on the system). This means that after reading your first integer there is still, at least, an end of line in the input and the attempt to read a character will return it - hence no wait for input. To discard the remaining, unused, input you can use fpurge. E.g.:

#include <stdio.h>

int main(int argc, char* argV[])
{
    int selection = 0;
    fputs("\n1. Add Account \n2. Remove Account \n3. Modify Account \nWhat would you like to do?: ", stdout);
    scanf("%i", &selection);

    if (selection == 1)
    {

        fputs("\nEnter account owner: ", stdout);
        fpurge(stdin); // skip any input left in the buffer as %c takes the very next character and does not skip whitespace
        char accountOwner;
        scanf("%c", &accountOwner);

        fputs("\nEnter opening balance: ", stdout);
        float openingBalance;
        scanf("%f", &openingBalance);

        printf("%c - %f\n", accountOwner, openingBalance);
    }
}

Note that reading in character strings does skip whitespace, so if your account owner was a string you would not need the fpurge.

这篇关于scanf函数不等待输入我的内循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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