Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 1st, 2006, 8:00 AM   #1
ingek
Newbie
 
Join Date: Nov 2006
Posts: 1
Rep Power: 0 ingek is on a distinguished road
Extracting numbers from a string

Hi there,

I'll get straight down to the point:

I need my program to accept a line as input - that's simple: fgets does exactly that - but then I need to extract the numbers contained in the line into the appropriate variables. Say there are 3 numbers in the line, so the line looks like this: "23 43 54". I'd want to have a=23, b=43 and c=54.

Is there any function that would extract the numbers for me? Or do I have to do it 'manually'?

Thanks for any input,
Ingek
ingek is offline   Reply With Quote
Old Nov 1st, 2006, 8:12 AM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
try this for C: LINK
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 1st, 2006, 8:46 AM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>Or do I have to do it 'manually'?
'Manual' is more or less what C is about. So yes, fgets the line and pull out your trusty strtol to extract the numbers.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is online now   Reply With Quote
Old Nov 1st, 2006, 6:59 PM   #4
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 4 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
I'm not sure on the correct syntax but look up Split. You'll call it like string.Split(" ") or something where String is the Var. What it does is create an array and splits the input
I'm not sure on this being the correct syntax but here is an idea:
string MyInput, InputArray(); //declare the variables
MyInput << Cin; //Assing the input to the var MyInput
InputArray = MyInput.Split(" "); //Split MyInput at " " assign it to the array InputArray
PhilBon is offline   Reply With Quote
Old Nov 1st, 2006, 7:54 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Ummmm, this is the C forum...please be careful when advising those seeking help.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 2nd, 2006, 4:53 AM   #6
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5 lectricpharaoh will become famous soon enough
If I were you, I'd read it in manually like you do, then break it up into substrings and individually convert those. You can use strtok() to grab each substring, and then atoi(), strtol(), strtoul(), or strtod() to parse each number. You can read up on these functions here.

You can either grab all the substrings (you'll need a pointer variable to store each) and then parse the numbers, or you can grab the first substring, parse it, grab the next, and so on. The second method seems more robust to me, and means you'll only need a single pointer to store the substring, but you still need a place to put all the numbers. That begs the question: are you receiving an arbitrary quantity of numbers, or are you limiting it (and thus, tossing any extra numbers)? If you can expand the second method to also process the numbers (ie, substring then parse then process then toss), you might only need a single variable that you just re-use (only works it they're of a like type, mind you).

Don't forget to test for errors in user input, either. I advise not using atoi(), as it returns zero on failure, leaving you not knowing if it returned a zero because it failed, or because the user actually entered a zero. The strto...() functions, on the other hand, return an error code, and fill in the number via a passed-in pointer, so they provide better error detection.
Quote:
Originally Posted by DaWei
Ummmm, this is the C forum...please be careful when advising those seeking help.
You're getting sloppy, DaWei (or at least less cantankerous). You didn't call him on the god-awful code he posted, which no C++ compiler I know of would accept, nor on the lack of code tags.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 2nd, 2006, 8:44 AM   #7
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>I advise not using atoi(), as it returns zero on failure
Not to mention it invokes undefined behavior if the input string can't be represented as an integer.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is online now   Reply With Quote
Old Nov 2nd, 2006, 9:27 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I didn't call him on bad C++ code because there was little reason to presume it WAS C++ code. I've never heard of "assing the input." I am trying to be less cantankerous, yes; there is no assurance that I will succeed to the extent that those who emit asininities would like.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 6:45 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM
function solomon_13000 Java 6 Apr 3rd, 2005 12:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC