Question

In: Computer Science

Program #include<iostream> using namespace std; struct bookRecord //structure definition {             //structure members definition char title[30];...

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

}

Solutions

Expert Solution

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


Related Solutions

complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct Node {     int data;     struct Node* next; }; void printMiddle(struct Node *head) {     struct Node *slow_ptr = head;     struct Node *fast_ptr = head;     if (head!=NULL)     {         while (fast_ptr != NULL && fast_ptr->next != NULL)         {             fast_ptr = fast_ptr->next->next;             slow_ptr = slow_ptr->next;         }         printf("The middle element is [%d]\n\n", slow_ptr->data);     } } void...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT