The Dink Network

Help With Programming

September 2nd 2016, 04:22 AM
milder.gif
duckhater
Peasant He/Him India
From The Depths Of Tartarus Itself 
I just started to learn C and I've reached a few hurdles(and apparently my teacher doesn't know what to do either). I was wondering if you guys could help.
1. When I use the modulus operator for floating point variables, it gives me a message that says "Illegal Use Of Float" or gives me a wrong result(when I use both int and float for the operation).
2. I couldn't for the life of me figure out how to add two char and display their sum as the sum of their ASCII values. What I typed is probably wrong but it will give an idea about what I tried to do:

char a, b;
int c;
printf ("Enter Characters");
scanf ("% c% c", &a,&b);
c = a + b;
printf("%d", c);


The output should be something like:
Enter Two Characters:
The Sum Of their ASCII Values Is:


Thanks in advance
September 2nd 2016, 12:05 PM
peasantmp.gif
Skurn
Peasant He/Him Equatorial Guinea duck bloop
can't flim flam the glim glam 
and apparently my teacher doesn't know what to do either

wow, totally unqualified teachers in india too? guess it's just universally accepted that cheap > quality.
September 3rd 2016, 12:52 PM
milder.gif
duckhater
Peasant He/Him India
From The Depths Of Tartarus Itself 
That second one is just something I thought up so I guess they weren't prepared when I asked
September 3rd 2016, 02:07 PM
burntree.gif
Striker
Noble She/Her United States
Daniel, there are clowns. 
It's been a while since I've programmed anything (and I know C++ instead of C), so I'll answer these questions to the best of my ability and what I recall:

1) Considering modulus works with remainders from division, it can *only* be used when all inputs and outputs are int (you can't get remainders from operators that have decimal points).

2) I don't quite have the knowledge of C to be able to understand what you're trying to do in the code, but here's how I would approach the problem:

I would likely stick with the same # of variables, but would create a separate int function for converting characters into their ASCII int values (you can't just add two characters together and have them come out as an int). It would something like this:

int ascii_convert(char character)
{
int result;
//code to convert character to int value goes here
return result;
}


So, instead of c = a + b, it would look something like this:

c = ascii_convert(a) + ascii_convert(b);


Followed by however you display stuff using C.

This, of course, assumes that you already don't have an available function for converting ASCII char to int... in which case, I would ask why you are even asking this question?
September 4th 2016, 01:33 AM
milder.gif
duckhater
Peasant He/Him India
From The Depths Of Tartarus Itself 
Thanks for the detailed answer, man. I really appreciate it. I asked the same question on Reddit. For the first one they just told me to use another header and the 'fmod' function for finding the remainder of floating point values, your answer explained why it didn't work for the modulus (thanks again).
For the second one they just told me to add a space between the % c's and to omit conio.h and its functions. Worked like a charm.