My program is nearly done.But my function SumN doesnt print the correct output.
SumN have to output the Sum of numbers that belong to the interval [a;b].
Example:
input
a=1
b=10
1
4
13
45
10
output
sum=15

but im getting 60 and in should be 15.

Can sameone help ?

#include "stdafx.h"
int CountEven(FILE *fint);
void SumN(FILE *fint,int a ,int b );
void MyFilePrint(FILE *fint );

int _tmain(int argc, _TCHAR* argv[])
{   FILE *fint;
    int m,i=0,a,b,number;

    if((fint=fopen("a.txt","w"))!=NULL){
        printf("vavedete interval [a;b]\na=");
        scanf("%d",&a);
        printf("b=");
        scanf("%d",&b);
        printf("Vavedete realni chisla, za krai vavedete CTRL/Z:\n");
        m=scanf("%d",&number);

        while(m != EOF){
            fprintf(fint,"%d",number);
            fputc(' ',fint);
            if(++i%2==0)fputc('\n',fint);
            m=scanf("%d",&number);
        }

    fclose(fint);
    }else printf("Greshka pri otvarianeto na faila");
        CountEven(fint);
        SumN(fint , a , b );
        MyFilePrint(fint);
    return 0;
}
int CountEven(FILE *fint)
{   int  even=0, number; 

    if((fint=fopen("a.txt","r"))!=NULL){
        while(fscanf(fint, "%d", &number) != EOF){ 

            if (number%2==0)  
                even++;   

        }  

        fclose (fint); 
    }else printf("Greshka pri otvarianeto na faila");

    return printf("Ima %d chetni chisla vav faila.\n", even); 
}
void SumN(FILE *fint,int a ,int b )
{   int  number,m; 
    int *sum=0;
    if((fint=fopen("a.txt","r"))!=NULL){
        m=fscanf(fint, "%d", &number);
        while( m != EOF){ 

            if ((number>=a) && (b>=number)) 
                sum=sum+number;

            m=fscanf(fint, "%d", &number);
        }  

        fclose (fint); 
    }

    printf("Sumata na chislata prinatlejashti na intervala ot %d do %d e %d\n",a,b,sum); 
}
void MyFilePrint(FILE *fint)
{   int m , number , i=0;
    if((fint=fopen("a.txt","r"))!=NULL){
    m=fscanf(fint,"%d",&number);

        while(m != EOF){
            printf("%d",number);
            printf(" ");
            if(++i%3==0)printf("\n");
            m=fscanf(fint,"%d",&number);
        }
    }else printf("Greshka pri otvarianeto na faila");
}

Dani AI

Generated

— the root cause is that the accumulator was declared as a pointer. 's remark about int *sum=0; is correct: pointer arithmetic moves an address by n * sizeof(int), it does not perform numeric addition, and printing a pointer with %d produces an unrelated integer. The accumulator must be an integer initialized to zero.

Beyond that single change, make the file-reading and function interfaces more robust: read with while (fscanf(...) == 1) (safer than testing != EOF), use the logical && for the interval check, and either open the file once in main and pass an open FILE* to helpers or pass the filename into helpers so each function manages its own fopen/fclose. Also note CountEven currently returns the value returned by printf() (the number of characters printed); return the actual count if the value is needed.

A compact, safe replacement for the summing helper (keeps filename-based interface) looks like this:

void SumN(const char *filename, int a, int b)
{
    FILE *f = fopen(filename, "r");
    if (!f) { printf("Error opening %s\n", filename); return; }

    int n, sum = 0;
    while (fscanf(f, "%d", &n) == 1) {
        if (n >= a && n <= b)
            sum += n;
    }
    fclose(f);
    printf("Sum in [%d..%d] = %d\n", a, b, sum);
}

Quick troubleshooting notes: print each number as it is read to verify the condition is triggered, confirm a and b are initialized in every path, and use a bigger integer type (long long) if sums can overflow. On Windows end stdin with Ctrl+Z (Unix: Ctrl+D) when testing.

int *sum=0;

sum is a pointer yet you use it as an integer. It shouldn't be a pointer in this case.

Be a part of the DaniWeb community

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