please help

This is a discussion on please help within the C Programming forums, part of the General Programming Boards category; Originally Posted by ali_1234 if i use return one after fscanf then its reading only the fist number from input ...

  1. #16
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by ali_1234 View Post
    if i use return one after fscanf then its reading only the fist number from input file and after i press enter the program terminates...
    i m using borland as my compiler
    what are you talking about?

    fscanf reads from the "indata.txt"

    what it has to do with your pressing enter?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    i am getting an output file now but the problem is min value is always zero and max mum value is some number...0814...
    now again im stuck ...

  3. #18
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by ali_1234 View Post
    i am getting an output file now but the problem is min value is always zero and max mum value is some number...0814...
    now again im stuck ...
    post your last code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int i,a[10],val,n=0,max,min;
    	FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    	
    	else
    	{
    		while(fscanf(fi,"%d",&val))
    		{
    			a[n]=val;
    			n=(n+1);
             printf("%d %d\n", val, n);
             getch ();
    
    
         
    		max = a[0];
    		min = a[0];
    		
    		for (i = 0; i <= 10; i++)
    		{
    			if (a[i] > max)
    			{
    				max = a[i];
    			}
    			else if (a[i] < min)
    			{
    				min = a[i];
    			}
          }
    		if((fo=fopen("result.txt","w"))==NULL)
    		{
    			printf("Error in opening the file!!!\n");
    			return 1;
    		}
    		else
    		{
    			fprintf (fo,"Maximum element in an array : %d\n", max);
    			fprintf (fo,"Minimum element in an array : %d\n", min);
    		}
    		
    		fclose(fo);
    		fclose(fi);
    		getchar ();
    		return 0;
    	   }
             }
    }

  5. #20
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    your indentetion suxx big deal - you do not even see simplest bugs

    also - when you have return inside if - you do not need else - it just adds unneded indentation level

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int i,a[10],val,n=0,max,min;
    	FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    
    	while(fscanf(fi,"%d",&val) == 1)
    	{
    		a[n]=val;
    		n=(n+1);
    		printf("%d %d\n", val, n);
    		if(n == 10)
    			break;
    	}
    
    
    	max = a[0];
    	min = a[0];
    
    	for (i = 0; i <= 10; i++)
    	{
    		if (a[i] > max)
    		{
    			max = a[i];
    		}
    		else if (a[i] < min)
    		{
    			min = a[i];
    		}
    	}
    	if((fo=fopen("result.txt","w"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    	fprintf (fo,"Maximum element in an array : %d\n", max);
    	fprintf (fo,"Minimum element in an array : %d\n", min);
    
    	fclose(fo);
    	fclose(fi);
    	getchar ();
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    thanks people for ur kind support ...
    and vart i am really sorry i am jus a beginner and my programming teacher sux .. so i dont know a damn about it .. wht evr i have studied is by my self ... i m really thank full for ur support cudn't do this without u ppl !!!..

    THANK YOU PPL !!!!........... spl VART !!

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by ali_1234 View Post
    thanks people for ur kind support ...
    and vart i am really sorry i am jus a beginner and my programming teacher sux .. so i dont know a damn about it .. wht evr i have studied is by my self ... i m really thank full for ur support cudn't do this without u ppl !!!..

    THANK YOU PPL !!!!........... spl VART !!
    BTW - you still have a bug in your for loop - you access 11 elements whule have only 10 in the array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21