I’m trying input validation in the following piece of code:
typedef unsigned long DATATYPE;
typedef unsigned long ADDRTYPE;
enum {READ_MEMORY, WRITE_MEMORY, PRINT_CACHE, PRINT_MEMORY, EXIT};
typedef bool BOOL;
typedef struct
{
ADDRTYPE nRamSize;
DATATYPE* pMemory;
} MEMORY;
typedef struct
{
DATATYPE data[CACHELINE_SIZE_BYTES];
ADDRTYPE tag;
BOOL bValid;
} CACHELINE;
typedef struct
{
CACHELINE CacheLine[SET_ASSOCIATIVITY];
} CACHESET;
typedef struct
{
ADDRTYPE nNumSets; // Stores the number of sets in the cache
CACHESET* pCacheSet; // Each set contains SET_ASSOCIATIVITY slots
} SETASSOCCACHE;
int isPowerOfTwo(ADDRTYPE x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
int main()
{
int nChoice;
ADDRTYPE nCacheSize;
MEMORY Memory;
SETASSOCCACHE Cache;
do
{
cout << "Enter size of memory in bytes (has to be a power of 2)" << endl;
cin >> Memory.nRamSize;
cin.sync();
} while (!isPowerOfTwo(Memory.nRamSize)); // Keep taking in values till its a power of 2
do
{
cout << "Enter size of cache in bytes (has to be a power of 2)" << endl;
cin >> nCacheSize;
cin.sync();
} while (!isPowerOfTwo(nCacheSize)); // Keep taking in values till its a power of 2
}
According to this, using cin.sync() should get rid of any trailing newlines in the user input. But, even with that, I’m STILL getting a infinite loop after the first input. Any ideas I’m overlooking something obvious ?
Some more info:
- This is on a Mac OSX, using gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
- The terminal used for inputs is the Terminal.app supplied by apple (also on iTerm2).
EDIT – I’m beginning to think this is something peculiar to the g++ installed with XCode 4.1 in Mac OS X Lion – I ran the same code on my office linux machine, and it didn’t show this weirdness (also, none of the other folks seem to have reproduced the error). It would be nice if someone has Lion XCode, and could try out Brian’s example to see if they get an infinite loop when the first input is a non-power of 2.
Thanks everyone for jumping in !!
If you reduce your code to a SSCCE you’ll find that there’s no problem:
This works exactly as expected, therefore the code you posted isn’t the actual code exhibiting a problem, or there’s something screwy in your types.
EDIT: To explain why entering a non-number causes the loop to spiral out of control, see:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2
Once invalid input is given, it won’t try and read again.
The following will fix these issues (from the FAQ):
When the read into
nChoicefails, the character is left in the stream. You have to reset the stream state, then remove the offending characters from the stream withignore()