File I/O Looping Issue

I am having trouble getting my program to run correctly. Right now, when run as shown below, all I get is a never ending loop. Does anyone see issues that I may be able to fix?

# include <iostream> // Preprocessor Directive for cin and cout
# include <fstream> // Preprocessor Directive for File Input & Output
#include <cstdlib>
using namespace std;

int main ()
{ ifstream infile
ofstream outfile;

infile.open("Project_3_input.txt"); //Open input file
outfile.open("Project_3_output.txt"); //Open output file

char day_week_first, day_week_second; //Variable declaration
int time_started_hour, time_started_minutes, duration;
double cost_minute=0, total_cost;


while ((!infile.eof()) || (!infile.fail())) //Run loop while file is not at end and doesn't fail

infile>>day_week_first>>day_week_second>>time_started_hour>>time_started_minutes>>duration;

{

if(((day_week_first== 'M') || (day_week_first== 'm')) && ((day_week_second== 'O') || (day_week_second= 'o'))){
if(time_started_hour >= 7 && time_started_hour <= 21){
cost_minute=0.30;
} else {
cost_minute=0.15;
}

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else if(((day_week_first== 'T') || (day_week_first== 't')) && ((day_week_second== 'U') || (day_week_second= 'u'))){
if(time_started_hour >= 7 && time_started_hour <= 21){
cost_minute=0.30;
} else {
cost_minute=0.15;
}

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else if(((day_week_first== 'W') || (day_week_first== 'w')) && ((day_week_second== 'E') || (day_week_second= 'e'))){
if(time_started_hour >= 7 && time_started_hour <= 21){
cost_minute=0.30;
} else {
cost_minute=0.15;
}

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else if(((day_week_first== 'T') || (day_week_first== 't')) && ((day_week_second== 'H') || (day_week_second= 'h'))){
if(time_started_hour >= 7 && time_started_hour <= 21){
cost_minute=0.30;
} else {
cost_minute=0.15;
}

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else if(((day_week_first== 'F') || (day_week_first== 'f')) && ((day_week_second== 'R') || (day_week_second= 'r'))){
if(time_started_hour >= 7 && time_started_hour <= 21){
cost_minute=0.30;
} else {
cost_minute=0.15;
}

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else if(((day_week_first== 'S') || (day_week_first== 's')) && ((day_week_second== 'A') || (day_week_second= 'a'))){
cost_minute=0.10;

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else if(((day_week_first== 'S') || (day_week_first== 's')) && ((day_week_second== 'U') || (day_week_second= 'u'))){
cost_minute=0.10;

total_cost=(cost_minute)*(duration);
outfile<<total_cost;

} else {
cout<<"Invalid day of the week";
}


}

infile.close();
outfile.close();

return 0;
}
Last edited on
Maybe you should change the (!infile.eof()) || (!infile.fail()) to infile.good() for simplicity? Just a suggestion... as what you have now is pretty much equivalent to !(infile.eof() && infile.fail()) which as you can see will only return false if both bits are true. :/

-Albatross
Thanks for the speedy reply. That didn't seem to help my infinite looping issue, but I appreciate the suggestion.
it's difficult to read code that's not wrapped in code tags, but at first glance, the only thing scoped to the loop is this:

infile>>day_week_first>>day_week_second>>time_started_hour>>time_started_minutes>>duration;

if it's not a scoping issue, you probably need to ignore a newline character or clear a bad bit; these are the most common causes of infinite loops when working with input streams for me
Last edited on
okay, this is probably your problem. you're using this:

infile>>day_week_first>>day_week_second>>time_started_hour>>time_started_minutes>>duration;

to read in individual characters. the stream extraction operator tries to write everything to the variable until the next whitespace character, so there's a bunch of crap sitting in the input stream buffer, so you immediately have a bad bit, just not a fail bit.

use this:

yourVariable = infile.get();

that will read in only one character at a time which is what i believe you are trying to do

you still need to fix your scope, though
Last edited on
Sorry about the lack of code tags. This is my first time posting on the boards.

Thanks so much for identifying the likely problem. I will try that out first thing in the morning. Forgive me for the stupidity, but how do I change the scope?
Last edited on
what i mean about your scope is that you need an opening curly brace immediately after your while loop test condition. if you don't have that, the only thing that's going to run in the loop is the very first statement after your loop condition. everything after that line won't execute until the loop finishes. and all you've done in the loop is overwrite those variables a whole bunch of times
Ahhhh I see what you mean. Thanks again. I really do appreciate the help!
Topic archived. No new replies allowed.