I am trying to use the mod operator '%' and seem to get incorrect results with it in my programs. There are no errors, the program will compile fine, but the results it returns are incorrect.
I understand that there is no special library that I need to include to use the mod operator, so it should be working by default, isn't it so? I suppose the other likely explanation is that I am doing something wrong in terms of syntax when I try to use it, but I've looked at plenty of examples and I do not see something out of place.
Here's a sample program I seem to get problematic results out of:
include <iostream>
include <conio.h>
int main()
{
int a;
int b;
int x;
x=(a%b);
scanf("%d",&a);
scanf("%d",&b);
printf("%d",x);
getch();
return 0;
}
Any feedback would be greatly appreciated.
With best wishes,
Okaya
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
does not as you appear to think define some unbreakable relationship between a, b, and x. It simply performs the operation on the current values and assigns teh result to x. Since at that point a and b have undefined value, the result x will be undefined. The values you later assign to a and b will not have any effect on x.
The order of code should be:
scanf( "%d", &a ) ;
scanf( "%d", &b ) ;
x = ( a % b ) ;
printf( "%d", x ) ;
You are not the first person to have posted code like this here or on other forums, but it beats me how anyone could thing that it would work like that. C is not that kind of language.
Not related but your code is not correct in other ways; printf() and scanf() are declared in <stdio.h> or in C++ <cstdio>, you have included <iostream>, but not used any iostream objects (or indeed written anything that is otherwise not plain C code). You got away with int because <iostream> itself includes <cstdio> for its own purposes. But that is not a given in all library implementations.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello to all,
I am trying to use the mod operator '%' and seem to get incorrect results with it in my programs. There are no errors, the program will compile fine, but the results it returns are incorrect.
I understand that there is no special library that I need to include to use the mod operator, so it should be working by default, isn't it so? I suppose the other likely explanation is that I am doing something wrong in terms of syntax when I try to use it, but I've looked at plenty of examples and I do not see something out of place.
Here's a sample program I seem to get problematic results out of:
include <iostream>
include <conio.h>
int main()
{
int a;
int b;
int x;
x=(a%b);
scanf("%d",&a);
scanf("%d",&b);
printf("%d",x);
getch();
return 0;
}
Any feedback would be greatly appreciated.
With best wishes,
Okaya
Thank you for these helpful comments, Clifford. Needless to say, your directions result in the solution of the problem I was expriencing.
Thank you also for the note regarding my included libraries, which is definitely a good point. Your feedback is most helpful.
Okaya
Your code is nonsense. The expression
x=(a%b);
does not as you appear to think define some unbreakable relationship between a, b, and x. It simply performs the operation on the current values and assigns teh result to x. Since at that point a and b have undefined value, the result x will be undefined. The values you later assign to a and b will not have any effect on x.
The order of code should be:
scanf( "%d", &a ) ;
scanf( "%d", &b ) ;
x = ( a % b ) ;
printf( "%d", x ) ;
You are not the first person to have posted code like this here or on other forums, but it beats me how anyone could thing that it would work like that. C is not that kind of language.
Not related but your code is not correct in other ways; printf() and scanf() are declared in <stdio.h> or in C++ <cstdio>, you have included <iostream>, but not used any iostream objects (or indeed written anything that is otherwise not plain C code). You got away with int because <iostream> itself includes <cstdio> for its own purposes. But that is not a given in all library implementations.
Clifford