Program Won't Output Correctly

Hi all, I have a program where I am to open a .dat file containing a list of flowers and whether or not they grow in the sun or shade and print it into a .cpp file. Everything is outputting smoothly except the first flower, Astilbe, does not print. Any help would be appreciated as I am a bit confused, thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
    // Declare variables here
    ifstream fin;
    string flowerName;
    string sun_or_shade;
    ifstream data_in;
    ofstream data_out;

    // Open input file
    fin.open("flowers.dat");
    fin >> flowerName;
    fin >> sun_or_shade;
    while (!fin.eof())

    // Write while loop that reads records from file.
      if (fin.is_open())	// Open input file
	{
		while (fin >> flowerName >> sun_or_shade)	// Write while loop that reads records from file.
		{
			cout << flowerName << " grows in the " << sun_or_shade << endl;
		}
	}
   fin.close(); 	
   return 0;
  } // End of main function 

Here is the .dat file that with the flowers if it is of any help:
1
2
3
4
5
6
7
8
9
10
11
12
13
Astilbe Shade
Marigold Sun
Begonia Sun
Primrose Shade
Cosmos Sun
Dahlia Sun
Geranium  Sun
Foxglove Shade
Trillium Shade
Pansy Sun
Petunia Sun
Daisy Sun
Aster Sun
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

// create a small file for testing the code
void make_a_test_file( std::string file_name )
{
    std::ofstream(file_name) << "Astilbe shade\n"
                                "Marigold sun\n"
                                "Begonia sun\n"
                                "Primrose shade\n"
                                "Cosmos sun\n"
                                "Dahlia sun\n"
                                "Geranium  sun\n"
                                "Foxglove shade\n"
                                "Trillium shade\n"
                                "Pansy sun\n"
                                "Petunia sun\n"
                                "Daisy sun\n"
                                "Aster sun\n" ;
}

int main()
{
    const std::string file_name = "flowers.dat" ;

    // for testing; comment this out if you already have a file
    make_a_test_file(file_name) ;

    if( std::ifstream fin{file_name} ) // if the file was opened successfully
    {
        std::string name ;
        std::string grows_in ;
        int cnt = 0 ;
        while( fin >> name >> grows_in ) // for each flower information in the file
            std::cout << std::setw(3) << ++cnt << ". " << name << " grows in the " << grows_in << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/1ccca704cddeb4e3
Hi, thanks for the help but I am instructed to implement code that opens the input file, dat, into my .cpp file. I am also instructed to use a while loop. And while your code outputs my program correctly, my code succeeds in the other two areas but fails to output correctly.
JLBorges' code does everything you requested, so what's the problem...? ;D

Did you want an explanation of where your code failed? Okay *takes out the red marker*

fin.open("flowers.dat");
1. Set your file name to a variable, just like JLBorges did.
2. The constructor of a fstream object auto-magically opens the file, so an explicit open call isn't needed for the first use.
3. Do a boolean comparison on the fstream object itself. If something went wrong and you previously set a variable like file_name, you can then mention it and terminate the program:
1
2
3
4
5
6
7
const string file_name("flowers.dat");
ifstream fin(file_name);
if (!fin)
{
    cout << "Error: unable to open '"<< file_name << "'.  Please check path and make sure file is not in use.\n";
    return -1;
}


These next few lines are pointless since the while loop iterates over the file anyway, assigning every whitespace-separated string anyway. No need to read two strings in advance.
1
2
3
4
fin >> flowerName;
fin >> sun_or_shade;
while (!fin.eof())    // Delete this.  You got lucky with unbracketed loop not causing more damage -- 
                       //   it simply executed the next statement, which was a harmless 'if' statement   


if (fin.is_open())
Ah, the harmless "if" statement. The file has to be open because this line appears after your outer while (which should be deleted). Delete this "if" statement and refactor to the form I presented above.

Then finally your loop that does the work; this all looks good:
while (fin >> flowerName >> sun_or_shade) HOWEVER, you overrode your previous lines that I called pointless. flowerName was set to "Astilbe" and sun_or_shade was set to "Shade", and here you overwrote these values before ever printing anything to the screen. So then don't be surprised that Astilbe never gets printed

Lastly, you can remove your last line:
fin.close();

because when fin goes out of scope, it automatically closes itself. Since you're not trying to reuse fin for other files, and nothing interesting happens before program terminates anyway, this line can be removed.
Last edited on
Topic archived. No new replies allowed.