You use the standard C functions strcat()
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
If dd[0] and dd[1] contain the character they entered, the character is NOT the numeric value. (For example in ASCII '0' is 48)
So if the example is dd[0] = '2' and dd[1] = '3' then aa = dd[0] + dd[1]; would give aa the value 50 + 51 or 101.
If you want the numeric value I usually do something like (dd[0] - '0') which in our case would be 2.
So if we did that as we went, we could write
aa = (dd[0] - '0') + (dd[1] - '0')
There, now aa is 2 + 3 which makes aa 5. (wait, didn't you say it should be 23? maybe I missed something.)
I guess I could have done:
aa = 10 * (dd[0] - '0') + (dd[1] - '0')
That would be 23.
You could probably even do it without the array of characters (unless you needed them for something else):
aa = (getinp() - '0');
aa = 10 * aa + (getinp() - '0');
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
char onetwo[3]; /* for one, two, zero chars */
char one, two;
one = getche();
two = getche();
onetwo[0] = one; /* array indicies started from 0 */
onetwo[1] = two;
onetwo[2] = '\0'; /* c-string terminated zero byte */
/* Now you have well-formed c-string */
printf("echo: %s\n",onetwo);
Remember: a single char is not a string in C, a c-string is an array of chars with zero byte at the end of its contents.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Your original program declared aa as an array of characters:
char dd[1], aa[2];
In your most recent post, you treated aa as an integer:
aa = dd[0] + dd[1]; // aa will then have 23
if (aa > 30)
So I did too. It is not permitted to assign to the array.
Change your declaration to:
char dd[2];
int aa;
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
>What does that means ?
It means you need to learn a little be more about array of chars and what they represent.
As far as the compiler is concerned aa is not a proper variable to re-assign the kind effect resulted from 10 * (dd[0] - '0') + (dd[1] - '0') one = getche();
two = getche();
No reason to use a non-portable function when getchar() could have do.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
The 'syntax' question you were ask would be easy to answer if you had 2 strings instead of 2 characters.
In C, you make your own strings if you're taking input one character at a time.
If you wrote:
char dd[3];
dd[0] = getinp();
dd[1] = getinp();
dd[2] = '\0';
Now dd is a real C string. If you wanted to assign it to another string (of sufficient size or bad things happen) you could strcpy(otherstring, dd);
But your problem description implied that you don't really need the string, you want the number that it would represent (which is why my previous posts look like they did.)
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
No reason to use a non-portable function when getchar() could have do.
Yes, of course, but there is getche in the original post.
Let him/her shoot him/herself in the foot ;)...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
You have been given several suggestions. I haven't seen anything that you've tried, all I've seen are comments that it isn't working -- and I followed those up with what was wrong and I thought I told you how to fix it.
Post the code as it now exists and please confirm whether you want a string with the two characters in it ("23"), or the numeric value of the number formed by the two characters (23). If you want to perform a numeric comparison (say to see if the value they entered was < 30) then you need the numeric value.
Please enclose your code in code tags with a language specifier:
[code=c]
/* Your code here */
[/code]
PS- It may have felt like 2 days, but your first post was 13 hours ago.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116