Back to General discussions forum
hey guys i'm trying to distribute through the modulate and hitting a dead end. (code in c)
int main(int argc, char* argv[]) {
char op[100];
int x,stepcount;
int y[100];
stepcount = 0;
scanf("%d", &x);
for(;;) {
scanf("%c %d", &op[stepcount], &y[stepcount]);
if(op[stepcount] == '%') {stepcount++;break;}
stepcount++;
}
int term = y[stepcount-1];
for(int i = 0; i < stepcount; i++) {
if(op[i] == '*'){x = x * y[i] % term;}
if(op[i] == '+'){x = (x + y[1]) % term;}
}
x = x % term;
printf("%d", x);
return 0;
}
Hi,
After your second scanf, try adding the following line, so you can see what you are reading
printf("Read %c %d\n",op[stepcount],y[stepcount]);
Hi crashmatusow!
One thing I will also suggest is that now is the time to learn the power of a debugger. Quandray has told you to insert what's called a trace print. This can work for simple problems, but eventually your code will be littered with these traces for difficult problems/bugs. Since CodeAbbey is output sensitive (ie. printing too much is just as wrong as not printing the right thing), forgetting to remove just one of these traces can cause you to fail the problem. Oops!
Therefore, I recommend learning a debugger. It's well worth your time investment.
Here are the canonical command line oriented debuggers: (Disclaimer: I'm a contributor to the LLVM/LLDB project, so I'm partial to that one.)
But what if you hate the command line? We've got graphical debugger interfaces built on GDB, LLDB or the Visual Studio IDE!
If you had used a debugger on your particular question, you'd have seen that line 7 is not doing what you think it should be doing, and is thus breaking your control flow in line 12. How to fix it? I leave that up to you...