Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include<stdio.h>
int main()
{
 /*to print the frequency of odd digits*/
   int i , n ,num,c=0;
   FILE *f1,*f2;
   printf("enter the no. of positive nos.\n");
   scanf("%d",&n);
   printf("enter the numbers:\n");
   f1=fopen("DATA.C","w");

   for(i=0;i<n;i++) 
   {
     scanf("%d",&num);
     putw(num,f1);
   }
   fclose(f1);

   f1=fopen("DATA.C","r"); 
   f2=fopen("ODD.C","w");

   while((num=getw(f1))!=EOF)
     if(num%2!=0)
       putw(num,f2);

   fclose(f2);
   
   printf("total frequency is::\n");

   if (num%2!=0) c++; 

   printf("%d",c);  
   return c;


if we put c++ below putw(num,f2) then the output is different but of same. why?
Above one correct

C++
#include<stdio.h>
int main()
{
   int i , n ,num,c=0;
   FILE *f1,*f2;
   printf("enter the no. of positive nos.\n");
   scanf("%d",&n);
   printf("enter the numbers:\n");

   f1=fopen("DATA.C","w");

   for(i=0;i<n;i++) {
     scanf("%d",&num); 
     putw(num,f1);
   } 

   fclose(f1);
   f1=fopen("DATA.C","r");
   f2=fopen("ODD.C","w");
   
   while((num=getw(f1))!=EOF)
     if(num%2!=0) 
        putw(num,f2);
     c++; 
     /*here is the fault*/
     fclose(f2);
    
     printf("total frequency is::\n");
     printf("%d",c);

     return c;


please check fault...

What I have tried:

I've tried running both sets of code
Posted
Updated 8-Dec-16 22:37pm
v3
Comments
CPallini 9-Dec-16 2:41am    
I strongly doubt the first version is correct. Both pieces of your code looks flawed to me.

Your code is was rather unreadable (thanks to Chris for editing; I tried it before answering here but failed).

But I guess the solution is
C++
while((num=getw(f1))!=EOF)
{
    if(num%2!=0) 
    {
        putw(num,f2);
        c++;
    }
}

while your code seems to be
C++
while((num=getw(f1))!=EOF)
    if(num%2!=0) 
        putw(num,f2);
c++;

Note that using proper indentation shows what is happening.
 
Share this answer
 
v2
Comments
CPallini 9-Dec-16 2:38am    
Thta's it. 5.
Jochen Arndt 9-Dec-16 2:53am    
Thank you.

And thanks to Chris for editing. The code from the original question was unreadable and I failed to edit it.
Not clear what is supposed to be c. if it is supposed to count the number of odd values, both codes are wrong.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900