Thread: Extracting values...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    11

    Extracting values...

    I have what I believe to be a simple question for those that are experienced. I hope you guys can help me out.

    How do you take part of a value and break it up to be used as a different value? For example, Value = California (100) is the value to be used. However, how can I take pieces of that value, such as split up California and 100 to be used somewhere else? Ultimately, I want to be able to take the value up until the parentheses, use that value separately, then take what is actually inside the parentheses, and then use that value independently as well.

    I hope this makes sense. Thank you all for your response. And this is not for any kind of assignment whatsoever. I'm just trying to learn the language as part of ongoing work.

    Thanks again!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I take it "California (100)" is a string. You might want to look at using strchr.
    If you understand what you're doing, you're not learning anything.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like you've already got it worked out. Go through the input until you reach the '=', and ignore everything before it. Then, go through until you reach '(', copying that some place. Then skip past that, and copy everything until you reach the ')'.

    There are many ways to do it. But that's an easy to understand method. Use a couple of arrays to hold what you're copying. This assumes you've got the data some place as a string. Be it a file or an array or what not.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    in some situations, you can scan using sscanf().
    The format string must be carefully prepared though.
    For example, this one may work for you:
    Code:
    	const char *str = "Value = California(100)";
    	char	v[100];
    	int	x;
    
    	sscanf(str, "%*s = %99[A-Za-z](%d)", v, &x);
    	printf("[%s] [%d]\n", v, x);

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    11
    Cool. Thanks for all the replies. I really appreciate it!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  3. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 09:24 PM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM