strings

This is a discussion on strings within the C++ Programming forums, part of the General Programming Boards category; Can you use strings to accept input and then use it for an error handling situation? Such as when the ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    strings

    Can you use strings to accept input and then use it for an error handling situation? Such as when the user is supposed to enter a number, they enter a letter, so you need to figure out how to handle that, but I can't seem to use any type of string in my program. If I use a standard char then if you enter a number with 2 or more digits then the program will get all messed up...

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,669
    if you're using cin, you can supply it with a number and then check it's return value to see if the input was valid or not:

    Code:
    int num;
    cin >> num;
    if(!cin.good()) {
     cin.clear();
     cin.ignore();
     cout << "Invalid Input" << endl;
     }
    or simply:

    Code:
    int num;
    while(!(cin >> num)) {
     cin.clear();
     cin.ignore();
     cout << "Invalid Input" << endl;
     }
    otherwise, to validate a character array yourself, use the functions from the cctype header (such as isdigit()).



    ITSA
    Socket Library!

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    char stands for character, not characters. A char can hold only a single character, and you should use a std::string for more.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You could try something like this:
    Code:
    char str[8];  //or string str;  if you prefer STL strings
    cin >> str; 
    int i = 0;
    int input;
    bool valid = true;
    while( i < strlen(str)) //or i < str.length();
       if(isdigit(str[i]))
    	 ++i;
       else
    	valid = false;
    	break;
    if(valid)
      input = atoi(str); //or use strtol() if you prefer

  5. #5
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    strings are pretty straight forward, below is an example:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string input;
        cout<<"Enter your name: ";
        cin>>input;
        cin.ignore();
        cout<<"You typed " << input << "!\n\n";
        cin.get();
    }
    I started out with nothing and I still have most of it left.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    char str[8]; //or string str; if you prefer STL strings
    You should prefer STL, or at least guard against buffer overflow:
    Code:
    const int BUF = 8;
    char str[BUF + 1];
    cin >> setw(BUF) >> str;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Quote Originally Posted by xelitex
    Can you use strings to accept input and then use it for an error handling situation? Such as when the user is supposed to enter a number, they enter a letter, so you need to figure out how to handle that, but I can't seem to use any type of string in my program. If I use a standard char then if you enter a number with 2 or more digits then the program will get all messed up...
    To sum up...

    if you want the user to be able to enter more than 2 digits, you should use string like:
    Code:
    char myString[20];
    //or
    string myString2;
    and you can validate the input by checking the values of char's in the string which is to check if they are between '0' and '9' ( the ASCII values of 0 and 9 ).

    Or you can use an int, but then you should make sure that the input is only numeric using cin.good().
    none...

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>You should prefer STL, or at least guard against buffer overflow:

    I agree, the STL string class is safer to use than C style strings, because of less risk of buffer overflow, etc. But usage will is be influenced more by the experience/knowledge base of the user.


    >>cin >> setw(BUF) >> str;

    However, the way I interpret the information at cppreference.com, setw() is only available for output streams, not input streams.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    However, the way I interpret the information at cppreference.com, setw() is only available for output streams, not input streams.
    Lets ask Mr Standard
    Quote Originally Posted by Section 27.6.3.7
    Returns: An object s of unspecified type such that if out is an (instance of) basic_ostream then the expression out<<s behaves as if f(s) were called, in is an (instance of) basic_istream then the expression in>>s behaves as if f(s) were called. Where f can be defined as:
    Code:
    ios_base& f(ios_base& str, int n)
    {
      // set precision
      str.precision(n);
      return str;
    }
    The expression out<<s has type ostream& and value out. The expression in>>s has type
    istream& and value in.
    Code:
    smanip setw(int n);

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>Lets ask Mr Standard

    Fair enough.

    Thank you.

    Edit: I have confirmed that setw() with input stream is accepted by Dev-C++, for whatever that's worth. Thanks again.
    Last edited by elad; 12-24-2004 at 09:52 AM.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    NP. I tried cplusplus.com which I use for my I/O references but couldn't find much on setw() so well off to the standard

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    setw on input streams has an effect only on reading into character arrays and std::strings, and nothing else among the predefined operators. It regulates the maximum character count to read.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21