Hi

I have written a program, it doesn't work and i can't figure out why.
It does compile, but it doesn't run.

The whole program:

``````    #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int a;
int j;
int c;
int d;
int dig;
c = 0;
int i;
a = 0;
d = 0;
for (i = 0;i == i;i++) {
a = a + i;
// from this
for (j = 1;j < a; j++){
dig = a / j;
if (isdigit(dig)){
d++;
}
//to this doesn't work
if (d == 500) {
printf("%d\n",a);
system("sleep 10s");
}
else {
d = 0;
}
}
}
return 0;
}
``````

The part which doesn't work should count how many divisors the number 'a' has.

What am i doing wrong?

## All 2 Replies

Your code is difficult to read due to the poor naming of variables and the complete lack of comments. Aside from that it just doesn't make much sense. Trying to make sense of it I added some comments:

``````#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
int a = 0;  // Will be sum(i).
int d = 0;  // You probably try to let this contain the amount of divisors.
int j;      // For-loop variable.
int i;      // For-loop variable.
int dig;    // Will contain how many times a 'j' fits into 'a'.

// Warning: "i == i" will result in an endless loop. Is this what you want?
for (i = 0; i == i; i++)
{
// 'a' will be sum(i).
a = a + i;

// Go past every number smaller than sum(i), starting at 1.
// You might want to change '<' to '<=' or let j start at 0.
for (j = 1; j < a; j++)
{
// dig will contain the amount of times 'j' fits into 'a'.
// This might not be what you want it to do, going by your next statement.
dig = a / j;

// 'isdigit' wants a char and you're providing it with ASCII values now. If 'a' isn't divisible by 'j' dig will contain the highest multitude of
// j smaller than a. (e.g. 5/2 = 2) Search for "integer division" for more information. If you want
// to check for divisibility you could use the module operator, '%'. (e.g. if "x % y" results in 0 then
// x is divisible by y)
if (isdigit(dig))
{
// I think your goal was to let 'd' contain the amount of divisors. This is not what it now contains.
// Instead it will end up being equal to 'a - 1' if a is bigger than 1. So not what you want.
d++;
}

// What are you even trying to do here? What is '500' supposed to represent?
if (d == 500)
{
printf("%d\n",a);
system("sleep 10s");
}
else
{
// Don't you want to reset 'd' for every 'i'?
d = 0;
}
}
}

return 0;
}
``````

I'm not even going to begin looking into what the problem could be before I know what it is you're trying to do.

-edit-
Modified.

isdigit expects a single character. Just one. It's meant to be used to check if a single character is a digit (so the input is expect to have a numerical value in the range zero to 127 for a char, because those are the values a char takes - see http://en.cppreference.com/w/c/string/byte/isdigit and look at that nice table at the bottom). I expect that isdigit is implemented in your system something like this:

``````#define isdigit(c) ((map[c] & FLAG_DIGIT)==FLAG_DIGIT)
``````

where map is an array of some fixed size. Your code manages to feed it many values larger than the array size, so it segFaults eventually when the OS spots you trying to read memory that isn't yours. What are you actually trying to test? Are you trying to test for dig being a single digit? That's not what isdigit does.

You could have identified that the segFault was being thrown by the function isdigit yourself if you used a debugger. The ten minutes it will take you to learn how to use a debugger will save you vast, vast amounts of time in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.