* [Note: If you intend to use Objective-C, you can use input conversion methods from Cocoa, rather than mixing Cocoa ( NSLog ) and stdio ( scanf ). But this does not answer your question ...]
When analyzing integers, scanf floats and even scanf lines omit spaces - for example. spaces, tabs, end of line, etc. - and each line of input ends with at least the end of the 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 the end of the line in the input, and trying to read the character will return it - therefore, do not wait for input. To undo the remaining, unused input, you can use fpurge . For instance:.
#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); } }
Please note that reading in character strings skips spaces, so if your account owner was a string, you do not need fpurge .
source share