a) Describe a struct type, named Dog, that contains one string, one integer, and
ID: 3816371 • Letter: A
Question
a) Describe a struct type, named Dog, that contains one string, one integer, and one double. B) In the main program area declare a Dog variable open a file named "dogfiles.1st" write a message if the file did not open successfully call a function to read information for your Dog variable from the text file. c) Write the function which will; receive the file and the Dog variable read data into each of the Dog variable from the file bring back the changed Dog variable Type declaration Main program code Function codeExplanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
//Declare a struct type, named Dog.
struct Dog
{
string name;
int number;
double weight;
};
//Write a function which receive the file and the Dog variable.
Dog readDogInfo(ifstream& fin, Dog myDog)
{
//Read dog into each field of the Dog variable from the file.
fin >> myDog.number;
getline(fin, myDog.name);
fin >> myDog.weight;
return myDog;
}
int main()
{
//Declare a Dog variable.
Dog myDog;
//Open a file named dogfile.txt
ifstream fin;
fin.open("dogfile.txt");
//Write a message if the file did not open successfully.
if(!fin.is_open())
{
cout << "Unable to open the file." << endl;
return 0;
}
//Call function to read information for your Dog variable from text file.
myDog = readDogInfo(fin, myDog);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.