Question

I need help writing this program, below are the directions

Be sure that you downloaded the input file input.dat In the global space, define a struct called Album The members of the struct shall include: - artist - title year - numTracks Your program shall open the input.dat file and read the contents into memory - The very first value in the file always states how many albums are in the file - This is how youll know how large your array should be You could dynamically allocate the array After reading the albums from the file, the program will prompt the user if they want to add an album - Only one album will be added whenever the program is run The albums will then be sorted by artist Then the albums will be written back to input.dat

input.dat information

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<sstream>

using namespace std;

struct Album{
    string artist;
    string title;
    int year;
    int numTracks;
};

int main(){

   Album data[100];
   ifstream fin("input.dat");
   if (!fin){
       cout << "Error opening file\n";
       return 0;
   }
   int n;
   string line;
   getline(fin,line);
   n = atoi(line.c_str());
  
   for (int i = 0; i<n; i++){
       getline(fin,data[i].artist);
       getline(fin,data[i].title);
       //cout << data[i].title << " " << data[i].artist<< endl;
       string line;
       getline(fin,line);
       data[i].year = atoi(line.c_str());
       getline(fin,line);
       data[i].numTracks = atoi(line.c_str());
   }
   cout << "Want to add an album:(y/n) :";
   //string line;
   getline(cin,line);
   if (line[0] == 'y'){
       cout << "Enter artist:";
       getline(cin,data[n].artist);
       cout << "Enter title:";
       getline(cin,data[n].title);
       cout << "Enter year:";
       string line;
       getline(cin,line);
       data[n].year = atoi(line.c_str());
       cout << "Enter number of tracks:";
       getline(cin,line);
       data[n].numTracks = atoi(line.c_str());    
       n++;
   }
   /*
   for (int i = 0; i<n; i++){
      cout << data[i].artist << endl;
      cout << data[i].title << endl;
      cout << data[i].year << endl;
      cout << data[i].numTracks << endl;
   }
   */
   for (int i = 0; i<n; i++){
       for (int j = 0; j<n-1-i; j++){
           if (data[j].artist > data[j+1].artist){
               string tempa = data[j].artist;
               string tempt = data[j].title;
               int tempy = data[j].year;
               int tempn = data[j].numTracks;
               data[j].artist = data[j+1].artist;
               data[j].title = data[j+1].title;
               data[j].year = data[j+1].year;
               data[j].numTracks = data[j+1].numTracks;
               data[j+1].artist = tempa;
               data[j+1].title = tempt;
               data[j+1].year = tempy;
               data[j+1].numTracks = tempn;         
           }         
       }
   }
  
   ofstream fout("input.dat");
   fout << n << endl;
   for (int i = 0; i<n; i++){
      fout << data[i].artist << endl;
      fout << data[i].title << endl;
      fout << data[i].year << endl;
      fout << data[i].numTracks << endl;
   }
  
}

Add a comment
Know the answer?
Add Answer to:
I need help writing this program, below are the directions input.dat information Be sure that you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can anyone help me with my C hw? Exercise 3 You will write a new program...

    Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...

    Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program...

  • Hi, I need help with my comp sci assignment. The parameters are listed below, but I...

    Hi, I need help with my comp sci assignment. The parameters are listed below, but I am having trouble generating the number of occurrences of each word. Please use a standard library. Read in the clean text you generated in part 2 (start a new cpp file). Create a list of all the unique words found in the entire text file (use cleanedTextTest.txt for testing). Your list should be in the form of an array of structs, where each struct...

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

  • I need help making this in C++ Asap! If you feel information is missing, or provided...

    I need help making this in C++ Asap! If you feel information is missing, or provided information is incorrect, make assumptions but clearly state your assumptions. You should NEVER use global variables. Other than the main() function, there should be no global functions in your code. Other than the test() function not other function needs to be a static function in this problem. Let us say you find a consulting opportunity for a regional real-estate company that wants you to...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT