0

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");
}

Edited by you207

2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by deceptikon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.