0

When I run my code it is adding the value of 81 two times. Why ???

Write a program to read the contents of file Squares.txt and display the sum of all the numbers, the average of all the numbers, the largest number and the smallest number in a file named Analysis.txt. Content in Squares.txt :

Number Square
3        9
5        25
1        1
7        49
9        81
#include <iostream>
using namespace std ;
#include <fstream>

int main() {
    ifstream file ;
    char arra[100];
    int num1,num2,avg;
    int sum=0 ;
    int smallest = 9999 ;
    int highest = -999 ;
    int count= 0 ;
    cout<<"Open file square.txt..."<<endl ;
    file.open("/Users/kchumun/Documents/Xcode/Labsheet 8(3)/square2.txt") ;
    if(!file){
        cout<<"File cannot be opened !"<<endl ;
    }else {
        cout<<"File opened !"<<endl;
        file.get(arra,100) ;
        while(!file.eof()){
            file>>num1;
            file>>num2 ;
            sum+=num2;
            count++ ;
            if(num2<smallest){
                smallest = num2 ;
            }
            if (num2>highest) {
                highest = num2 ;
            }
        }
    }
    file.close() ;
    avg= sum / count ;
    cout<<"Sum= "<<sum<<endl ;
    cout<<"Average= "<<avg<<endl;
    cout<<"Highest= "<<highest<<endl ;
    cout<<"Smallest= "<<smallest<<endl;
    
    ofstream File ;
    cout<<"Open file Analysis "<<endl ;
    if(!File){
        cout<<"Error !" ;
    }else{
        File.open("/Users/kchumun/Documents/Xcode/Labsheet 8(3)/Analysis.txt");
        File<<"Sum= "<<sum<<endl ;
        File<<"Averagem= "<<avg<<endl;
        File<<"Highest= "<<highest<<endl ;
        File<<"Smallest= "<<smallest<<endl ;
    }
    File.close();
    cout<<"Operation completed !";
    
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • `while(!file.eof())` often causes duplication errors. Details here: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Jan 31 '21 at 06:05

1 Answers1

1

This style of code is incredibly common but also incredibly wrong

while(!file.eof()){
        file>>num1;
        file>>num2;

Like this instead please

while (file >> num1 >> num2) {

The problem with your code is a misunderstanding of how eof works. Eof tests if you are at the end of the file right? No, wrong. In reality eof tests if your last read failed because you were at the end of file. A subtly different thing, and this difference explains why your loop seems to read the last item twice.

If you do use eof you should use it after reading to test if the last read failed. Not before reading to predict if the next read will fail.

john
  • 85,011
  • 4
  • 57
  • 81
  • General rule: 1. Read data. 2. Make sure you read data. 3. Make sure data read is with a valid range. 4. If everything is good up to this point, use data. If you try to do a read out of order, like testing for validity before reading, sooner or later it will fail. – user4581301 Jan 31 '21 at 08:19