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 8737601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:33:48+00:00 2026-06-13T10:33:48+00:00

I have started working on a program, and for the life of me I

  • 0

I have started working on a program, and for the life of me I can’t find a bug which continuously searches the eof and doesn’t end. It is most certainly a problem with my fileread and widthcount functions, and they most likely have the same error. Is there a fix for this? It just keeps looping.

The file input is this

12.43 62.38 Los Angeles
21 59 Columbus, Ohio
0 15.58 Green Bay, Wisconsin

This continues for another 10 lines.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void fileread(ifstream &ifFile, char newline, double &temp1, double &temp2, string city);
void widthcount(ifstream &ifFile, int &maxCount, char newline);

int main() {
   double temp1, temp2, minTemp, maxTemp;
   int maxCount;
   char newline=' ';
   string FileRead, city;
   ifstream ifFile;

   cout << "Please enter the location of the input file: ";
   cin >> FileRead;

   ifFile.open(FileRead.c_str());

   if (ifFile.fail()) {
       perror(FileRead.c_str());
       exit(1);
   }

   widthcount(ifFile, maxCount, newline);

   while(!ifFile.eof()) {
       widthcount(ifFile, maxCount, newline);
   }      

   ifFile.close();

   ifFile.open(FileRead.c_str());

   fileread(ifFile, newline, temp1, temp2, city);

   while(!ifFile.eof()) {
       fileread(ifFile, newline, temp1, temp2, city);

       cout << left << setw(20) << city
             << right << setw(6) << fixed << setprecision(2) << temp1
             << right << setw(6) << fixed << setprecision(2) << temp2 << endl;

   }

}

void fileread(ifstream &ifFile, char newline, double &temp1, double &temp2, string city) {
   int charcount = 0;

   ifFile >> temp1 >> temp2;

   while (newline == ' ')
       ifFile.get(newline);
   while (newline != '\n' || !ifFile.eof()) {
       charcount++;
       ifFile.get(newline);
   }
}

void widthcount (ifstream &ifFile, int &maxCount, char newline) {
   double temp1, temp2;
   int charcount = 0;
   ifFile >> temp1 >> temp2;
   ifFile.get(newline);
   while (newline != '\n' && !ifFile.eof()) {
       charcount++;
       ifFile.get(newline);
       cout << newline;
   }

   if (charcount > maxCount)
    maxCount = charcount;
}
  • 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-06-13T10:33:49+00:00Added an answer on June 13, 2026 at 10:33 am

    You need to check for failure (test .fail()), not end of file.

    Typically .fail() is checked by using the stream object directly as condition.

    E.g. while( f ) is equivalent to while( !f.fail() ).

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

Sidebar

Related Questions

At work, I have started working on a program that can potentially generate hundreds
I have started working on a project which requires Natural Language Processing. We have
Recently I have started working on a program that will monitor the packets of
I started using Padre and while working on a particular program which works just
Possible Duplicate: .eof() loop not working So I have a fairly simple C++ program
I have started working on some algorithm problems when I saw a problem asking
I have started working on a simple XML pull-parser, and as I've just defuzzed
I have started working with enhanced for loops due to it's best practices and
We have started working on VS11 projects involving a DDD - Agile - TDD
I have started working on yii framework and I am following the yii blog

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.