0

I think that the problem is in lines 59-63

#include <stdio.h>
#include "genlib.h"
#include "ctype.h"
#include "simpio.h"
#include "string.h"


typedef struct{
    char name[30];
    long sell;
    double supply;

}domiT;


void ypologismoi(domiT stx[],int *p,FILE *infile);
void emfaniseis (domiT stx[],int *p,FILE *outfile);

main()
{

    int p;
    FILE *infile,*outfile;
    domiT stx[100];

ypologismoi(stx,&p,infile);
emfaniseis(stx,&p,outfile);    


system("pause");    
}
void ypologismoi(domiT stx[],int *p,FILE *infile)
{
    char file[15];
    int sarwseis;
    char comments[68];
    char name[30];
    char termch;
    long sell;

    while(TRUE)
    {
        printf("Enter file's name: ");

        gets(file);

    infile=fopen(file,"r");
    if(infile!=NULL) break;
    printf("Cannot open file %s !! Try againn",name);

   }


   *p=0;


    while(TRUE)
    {
        sarwseis=fscanf(infile," %30[^,], %ld,%68[^,],%c",name,&sell,comments,&termch);

        if(sarwseis==EOF) break;
        if(sarwseis!=4 || termch!='n')
        printf("ERROR try to find the error plz! " );


        strcpy(stx[*p].name,name);
        stx[*p].sell=sell;
        if(sell>=300000)
        stx[*p].supply=sell*5/100.0;
        else
        stx[*p].supply=sell*3/100.0;

        (*p)++;

    }

}        

void emfaniseis (domiT stx[],int *p,FILE *outfile)
{

    double sum;
    char newname[15];
    int i;



    sum=0;

    for(i=0;i<*p;i++)
    sum+=stx[i].supply;


    printf("Enter the NEW file's name: ");
    gets(newname);
    outfile=fopen(newname,"w"); 

    fprintf(outfile," %-30s %10s n","ONOMATEPONYMO","PROMHTHEIA");
    for(i=0;i<40;i++)
    fprintf(outfile,"%c","-");

    fprintf(outfile,"n");

    for(i=0;i<*p;i++)
    fprintf(outfile,"%-30s  %10g n",stx[i].name,stx[i].supply);

    for(i=0;i<40;i++)
       fprintf(outfile,"%c","-");

       fprintf(outfile,"n");

       fprintf(outfile,"%-30s %10g","SYNOLO PROMHTHEIWN:",sum);


}

Edited by deceptikon: Changed to a discussion thread

6
Contributors
10
Replies
12
Views
4 Years
Discussion Span
Last Post by tinstaafl
0

We would have to get a copy of what file you're working with to even begin to understand what your problem is.

Edited by Tumlee: Read up a little more on fscanf(), removed a sentence.

0

If you think the problem is in lines 59~63, how about adding printf() to print out something at line 60, and between line 61 and 62? I mean if the bug is really there. (Simple debugging approach)

Edited by Taywin

0

indeed it's an infinity loop because of while(TRUE)

and it will only exit the loop if it statistfies

`if(sarwseis==EOF) 
        break;`

coz the other ifs are changing the value of the left variable

and on lines 62-67

`   if(sarwseis!=4 || termch!='n')
        printf("ERROR try to find the error plz! " );
        strcpy(stx[*p].name,name);
        stx[*p].sell=sell;`

is it intentional that you didn't add open and close braces

how about adding printf() to print out something at line 60, and between line 61 and 62? I mean if the bug is really there. (Simple debugging approach)

you should try to see the value of the variable sarwseis

0

Could the file still be open and without an EOF marker? I noticed that the sub that writes to the outfile doesn't close the stream, if the same file is then read would it have an EOF marker? just a thought.

Edited by tinstaafl

0

Had another thought, see if this makes a difference:

 while(!infile.eof())
{
    sarwseis=fscanf(infile," %30[^,], %ld,%68[^,],%c",name,&sell,comments,&termch);
    if(sarwseis!=4 || termch!='n')
        printf("ERROR try to find the error plz! " );
    strcpy(stx[*p].name,name);
    stx[*p].sell=sell;
    if(sell>=300000)
        stx[*p].supply=sell*5/100.0;
    else
        stx[*p].supply=sell*3/100.0;
    (*p)++;
}
0

I've found the problem. Is in line 59-60 . I have to type in scanf ...%68[^\n] instead of %[^,]

This question has already been answered. 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.