hey guys, i would greatly appreciate it if u wood help me out to see whats wrong with my codes.... heres my question and heres my code......

DONT 4GET TO MAKE THE TEXT FILE TO RUN IT !!!! <input.txt, output.txt> <fp_in, fp_out>

1. Write a program to average real numbers input from a text file called “input.txt”. Each line of the text file will contain a single number to be averaged. Collect and sum numbers from the file until there are no more numbers in the file, then compute the average. Write the sum, the number of entries in the file and the average to another text file called “output.txt”.

Create the following text file and use as your input file:
10
15
20
45
6
19
33
21


here are my codes:

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main(int nNumberofArgs, char*psz[]){

    FILE* fp_in;
    FILE* fp_out;

    int Average;
    int sum = 0;
    int count = 0;
    int num;

    fp_in = fopen("input.txt", "r");
    fp_out = fopen("output.txt", "w");

    //Error Checking

    if (fp_in = NULL)
    {
     printf("File does not exist or could not be found/n");
     system("PAUSE");
     exit;
     //return 0;
    }

    if (fp_out = NULL)
    {
     printf("File does not exist or could not be found/n");
     system("PAUSE");
     exit;
     //return

    }
           fscanf(fp_in, "%d", &num);


              while (num != EOF)
                {
               // printf("test2");
                count++;
                sum = sum + num;
                fscanf(fp_in, "%d", & num);
                }

           Average = sum/count;

           fprintf (fp_out, "The sum is equal %d", sum);
           fprintf (fp_out, "the number of entries is %d", count);
           fprintf (fp_out, "The average is %d", Average);

           fclose(fp_in);
           fclose(fp_out);

      system("PAUSE");
      return 0;
}

You call that c++ code ???:D My suggestion to you is to scrap it and rewrite in true c++ -- a program without all those C style functions calls.

9 posts and you can't find the correct forum? Your code is C, not C++.

That said, your logic is generally correct. I would not expect you to put the text in the output file, just the values.

In computing the average, remember that integer division gives an integer result - is that really what you (or your instructor) want?

Do you get any error or incorrect result when you run this? What is your real question?

Not only the errors mentioned in the post above it also has syntax errors and other major errors too.

1>

if(fp_in = NULL)

you are not assigning the pointer you need to check so its

if(fp_in == NULL)

Same goes for fp_out

2>Why are you exactly using exit .When you are using it at least call the function properly with a return value.

3>This is a C++ forum and this post is first of all not C++ its C. The only C++ thing I see here is the header <iostream> wonder why you even cared to use it.???:?:

4>Your starting line itself is hilarious

int main(int nNumberofArgs, char*psz[])

Where are you even using the command line parameters ???

The main thing wrong with your code is "You don't know which language you are writing it in...!!!".So try to put in some more hard work.you definitely can improve.

Ditto as to what CSurfer said.

Also you need an exist but empty file handler! Meaning file exists but empty, thus no data read thus BOOM! Divide by zero!
You need a zero case handler!

I'd also recommend to write clean code! Something teachers don't teach from the old C books. They teach signed data. In reality they should be teaching ALL DATA IS UNSIGNED, unless it is necessary for it to be signed! So your declarations shouldn't be integer, they should be unsigned int. And if that's a mouthful then...

typedef unsigned int uint;

uint nCount = 0;

See there is no negative count of something counted! And if you're accumulating positive numbers then why use signed arguments?

"%u" vs "%d" etc.

And convert to floating point for a floating point-average.

OR
add 1/2 a count for a more approximate average.

nTotal = (nTally + (nCount>>1)) / nCount;

There is one good thing in Java that should be adopted fore every C/C++ program written. If you had done the following, you would have immediately caught your comparison vs assignment error! ALWAYS put the constant first in your comparisons!

if (NULL == fp_in)
{

if (NULL == fp_out)
{

if you had done...

if (NULL = fp_in)
{

The compiler would have caught it immediatly with an error!

This article has been dead for over six months. Start a new discussion instead.