#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    London
    Posts
    2
    Rep Power
    0

    need help with manipulating user input


    Suppose a user enters this

    cin >> 23,4545,576,43,4

    Is there a way I can capture the input and split it into the individual numbers and put them in an integer array in C++. I know there is getline() but it does not allow you to keep the elements as integers. Help will be greatly appreciated.
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    You know you can always use atol() or atoi() to convert a character to a long or integer values.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,624
    Rep Power
    4247
    Are there always going to be a fixed number of elements in the input?
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    London
    Posts
    2
    Rep Power
    0
    yeah, the user always specifies how many elements they are going to enter.

    Thanks Onslaught those functions will come in handy!! :o)
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,624
    Rep Power
    4247
    You could then possibly use cin in a loop and use a char field to extract the commas out of the way, something like this:
    Code:
    #include <iostream>
    int main(void) {
      int nCount, i;
      cout << "Enter number of elements: ";
      cin >> nCount;
      
      int *nElems = new int[nCount];
      char cDummy; 
    
      cout << "Enter elements separated by commas below\n";
      for (i=0; i < nCount-1; i++)
        cin >> nElems[i] >> cDummy;
      cin >> nElems[nCount - 1];
    
      // Now print out the numbers entered
      for (i = 0; i < nCount; i++)
        cout << i << " => " << nElems[i] << "\n";
    
      // clean up
      delete [] nElems;
      return 0;
    }
    Hope this helps!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Here's a slightly more elegant solution:

    Code:
    int* digit;
    int digitno;
    string userinput;
    
    cout << "Enter your number: ";
    cin >> userinput;
    
    digit = (int*)calloc(userinput.length() + 1, sizeof(int));
    
    digitno=0;
    for(int i=0; i < userinput.length(); i++) {
      if (isdigit(userinput[i]))
        digit[digitno++] = atoi(userinput[i]);
    That way the user doesn't need to know how many digits ahead of time.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,624
    Rep Power
    4247
    ClayDowling, the code you've presented won't quite work because atoi works on const char *, not char. So atoi(userinput[i]) won't work. Also it appears that your code is attempting to put each digit in a separate element of the array, which wasn't the original requirement. Interesting approach though :)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Scorpion, thanks for the heads up. Compiler would have caught that in time. So we'll change the last line of the code a bit:

    Code:
    digit[digitno++] = userinput[i] - '0';
    Last edited by ClayDowling; October 31st, 2002 at 01:42 PM.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,624
    Rep Power
    4247
    Actually I think it was individual numbers rather than individual digits, with the numbers being separated by commas on the input line.

IMN logo majestic logo threadwatch logo seochat tools logo