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???
19 Replies - 342 Views - Last Post: 30 May 2012 - 11:27 AM
Replies To: Get number from pattern (a,b)?
#2
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
cin will do all the work for you, while you just have to sit back and enjoy.
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
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 01:54 AM
Thank for Reply:
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...
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
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
Are you not even allowed to use scanf and printf ??!!??!! Recursion will make the program unnecessarily complicated.
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
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 02:14 AM
im not familiar with C++ syntex.
didnt think about that!
nice thank u!
didnt think about that!
nice thank u!
#6
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
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 )?????
(2,3), i need to check if the pattern is right like:
( next a next , next b next )?????
#8
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 07:48 AM
tomas7470, on 30 May 2012 - 04:54 AM, said:
Thank for Reply:
3. i can use only recursion.
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
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 08:18 AM
how can i convert char to int?
#10
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.
(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
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
thank u
#12
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.
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
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.
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
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
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..
??
printf("%d", a-48);
and it will give me: 2..
??
|
|

New Topic/Question
Reply




MultiQuote





|