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

  • SEARCH
  • Home
  • 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 6763819
In Process

The Archive Base Latest Questions

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

Previously I’m a C programmer(wanna know more about system programming but don’t wanna go

  • 0

Previously I’m a C programmer(wanna know more about system programming but don’t wanna go into assembly so i choose C) more but later on in my University I have to take C++ , I’m actually having a hard time on using the cin and cout object as it has some changes compare to my trusted printf() , scanf() , getchar() macro in C . Here’s the code.

The Code

 int main(void)
 {
    using namespace std;

    cout << "What is the number?\n\n";
    cout << "#Number :";
    cin >> num;

    while(cin.fail())
    {
        cin.clear();
        cin.ignore(1000 , '\n');
        cout << "Please enter a number\n\n";
        cout << "#Number :";
        cin >> num;
    }

    return 0;
 }

The Question

1.)I want this code to ask for a number from user ( less than and more than or equal to 0) , when user enter a character or string , i want it to alert user about it , clear the input buffer and reprompt user for a new value until it’s a number.

2.)So i just googling around and find a page preaching the method , so i just follow the way but it failed . I’ve no idea why once i run this code , type in a character it will causes infinite looping of the output Please enter a number.Any mistake done by me in this code??

Thanks for spending time reading my problem

P/S : I’m using CodeBlocks with minGW compiler.

  • 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-26T14:31:19+00:00Added an answer on May 26, 2026 at 2:31 pm

    You don’t say how it failed, so it’s difficult to say what your problem
    is. The fact that you never declare num is one obvious problem. And
    it will fail if you enter a line longer than 1000 characters as well.

    A more idiomatic solution in C++ would be to read the input line by line, using std::getline, and then using std::istringstream to convert the line into a number.

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

Sidebar

Related Questions

Previously I've asked about inserting a column into a dataset . I now have
Previously, WinAPI::shellExecute() is working on AX 2009, but I do not know why, after
Previously I've worked alone... Now I may have many collaborators. But I don't want
Previously, I had a class that wrapped an internal System.Collections.Generic.List<Item> (where Item is a
Previously, I asked about a clever way to execute a method on a given
Previously we had desktop applications but given the fact that accessing the server (either
previously, there is such method for Rectangle in MFC, i dont know why there
Previously I had: MyClass::MyClass() : myMember(VALUE,this,5,etc.) { ... } but I need to do
Previously i have used restful wcf webservices to get the data from server.But now
Previously I asked about how I could call separate values from a text file

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.