The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
need help with manipulating user input
Discuss need help with manipulating user input in the C Programming forum on Dev Shed. need help with manipulating user input C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 18th, 2002, 05:20 AM
|
|
Junior Member
|
|
Join Date: Oct 2002
Location: London
Posts: 2
Time spent in forums: < 1 sec
Reputation 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.
|

October 18th, 2002, 07:49 AM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
|
You know you can always use atol() or atoi() to convert a character to a long or integer values.
|

October 18th, 2002, 10:35 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Are there always going to be a fixed number of elements in the input?
|

October 19th, 2002, 02:26 PM
|
|
Junior Member
|
|
Join Date: Oct 2002
Location: London
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
yeah, the user always specifies how many elements they are going to enter.
Thanks Onslaught those functions will come in handy!!  )
|

October 19th, 2002, 08:20 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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!
|

October 31st, 2002, 10:57 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
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/
|

October 31st, 2002, 12:58 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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 
|

October 31st, 2002, 01:39 PM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
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.
|

October 31st, 2002, 01:43 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Actually I think it was individual numbers rather than individual digits, with the numbers being separated by commas on the input line.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|