Hey guys, been to the forum a couple times but never posted. Thought I'd give it a try.

So my problem is basically this.

I'm to create a program which reads a line in this form

"-6 1 2 3 6"
"-10 1 2 5 10"

-6 being a perfect number, and 1 2 3 6 being its factors.

if the numbers on the line (except the last one) are equal to the first number it is perfect.

I'm having trouble parsing the 1 2 and 3 out of the line. 6 can be ignored because only the factors not equal to the number are important. I can successfully parse the -6 using sscanf but Im stumped as to how to go about getting the 1 2 and 3 out of the line.

Any ideas?

Here's one way with sscanf:

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

int main(void)
{
const char *src = "-6 1 2 3 6";
int pos = 0;
int n = 0;
int x;

while (sscanf(src + pos, "%d%n", &x, &n) == 1) {
printf("%d\n", x);
pos += n;
}

return 0;
}``````

Perhaps I could've been more specific. The line i presented is an example of what a line would look like.

I am reading in a file with anywhere from 100k to 550k of those lines.
The file was generated by another program which puts a number and all of its factors in a line as I specified in my original post. I'm not sure if your solution would work because line lengths are variable.

"-20670 1 2 3 5 6 10 13 15 26 30 39 53 65 78 106 130 159 195 265 318 390 530 689 795 1378 1590 2067 3445 4134 6890 10335 20670"

That is the line for 20670. I hope I've made myself more clear.

Would that solution still work?

Sorry for the vague first post. I'm new here :P

Here's one way with sscanf:

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

int main(void)
{
const char *src = "-6 1 2 3 6";
int pos = 0;
int n = 0;
int x;

while (sscanf(src + pos, "%d%n", &x, &n) == 1) {
printf("%d\n", x);
pos += n;
}

return 0;
}``````

>Would that solution still work?
Yes, and it would have saved us both some time if you had simply tested this for yourself, seeing as how I gave you a complete program. Do you not have a compiler installed?

I do have a compiler. I just looked at the code and figured it could never do what I wanted due to its simplicity. It works beautifully however, and I feel quite silly for questioning you.

Thanks for the help! Ill mark this solved!

>I just looked at the code and figured it could never do what I wanted due to its simplicity.
Isn't elegant code a wonderful thing? :)

Hey, i made a C program that it says if a number is perfect or not

``````int main()
{
/** Variables */
int div=1; // divisor
int res;

printf("ingrese la nota de un alumno\n");
scanf("%d",&N);

/** Validaciones*/
if(N==0){ printf("'N' no puede ser 0\n");}
if (N<0){N=-N;}
/**------------------------------------*/

while(N>0){
/** Calculo*/
if(div>=N){printf ("No es perfecto"); return 0;}

res=N%div;

if (res==0) {
printf("Los factores son %d \n",div);
x=div;
}

/**------------------------------------*/

/** Si el numero es perfecto*/

return 0;}
/** Actualizacion
*/
div+=1;

}
return 0;
}
``````

>Hey, i made a C program that it says if a number is perfect or not
Do you want a gold star or something? :icon_rolleyes:

no?

Ok

No, but you did post completely irrelevant code to a thread with a "look at what I did!" tone. I could only conclude that you were trying (and failed miserably) to show off.

theme:
Sum a line of integers to check for a perfect number.

question: Any ideas?