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

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;...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT