In: Computer Science
Program
#include<iostream>
using namespace std;
struct bookRecord //structure definition
{
//structure members definition
char title[30];
char main_author[30];
int year_publish;
float price;
};
int main()
{
struct bookRecord book1; // declare structure variable;
//Get data from user
cout<<"Enter book title: ";
cin>>book1.title;
cout<<"Enter main author name: ";
cin>>book1.main_author;
cout<<"Enter year publish: ";
cin>>book1.year_publish;
cout<<"Enter book price RM: ";
cin>>book1.price;
//Print the information the screen output
cout<<"\n\nBook Information";
cout<<"\nTitle: "<<book1.title;
cout<<"\nMain author: "<<book1.maon_author;
cout<<"\nYear published "<<book1.year_publish;
cout<<"\nBook price: RM"<<book1.price;
return 0;
Task 1
Create a program to store a record using structure. You need to define one structure template with minimum 3 data members. In main function, declare one structure variable, then get input from user and store into the variable. Then your program will display the content of the structure on the screen output.
Task 2
Use the structure definition in Task 1 to store 5 records using an array of structure with five elements. Get the data from user, store into the array, then display the output in table format.
Task 3
You are required to create a program which involve nested structure. What u need to do is to create another structure that related to the structure definition in Task 1. For example you may have structure such as BookInfor and AuthorInfor. Therefore you can produce information like the book A is written by the this author B. Your program should store five records accordingly
}
Here is the solution to above problem in C++. Please read the code comments for more information
Please give a thumbs up
Task 1
1) Create a new file write all the content of the structure inside the newly created text file , by taking file name input by the user
2) Open the file write to it and than close it
Task 2
1) Create an array of type bookRecord of size 5 , use loop to take input 5 times for a user and save it inside the array
2) write the contents of the array inside the file using loop
Task 3
1) write a structure author and adds its object in the bookRecord Structure
2) Read the input from user and write it to a file
C++ Code (Combination of Task1,Task2 and Task 3)
#include<iostream>
#include<fstream>
using namespace std;
//another structure for author which contains the author
name
//PART OF TASK 3
struct author {
char main_author[30];
};
struct bookRecord //structure definition
{
//structure members definition
//add the author structure obeject here
struct author main;
char title[30];
int year_publish;
float price;
};
int main()
{
//TASK 1
char filename[100];
cout<<"Enter the filename to store data:
";
cin>>filename;
//open the file
ofstream fout(filename);
if(!filename)
{
cout<<"Cannot open the file
\n";
return -1;
}
//create array of 5 to store 5 books from user
//TASK 2
struct bookRecord array[5];
int i;
//use for loop of 5 to take input 5 times
//TASK 2
for(i=0;i<5;++i)
{
struct bookRecord book1; // declare structure variable;
//Get data from user
cout<<"Enter data for book "<<i+1<<": \n";
cout << "Enter book title: ";
cin >> book1.title;
cout << "Enter main author name: ";
cin >> book1.main.main_author;
cout << "Enter year publish: ";
cin >> book1.year_publish;
cout << "Enter book price RM: ";
cin >> book1.price;
//SAVE DATA INSIDE ARRAY
array[i]=book1;
}
//Write the content in a file
for(i=0;i<5;++i)
{
fout << "\nTitle: " << array[i].title;
fout << "\nMain author: " << array[i].main.main_author;
fout << "\nYear published " << array[i].year_publish;
fout << "\nBook price: RM" << array[i].price;
}
//close the file
fout.close();
return 0;
}
Screenshot of output
friend2.txt Screenshot