In: Computer Science
Read inputs from console and store in Structure Given a structure of type “struct book” (shown below), read input into struct book from a console. struct book { char name[50]; char author[50] ; float price; }; Write a function: struct book solution() that reads name, author and price into “struct book”. A function return structure variable. Input C Programming: A Modern Approach K.N.King 50.45 where, First line of input represents book name. Second line of input represents book author. Third line of input represents book price. Output Name: C Programming: A Modern Approach Author: K.N.King Price: 50.45 Assume that, “struct book” is already declared. Please do not use the Get function. When used and compiled there is an error stating that it is dangerous to use and that it shouldn't be used. It won't complete the output in our system bc of the error. Thank you.
Given code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFERSIZE 2
struct book
{
char name[50];
char author[50] ;
float price;
};
struct book solution()
{
//Write your code here
}
int main()
{
struct book b;
b = solution();
printf("Name=%s\nAuthor=%s\nPrice=%.2f",b.name,b.author,b.price);
return 0;
}
Here is the code : -
Code in text format : -
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFERSIZE 2
struct book
{
char name[50];
char author[50] ;
float price;
};
struct book solution()
{
char name[50], author[50];
float price;
printf("Enter book name-> ");
gets(name);
printf("Enter author name-> ");
gets(author);
printf("Enter book's price-> ");
scanf("%f", &price);
struct book temp;
for (int i = 0; i < 50; ++i)
{
temp.name[i]=name[i];
temp.author[i]=author[i];
}
temp.price=price;
return temp;
}
int main()
{
struct book b;
b = solution();
printf("Name=%s\nAuthor=%s\nPrice=%.2f",b.name,b.author,b.price);
return 0;
}