Question
Write a c program that takes in information about songs and stores the artist and title in an array of structs to be displayed. Dynamic
Write a c program that takes in information about songs and stores the artist and title in an array of structs to be displayed. Dynamic memory allocation is used to store the strings in the struct.
User Input The user will input ten pairs of strings about songs. The first string will contain the name of the artist. The second string will contain the title of the song.
As always, prompt the user before getting the input.
Assume the input string will be less than 40 characters long.
Data Structure Define a structure called songInfo that has two fields:
a pointer to a string for the artist a pointer to a string for the title Both pointers must be set up by using malloc() once you know how long each string is (failing to use malloc will result in a mark of 0). Dont forget to add one to the length for the null-termination.
Define songInfo before any of your functions. This does not violate the banning of global variables since these are data types, not variables.
Program Structure In main(), declare an array variable of struct songInfo of size 10.
Write a function called getSongInfo that takes a pointer to a struct songInfo (which is a pointer to an element of the array) and the two song information strings containing the artist and title information and fills in the struct's fields. Note the data type of the first parameter. You are taking in a pointer to a struct, not an array and not a struct.This function is also where you will allocate the two blocks of memory to contain the artist string and title string.
Write a function called printSongInfo that takes the array of structs as a parameter and prints all of the information contained within the array in a nicely-formatted fashion, one song per line. The artist must be displayed in the first 40 characters of the line (left-justified) and the title must be displayed in the next 40 characters of the line (left-justified). You must use printf() width specifiers (or research how to specify widths using cout) to help with your formatting.
Additional Requirements Note that getSongInfo must be called once for each song (artist/title pair) while printSongInfo must be called only once in total. You will likely want to call them from main().
You must free the memory allocated once you are done.
Do not use calloc(). Do not use the C++ new operator.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started