# Get number from pattern (a,b)?

• (2 Pages)
• 1
• 2

## 19 Replies - 953 Views - Last Post: 30 May 2012 - 11:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281177&amp;s=5ae9b169af7ffa1278d7fecc65570e96&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

# Get number from pattern (a,b)?

Posted 30 May 2012 - 01:37 AM

The input should be (a,b) a and b are numbers! I should get those numbers from the pattern to an integer variable...
Example:
---------------------------
User input is: (2,3)
Int a = 2
Int b = 3
---------------------------
Is it possible???
Is This A Good Question/Topic? 0

## Replies To: Get number from pattern (a,b)?

### #2 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 01:42 AM

Yup. You can go about it in two ways :
1. Take the input as string and then get the required components.
2. Use a simpler method
```int a , b ;
char c ;
cin >> c >> a >> c >> b >> c ;
cout << endl << a << endl << b ;

```

cin will do all the work for you, while you just have to sit back and enjoy.

### #3 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 01:54 AM

1. im using C Not C++.
2. cant use any Loops or existing Functions.
3. i can use only recursion.

Im thinking if i will gets(somestring); and send it to a function that check if it is good (no problem doin that) but then how can i get the numbers, they are not integer they are Chars...

### #4 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 02:01 AM

Actually, the code which I sent requires a little editing to make it a C code. Here is a C equivalent
```int a , b ;
char c ;
scanf("%c %d %c %d %c",&c,&a,&c,&b,&c);
printf("\nThe numbers are %d and %d\n",a,B)/>;

```

Are you not even allowed to use scanf and printf ??!!??!! Recursion will make the program unnecessarily complicated.

This post has been edited by aresh: 30 May 2012 - 02:02 AM

### #5 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 02:14 AM

im not familiar with C++ syntex.
nice thank u!

### #6 baavgai

• Dreaming Coder

Reputation: 6379
• Posts: 13,629
• Joined: 16-October 07

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 06:57 AM

I'd avoid the dummy vars. This will work:
```char *userInput = "(2,3)";
int a, b ;

sscanf(userInput, "(%d,%d)", &a, &b );
printf("User input is: %s\nInt a = %d\nInt b = %d\n", userInput, a, b );

```

### #7 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 07:09 AM

the user input should be:
(2,3), i need to check if the pattern is right like:
( next a next , next b next )?????

### #8 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 07:48 AM

tomas7470, on 30 May 2012 - 04:54 AM, said:

3. i can use only recursion.

Since the point of the assignment seems to be to learn recursion, using scanf or ssanf will not meet that requirement.

tomas7470, on 30 May 2012 - 04:54 AM, said:

... but then how can i get the numbers, they are not integer they are Chars...

If a and be are limited to single digits it's easy -- all that requires is one simple arithmetic operation based on the ascii values of the chars.

If they can be multi-digit numbers it's a bit more complicated but you can design your function to convert the chars to int one decimal place at a time (this can be done recursively) or you can use the stdlib function atoi if your assignment allows this.

### #9 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 08:18 AM

how can i convert char to int?

### #10 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 08:29 AM

A char is "like" an int, except that:
(1) it is only 1 byte, whereas an int is generally 4 bytes (depending on your computer and OS, but 4 is most common these days).
(2) and therefore a char is limited to values in the range -128 to 127.

Look at the ascii chart and you will see that each char has a decimal number associated with it. That's the numerical value that 'represents' that character. So for example, the digit '1' has the value 49. So if you subtract 48 from the char '1', the result is the integer 1.

Or to make it even simpler, you can subtract the char '0' from the char '1' and the result is the integer 1.

### #11 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:00 AM

i didnt get that part from char 48 to int 1...?
thank u

### #12 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:06 AM

You must realize that everything -- every kind of data -- is represented in the computer as numbers.

So the number 48 is understood by your program to "mean" the character '0' when you tell it to interpret it as a char.

And the number 49 is understood by your program to "mean" the character '1' when you tell it to interpret it as a char.

And so on ...

The ascii chart tells you the numerical values that are used under the ascii system to represent the various printed (and control) characters.

### #13 baavgai

• Dreaming Coder

Reputation: 6379
• Posts: 13,629
• Joined: 16-October 07

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:39 AM

tomas7470, on 30 May 2012 - 04:54 AM, said:

1. im using C Not C++.
2. cant use any Loops or existing Functions.
3. i can use only recursion.

Yeah, didn't see that last one.

Step one, print a string using recursion. That will at least give you an idea of what you're dealing with. Your sig should looks something like:
```void showStr(char *);

```

Know that every c-string ends in 0, so you stop calling yourself when you hit it.

If you can write that, then you're halfway there. Or one quarter, if your numbers can be greater than one digit. Still, it's a start.

Good luck.

### #14 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:46 AM

As r.stiltskin said, all characters are stored as their ASCII values inside your computer. So, 'A' is stored as 65, 'a' is stored as 97, and similarly '1' (character 1, not integer 1) is stored as 49. So, to convert it to integer, subtract 48 from it (49-48=1) or subtract '0' (having ASCII value 48) from '1'.

### #15 tomas7470

Reputation: 1
• Posts: 54
• Joined: 19-February 09

## Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 10:24 AM

so if i want that a for example will be 2 i need to do:
printf("%d", a-48);
and it will give me: 2..
??