CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Help with cin

  1. #1
    Join Date
    Mar 2007
    Posts
    8

    Help with cin

    Hi, what i want to do is, have a cin, and if the input the user puts in is anything other than 8 numbers, it displays an error saying invalid input.

    How can i do this?

  2. #2
    Join Date
    Mar 2007
    Posts
    8

    Unhappy Re: Help with cin

    anyone?

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with cin

    If you had enabled private messages in your profile (which is clearly explained in the FAQ), I would not have posted this publicly, but you have left me no choice to contact you.

    "Bumping" a message without waiting ATLEAST 24 hours is considered quite rude, and is likely to get your post totally ignored [this is ALSO covered in the FAQ].

    Also in the FAQ is that when you post a questiopn you should usually post code. We are not mind readers, just volunteers with a passion for programming. When you post code it should be a minimal yet complete (that means we can copy, pases the code intop a .cpp file, compiles it and run it and see the behaviour you are reporting (of course syntax related questions will not compile).

    Finally code should ALWAYS be posted enclosed in code tags. If you don't know how to use them, then once again it is time to read the FAQ.


    Good Luck.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Mar 2007
    Posts
    8

    Re: Help with cin

    Sorry about that, i'll read through the FAQs before i post again.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with cin

    OK, thanks.

    When you use cin (or any other input stream), the data must match the datatype that is being used, so the followinf are typical:

    Code:
    std::string s;
    int i;
    double d;
    
    cin >> s; // Expects a String
    cin >> i;  // Expects an Integer
    cin >> d; // Expects a double.
    One detail people often forget, is that reading from a stream stops at the first character which does not match the input data type. This character is left in the buffer for the next operation. This means that input seperated by "enter" or "return" needs to account for the newline information in the buffer.

    Once you have read a bit oon the faqs, please post back, and provide as many details as possible.
    Last edited by TheCPUWizard; April 28th, 2007 at 01:43 PM.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Mar 2007
    Posts
    8

    Re: Help with cin

    OK, what i am trying to do is
    Code:
    cin 8 numbers (eg. 12345678)
    if less than 8 numbers entered OR more than 8 numbers entered
    cout Invalid number entered

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with cin

    Good, you discovered code tags..

    Post your code as it exists now. This type of validation can be done a number of ways, so I would like to see what you have tried before providing suggestions that may radically differ (especially if yor implementation is correct). In addition to the actual code, please describe what the program currently does if you:

    1) Enter exactly 8 numbers?
    2) Enter less than 8 numbers?
    4) Enter more than 8 numbers?

    Also Are you entering the numbers all on the same line? How are you seperating them?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Dec 2005
    Posts
    254

    Re: Help with cin

    Yes, you should be very specific in your information. Strictly speaking, it isn't 8 numbers that the user should input, but one number containing 8 digits. I only gleaned this from your

    cin 8 numbers (eg. 12345678)
    line in your last post. Prior to that post I thought you had wanted to input 8 distinct numbers, each composing of any number of digits.

    Here is one way:
    #include <iostream>
    #include <string>
    #include <cstdlib> // for atoi

    using std::cout;
    using std::cin;
    using std::endl;

    int main()
    {
    int myint;
    std::string input;
    cout << "Enter a number comprised of 8 digits: ";
    cin >> input;
    if(input.size() != 8)
    cout << "You entered wrong number of digits." << endl;
    else
    {
    myint = atoi(input.c_str());
    cout << "You entered " << myint << endl;
    }

    return 0;
    }
    There's other ways to do this but this way works. Input the number as a string, compare its size to 8, and if it is the correct size, then convert the string to an integer. You could also insert some error checking to make sure that only digits were entered by using the isdigit() function, in which case you would have to include the cctype library.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with cin

    Good example, but...


    Code:
    myint = atoi(input.c_str());
    I would much prefer seeing a strstream used to convert the string to an int. atoi() is more appropriate to "C" than "C++".

    Also be careful using an int. If your int size happens to be 16 bits, then it will not hold the full 8 digits. I would recommend a long just for safety sake....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured