Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 890585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:45:24+00:00 2026-05-15T13:45:24+00:00

My program is reading numbers from a file- and it reads the last number

  • 0

My program is reading numbers from a file- and it reads the last number twice. What is wrong with my program and what can I do to fix it?

int main()
{

    ifstream inputFile;

    int number = 0; //the input for the numbers in the file
    double total = 0; //the total of the numbers
    double counter = 0;//number of numbers
    double average = 0;//average of the number



    inputFile.open("random.txt");//open file
    if (!inputFile)//test for file open errors
    {
        cout<<"Error \n";
    }



    while (inputFile)
     {
        inputFile >> number ;
        total+=number;
        counter++;
        cout<<"The running total is: " <<total <<"\n";

     }


    total=total*1.00;
    counter=counter*1.00;

    average = total/counter;
    cout<<"\nthe final total is: \t" <<total;
    cout<<"\nthe number of numbers is: \t";
    cout<<counter;
    cout<<"\nthe average of the numbers is: \t";

    cout<<setprecision(8)<< fixed<< average<<"\n";

    inputFile.close();
    return 0;
}

the contents of the file:
42
468
335
501
170
725
479
359
963
465
706
146
282
828
962
492
996
943
828
437
392
605
903
154
293
383
422
717
719
896
448
727
772
539
870
913
668
300
36
895
704
812
323
334
674
665
142
712
254
869
548
645
663
758
38
860
724
742
530
779
317
36
191
843
289
107
41
943
265
649
447
806
891
730
371
351
7
102
394
549
630
624
85
955
757
841
967
377
932
309
945
440
627
324
538
539
119
83
930
542
834
116
640
659
705
931
978
307
674
387
22
746
925
73
271
830
778
574
98
513
987
291
162
637
356
768
656
575
32
53
351
151
942
725
967
431
108
192
8
338
458
288
754
384
946
910
210
759
222
589
423
947
507
31
414
169
901
592
763
656
411
360
625
538
549
484
596
42
603
351
292
837
375
21
597
22
349
200
669
485
282
735
54
1000
419
939
901
789
128
468
729
894
649
484
808
422
311
618
814
515

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:45:25+00:00Added an answer on May 15, 2026 at 1:45 pm

    Because the inputFile becomes false1 after an unsuccessful reading attempt has been done, and not when there’s just no more data to read. So, when you’ve read successfully the last element you are inputFile still evaluates to true, and the next iteration of the while is started. Now, at inputFile>>number the failbit is set, but you’re not checking it immediately, so your code goes on normally, “thinking” that another element has been read (when actually is just the old one which happened to remain in number).

    Quick solution: move the check after the read:

    for(;;)
    {
        inputFile >> number;
        if(!inputFile)
            break;
        total+=number;
        counter++;
        cout<<"The running total is: " <<total <<"\n";
    }
    

    or (better):

    while(inputFile >> number)
    {
        total+=number;
        counter++;
        cout<<"The running total is: " <<total <<"\n";
    }
    

    This works because operator>> returns the stream object, which is evaluated just after the read in the while condition section.


    1. I know, I know that it’s not actually false but it’s operator(void*)… but don’t overcomplicate things 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a binary file into a parsing program. I will need to
I wrote a program which includes writing and reading from database. When I run
I'm having problems redirecting stdio of another program using subprocess module. Just reading from
I'm reading input in a C++ program. First some integers, then a string. When
In an Open Source program I wrote , I'm reading binary data (written by
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
one of the programs I'm developing at work is reading the registry to figure
Programming is learned by writing programs. But code reading is said to be another
Most program languages have some kind of exception handling; some languages have return codes,
My program generates relatively simple PDF documents on request, but I'm having trouble with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.