Open file, print content

I need to open the file flowers.dat (i think I got that part right)
In the body of the loop I need to print name of each flower and where it needs to be grown (sun or shade)
I think I am good up to the while loop now I am not sure where to do from here
I would appreciate a push in the right direction
Thank you in advance for your help

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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	ifstream fin;
	fin.open ("flowers.dat");	// Declare variables here
	string flowerName;
	int sun, shade;
	
	
	fin.open("flowers.dat");	// Open input file
 
	if (fin.is_open());
	{
		cout << "file flowers.dat could not be read!" << endl;
	}

	   while (fin.open)						// Write while loop that reads records from file.
	   {
		 cout << flowerName;
		 cout << sun;
		 cout << shade;
	}
	 
   fin >> flowerName;
   
      // Print flower name
      
   fin.close(); 	
   return 0;
} // End of main function 
Last edited on
Have you tried to compile this program?

1
2
3
4
5
6
7
8
9
10
 In function 'int main()':
9:6: error: expected ';' before 'flowerName'
9:16: warning: statement has no effect [-Wunused-value]
18:3: error: expected ')' before '{' token
19:11: error: 'flowerName' was not declared in this scope
20:3: error: 'data_in' was not declared in this scope
23:11: error: 'flowerName' was not declared in this scope
10:6: warning: unused variable 'sun' [-Wunused-variable]
10:11: warning: unused variable 'shade' [-Wunused-variable]
30:1: error: expected '}' at end of input
Yes I tried to compile it but got 10 errors when I tried to build it. I know I'm missing something just not sure where to go at this point.
One of the errors say that flowerName and data_in are not declared.
I'm at a loss on that point
Last edited on
Start at the first error, fix it then recompile, fix the first error, repeat until no errors or warnings exist.

Note the numbers in my post above are the line number and the position in that line where the error was detected. The first is line 9 column 6. I'm not really sure what you think that line is doing, but you can probably just remove that line and then go from there.



I would recommend you start fresh. Then compile early and often. I normally start with something like:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello There" << endl;

    return(0);
}


Then I compile and run this program and insure my build system is still working. I then remove the cout line and start writing my new program. I then compile often, many times after adding only one line. If I get warnings or errors I fix them before I proceed.

Line 9: What do you think this statement does? fin is an instance of ifstream. You probably want the following:
 
  string flowerName;


line 17: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.

line 17: You have unbalanced ()

line 19: The first time through the loop, you haven't read a flowerName yet. Subsequent times through the loop you're printing the previous value. You want this line AFTER your read in a flowerName.

line 20: data_in is undefined. Do you mean fin?

line 22: You have no closing } for the if at line 15

line 23: This line doesn't belong here.



Last edited on
I updated the original code since I couldn't figure out how to put it in code format in the reply.

I still have 1 error when I try to build it in Visual Studio. I'm not sure what the error means or how to fix it.
Please let me know any additional suggestions to fix this.
I think I am getting closer

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
// file and prints the information to the user's screen. 
// Input:  flowers.dat.
// Output: Names of flowers and the words sun or shade.

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	ifstream fin;
	fin.open ("flowers.dat", ios::in);	// Declare variables here
	string flowerName;
	int sun, shade;
	
	if (fin.is_open())	// Open input file
	{
		while ( getline (fin, line))	// Write while loop that reads records from file.
		{
		 cout << flowerName;
		 cout << sun;
		 cout << shade;
	}
	 
   fin >> flowerName;
   
      // Print flower name
      
   fin.close(); 	
   return 0;
} // End of main function
 

Last edited on
line 18: line is undefined. Your compiler should have told you this.

lines 20-22: You've not read anything into these variables.

Line 18: You've eliminated the loop on !eof(), which is good, but now your're reading the entire line at one time. If you do it that way, you now have the problem of parsing the line. Since the file is a known format, it would be easier to do it this way:

1
2
3
4
5
  string sun_or_shade;
...
  while (fin >> flower_name >> sun_or_shade)
  {  cout << flowerName << ", " << sun_or_shade << endl;
  }


Line 25: This line is still out of place. Delete it.

As I understand the file, it contains the word "Sun" or "Shade" for each flower. These is no need for two int variables to represent this.

AbstractionAnon,

I made those changes however I am still getting 1 error in the cout << flowerName << ", " << where it is bold. I tried several variations but I still get the same error. This is the error I get
Severity Code Description Project File Line Suppression State
Error C3867 'std::basic_ifstream<char,std::char_traits<char>>::open': non-standard syntax; use '&' to create a pointer to member Flowers


Everything else is good

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	ifstream fin;
	fin.open ("flowers.dat", ios::in);	// Declare variables here
	string flowerName;
	string sun_or_shade;
	
	if (fin.is_open())	// Open input file
	{
		while ( fin>>flower_name>>sun_or_shade)	// Write while loop that reads records from file.
		{
		 cout << flowerName << ", " << sun_or_shade << endl;
	}
	   
      // Print flower name
      
   fin.close(); 	
   return 0;
} // End of main function  
You should pay closer attention to your code.

fin>>flower_name // your variable is called flowerName. string flowerName;

And your while loop is missing a closing bracket.
Last edited on
I fixed those mistakes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	ifstream fin;
	fin.open ("flowers.dat", ios::in);	// Declare variables here
	string flowerName;
	string sun_or_shade;
	
	if (fin.is_open())	// Open input file
	{
		while (fin >> flowerName >> sun_or_shade)	// Write while loop that reads records from file.
		{
			cout << flowerName << ", " << sun_or_shade << endl;
		}

		// Print flower name
	}
   fin.close(); 	
   return 0;
}// End of main function  
Topic archived. No new replies allowed.