我是 Objective-C 的新手,这确实是我的第一个交互式程序。我已经学习了大约2周了。
所以,我的问题是:通常我注意到当您scanf
连续有多个 ' 时,它们每个都在等待输入 - 但是在这种情况下,我要求提供帐户所有者姓名和余额 - 它会触发这两个NSLog
函数而不是等待对于第一个输入。
这是我的主要内容:
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;
}