Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:31:16+00:00 2026-05-26T18:31:16+00:00

I’m trying input validation in the following piece of code: typedef unsigned long DATATYPE;

  • 0

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 !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:31:17+00:00Added an answer on May 26, 2026 at 6:31 pm

    If you reduce your code to a SSCCE you’ll find that there’s no problem:

    #include <iostream>
    
    int isPowerOfTwo(unsigned long x)
    {
       return (x != 0) && ((x & (x - 1)) == 0);
    }
    
    int main()
    {
        unsigned long nChoice;
    
        do
        {
            std::cout << "Enter size of memory in bytes (has to be a power of 2)" << std::endl;
            std::cin >> nChoice;
            std::cin.sync();
        } while (!isPowerOfTwo(nChoice)); // Keep taking in values till its a power of 2
    
    }
    

    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):

    while ((std::cout << "Enter size of memory in bytes (has to be a power of 2)" << std::endl)
           && !(std::cin >> nChoice))
    {
       std::cin.clear();
       std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    

    When the read into nChoice fails, the character is left in the stream. You have to reset the stream state, then remove the offending characters from the stream with ignore()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.