Question

In: Computer Science

In this lab, you open a file and read input from that file in a prewritten...

In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat.

Instructions

  1. Ensure the source code file named Flowers.cpp is open in the code editor.
  2. Declare the variables you will need.
  3. Write the C++ statements that will open the input file flowers.dat for reading.
  4. Write a WHILE loop to read the input until EOF is reached.
  5. In the body of the loop, print the name of each flower and where it can be grown (sun or shade).

FLOWERS

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

GIVEN CODE

// Flowers.cpp - This program reads names of flowers and whether they are grown in shade or sun from an input

// 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()

{

   // Declare variables here

      

   // Open input file

   

   

   // Write while loop that reads records from file.

   fin >> flowerName;

   

      // Print flower name using the following format

      //cout << var << " grows in the " << var2 << endl;

      

   fin.close();   

   return 0;

} // End of main functio

Solutions

Expert Solution

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// main function definition
int main()
{
// Declare variables here
string flowerName, growsIn;

// Open input file
ifstream fin;
fin.open("flowers.dat");

// Checks if the file unable to open for reading display's error message and stop
if(!fin)
{
cout<<"\n ERROR: Unable to open the file flowers.dat for reading.";
return 1;
}// End of if condition

// Write while loop that reads records from file.
// Loops till end of the file
while(!fin.eof())
{
// Reads the flowers name and grows
fin >> flowerName;
fin >> growsIn;

// Print flower name using the following format
cout << flowerName << " grows in the " << growsIn << endl;
}// End of while loop
// Close the file
fin.close();

return 0;
} // End of main function
Sample Output:

Astilbe grows in the Shade
Marigold grows in the Sun
Begonia grows in the Sun
Primrose grows in the Shade
Cosmos grows in the Sun
Dahlia grows in the Sun
Geranium grows in the Sun
Foxglove grows in the Shade
Trillium grows in the Shade
Pansy grows in the Sun
Petunia grows in the Sun
Daisy grows in the Sun
Aster grows in the Sun

flowers.dat file contents

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


Related Solutions

In this lab, you will read and write to a file, as well as sorting the...
In this lab, you will read and write to a file, as well as sorting the information. Copy and paste the following into a file named "inputs.txt": 7 199922007 C.J. Cregg Political_Science 200822013 Olivia Dunham Criminal_Justice 199822007 Josh Lyman Law 199922006 Toby Ziegler Communications 200922015 Leslie Knope Public_and_Environmental_Affairs 199922004 Sam Seaborn Law 200722013 Walter Bishop General_Sciences The input file provides details for a student database in the following format: Number_of_students ID_Number Student_First_Name Student_Last_Name Major …… ID_Number Student_First_Name Student_Last_Name Major Your...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
Implement an application that will read data from an input file, interpret the data, and generate...
Implement an application that will read data from an input file, interpret the data, and generate the output to the screen. - The application will have two classes, Rectangle and Lab2ADriver. - Name your Eclipse project, Lab2A. Implement the Rectangle class with the following description. Rectangle Data fields -numRows: int -numCols: int -filled: Boolean Store the number of rows in the Rectangle Store the number of columns in the Rectangle Will define either a filled or unfilled rectangle True =...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
Python 3 8.5 Open the file mbox-short.txt and read it line by line. When you find...
Python 3 8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line: From [email protected] Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end. Hint: make sure not to include the lines that start...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT