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 182155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:53:23+00:00 2026-05-11T14:53:23+00:00

I’ll need to accept a string of 5 numbers from the user (only 5).

  • 0

I’ll need to accept a string of 5 numbers from the user (only 5). Should I use

istringstream  

for the same ?

Can’t I get away with something like this ?

int main() {     char *InputMain;     InputMain=(char *)malloc(5+1);     cout << 'Enter the number : ' <<endl;     cin.getline ( InputMain, 5, '\n' );              // Input goes into InputMain     cout << 'The number entered is : ' << InputMain <<endl;     cin.get(); } 

Then comes the next part..

How’d I make sure the user inputs only 5 chars? And maybe if the user enters more than 5 chars, I should display a warning saying only 5 chars allowed..

One more question, since this essentially is a string, I’ll need to validate the input to be only numbers by parsing through each char in the string and checking against respective ASCII values (for numerals).. Is that approach the right thing?

  • 1 1 Answer
  • 2 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. 2026-05-11T14:53:24+00:00Added an answer on May 11, 2026 at 2:53 pm

    Unfortunately there is no portable way to restrict the number of characters input in C++. But whatever platform you are using will provide some mechanism: for example on Windows, look up Console functions.

    If you do go with plain old C++ iostreams input from cin, it’s a good idea to read the initial text into a std::string using istream& getline(istream&, string&) — this will prevent buffer overflows, because strings resize as necessary. (Your code involving getline(InputMain, 5, '\n') is technically safe in that it won’t read more than 5 characters into InputMain, but this code is fragile — if you later decide you want 6 characters, you could easily forget to update your call to malloc(), leading to crashes. Also, you need to remember to free(InputMain). But as I said, use string instead.)

    Regarding parsing:

    Whether you read the input into a string using getline(cin, str) or some platform-specific code, once it’s in there you need to get it out in the form of a number. This is where the istringstream class is useful — it lets you treat an existing string as a stream to read from, so you can use the formatted input >> operator:

    string str; if (!getline(cin, str)) {     cerr << 'Something went seriously wrong...\n'; }  istringstream iss(str); int i; iss >> i;    // Extract an integer value from the stream that wraps str  if (!iss) {     // Extraction failed (or a more serious problem like EOF reached)     cerr << 'Enter a number dammit!\n'; } else if (i < 1000 || i > 9999) {     cerr << 'Out of range!\n'; } else {     // Process i } 

    If you’re using C++ iostreams, you can actually just extract directly from cin instead of going via a string and an istringstream:

    int i; cin >> i;    // Extract an integer value from cin 

    However this can have subtle undesirable effects; notably, any additional characters on the line typed by the user (in particular the '\n' typed when they press Enter) will remain in the input buffer to be read by the next << operation. Often this doesn’t matter, but sometimes it does: e.g. if you follow up with cin.get();, expecting that this will wait for a keypress, it won’t — it will just read the '\n' (or the first non-digit character the user typed).

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.