0

I'm working on this piece of code where I am getting a holiday from my Holiday.txt file and then inputting that holiday into my Schedule.txt. There are multiple holidays so I'm trying to loop through the file until it reaches the end and has fully copied them onto my second file, however it doesn't seem to be inputting the holiday into my string variable and I keep getting stuck in an infinite loop.

int main(){  
fstream bookings("Schedule.txt");
fstream magicians("Magicians.txt");
fstream holidays("Holidays.txt");



string test;
string holiday;

bookings >> test;
testing to check if there's anything in bookings
if(test.length()==0){
   while(!holidays.eof()){
       getline(holidays, holiday);
       bookings << holiday << endl << endl;
       cout << "test";
        }
    }  
  return 0;
}

I've tried using different while loops including while(getline(holidays, holiday)) or even while(holidays >> holiday) until they reach the end of the file but everything seems to get stuck in infinite loops.

Sorry if these are basic questions but I'm a beginner that's been trying to figure it out for so long now, even stepping through the program, and nothing seems to work. Can anyone explain to me what's going wrong?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
conejo
  • 31
  • 6
  • Try putting a cout << holiday << '\n' ; just after getline – Shreyas Pimpalgaonkar Feb 10 '19 at 03:31
  • 1
    Have you verified that the files are actually opening? – Brandon Lyons Feb 10 '19 at 04:07
  • `while(!holidays.eof()){` https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Feb 10 '19 at 04:10
  • I expect you are not even opening your files. A common mistake is to put them in the wrong folder. If this is Visual Studio the default folder is the one containing your project. Other IDEs the default folder may be the one containing the executable. – drescherjm Feb 10 '19 at 04:13
  • Please include the contents of `holidays.txt` file. Also, please share the result of `holidays.is_open()` – Kunal Puri Feb 10 '19 at 04:24
  • Yeah so I made sure my files were open and added an .is_open test and it still doesn't work. [here] (https://i.gyazo.com/608d53e883b1d9796e9aec26f23ea37f.png) is how the program works and the first test doesn't work ( I purposely took out the ! from `!holidays.eof`). I can only assume it's the files not being in the right folder? I'm using Xcode on mac and placed the files in the same folder that the main.cpp file is in, does anyone know if that's where it's supposed to be placed? – conejo Feb 10 '19 at 04:41
  • Related: https://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode – drescherjm Feb 10 '19 at 04:53

1 Answers1

0

So I went in and made sure my .txt files are in the right directory and that they are open, however my program still is not outputting what I want into my Schedule.txt file.

This is what I currently have:

 #include <iostream>
 #include <fstream>
 #include <string>

using namespace std;

int main(){

fstream bookings("Schedule.txt");
fstream magicians("Magicians.txt");
fstream holidays("Holidays.txt");

//already tested to make sure these are open & they are

string test;
string holiday;

bookings >> test;

if(test.length()==0){
   while(getline(holidays, holiday)){
       bookings << holiday;
       cout << "test";

        }
    }

bookings.close();
magicians.close();
holidays.close();


  return 0;
}

My Holidays.txt contains this: Veteran's Day Valentine's Day Halloween Christmas New Years I stepped through the program to make sure holiday is getting the line accordingly but bookings << holiday; just doesn't seem to work properly for me?

conejo
  • 31
  • 6