0

So, I'm trying to get the program to read a file and print out the number of sunny, cloudy and rainy days. It just keeps printing 0. What am I missing? I've tried to change the file from a .dat to .txt but still the same result. Here's what's in the data file:

RRCSSSCSCRRRCSSSCSSRSCCRCRRCSS SSSCCSSSCCSSSCCSSSCRCRCCSSSSSS SSSSCSSSCSSSCRRCCCSSSSSCSSSSCS

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

    using namespace std;

    int main()

    {  

    const int MONTH = 3;

    const int DAY = 30;

    char name[MONTH][DAY] = {"June", "July", "August"};
    char rain = 'R'; 
    char sun = 'S';    
    char cloud = 'C';    
    char letter;    
    int day = 0;    
    int count = 0;    
    int rainy = 0;    
    int cloudy = 0;
    int sunny = 0;          

    ifstream inputFile;

    cout << " Weather for Summer Months\n";

    cout << "--------------------\n\n";   

    inputFile.open("C:\rainorshine.dat");    

    if (inputFile)
    {
    cout << "Error opening data file.\n";
        system("pause");
    }
    else

    {   cout << "Weather Report\n\n";

    while (inputFile >> letter)
    {
        cout << letter << endl;  // print out characters read from file 
    }             
   for (count = 0; count < MONTH; count++)    
    {    
        for (day = 0; day < DAY; day++)    
        {    
            cout  << name[count][day];    
           inputFile >> name[count][day];    
            if (name[count][day] == 'R')    
                rainy++;
                else if (name[count][day] == 'S')    
                sunny++;            
            else if (name[count][day] == 'C')    
                cloudy++;
        }

        cout << endl;        
        cout << "  Sunny Days Total: " << rainy << endl;    
        cout << "  Rainy Days Total: " << sunny << endl;    
        cout << "  Cloudy Days Total: " << cloudy << endl << endl;      
    }
    system("pause");
        return 0;
    inputFile.close();


    }
    }
2
  • 2
    You need to escape the backslash in inputFile.open ... Commented Mar 13, 2013 at 22:48
  • 1
    if (inputFile) { /* error */ } This condition seems to be the opposite of what you'd want. Commented Mar 13, 2013 at 22:50

3 Answers 3

5

This:

while (inputFile >> letter)
{
    cout << letter << endl;  // print out characters read from file 
}  

munches through all the letters, when it's done you're at the end of the file. So when you try to read your data again in the for loop, there's nothing left to read.

Sign up to request clarification or add additional context in comments.

2 Comments

I added that line to see if the file was even reading each character and it isn't - even if I remove that loop, the outputs are still 0
@Anna When I remove the loop on my system, it works (although I modified your program to use cin instead of file input). Read the comments by Shmil and jrok below your question for hints how to fix your file problems.
3
"C:\rainorshine.dat"
// ^^

This isn't a backslash followed by a 'r', it's an escape sequence for carriage return character. You need to use forward slash or escape the backslash itself:

"C:/rainorshine.dat" or "C:\\rainorshine.dat"

Which means you never open the file. You would've noticed that if your condition were

if (!inputFile) // notice the !
{
    cout << "Error opening data file.\n";

instead of logically incorrect

if (inputFile) ...

1 Comment

Thank you! I knew I was missing something
1

After this line;

while (inputFile >> letter)
{
    cout << letter << endl;  // print out characters read from file 
}

You need to reopen the file or seek back to the beginning. The next time you try to read the file you're already at the end so you get nothing.

Something like;

 inputFile.seekg(0, inputFile.beg);

should solve the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.