1

I'm a programming student in my second OOP class, my first class was taught in C# and this class is taught in C++. To get our feet wet in C++, our professor has asked us to write a rather large program with File I/O. The problem is, I have a small part of my program that is not working, at least, not for me. The project requires that we code a loop to test if the file could be opened successfully, and the loop I have written doesn't seem to be working.

I don't get any compiler errors, but when I enter in the path to the file, either relative or absolute, it says it's invalid. I have a feeling it has something to do with my conditions in my do-while loop, but I can't pinpoint it.

I don't mean to bring my homework to SO, but I've been scratching my head for two+ hours, and I can't seem to figure this out.

Would you mind helping me fix my loop? And maybe explain what it is that I'm doing wrong? I want to learn.

Thanks!

Code:

Rainfall rData;
ifstream actualReader;
ifstream averageReader;
string aRDataLoc;
char response = 'a';
const int KILL_VALUE = 1;
double actualRainfallD;
double actualRainfallPassedArray[ARRAY_CAPACITY];
double averageRainfallPassedArray[ARRAY_CAPACITY];
int i = 0;

do
{
    actualReader.clear();
    cout << "\nPlease enter in the path to the file containing the actual rainfall  data." << endl;
    cout << "Path to file: ";
    cin >> aRDataLoc;
    actualReader.open(aRDataLoc.c_str());
      if (!actualReader.is_open())
          {
            cout << "Invalid file path! Would you like to enter in a new file path?(y/n): ";
            cin >> response;
            if (response == 'n') { exit(KILL_VALUE); }
    }
}while (!actualReader.is_open() && response == 'y');
Alex
  • 64,178
  • 48
  • 151
  • 180
  • Can't help if we can't see the variables declared. – Hogan Feb 03 '10 at 04:06
  • 1
    Its working fine for me. If I enter a non-exiting filename, it prompts to continue or not. If I enter a existing filename, it exits. Is this not what you want? – codaddict Feb 03 '10 at 04:11
  • Can you tell us exactly what line it is failing on? Or is the file just never opening? Also, you should be passing a mode to ifstream.open().... eg: actualReader.open(aRDataLoc.c_str(), ifstream::in). Also, make sure that aRDataLoc is what you expect it to be when you hit that point in the program. – DigitalZebra Feb 03 '10 at 04:15
  • Also make sure you close the file when done with it... – DigitalZebra Feb 03 '10 at 04:16
  • Does the filename contain spaces? Did you type the filename extension as well? – Hans Passant Feb 03 '10 at 04:17

3 Answers3

1

I don't know what input you are giving to cin, but be aware that cin will stop at the first whitespace character it encounters. For example, if you give as input the following:

C:\Program Files\directory

then aRDataLog would have the value C:\Program .

In order to read the whole line, you could use getline.

Check also this post.

Community
  • 1
  • 1
Alex Ntousias
  • 8,962
  • 8
  • 39
  • 47
  • +1. Also worth noting that `std::cin` doesn't know or respect the normal way to delimit a path (with "s). – Billy ONeal Feb 03 '10 at 04:59
  • Thank you! Cin was stopping at whitespace for my `visual studio 2008` directory. Thank you very much! – Alex Feb 03 '10 at 15:40
1

While I'm not exactly sure what the issue is here, it might be a good idea to print what you're getting from std::cin to ensure you're getting what you're expecting.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

you need to put actualReader.close call in else, as the file has open handles and it is not available for open again

Naveed
  • 103
  • 6