Question

C++ Question I have a text file that contains data from a CD (ex 1. Adagio...

C++ Question

I have a text file that contains data from a CD (ex 1. Adagio “MoonLight” Sonata - Ludwig Van Beethoven /n 2. An Alexis - F.H. Hummel and J.N. Hummel)

How do I sort the data by author since that information is in the middle of the string?

Here's what I have that sorts the string from the beginning:

if (file.is_open())
   {
       while (getline(file, line))
       {
           lines.push_back(line);
       }
   }
   else {
       cout << "Could not open file" << endl;
   }

   for (int i = 0; i < lines.size(); i++)
   {

//code

sort(lines.begin(), lines.end()); //sorts from beginning (song)

   for (int i = 0; i < lines.size(); i++)
       cout << lines[i] << "\n";

Need help sorting the author while keeping the entire string.

Homework Answers

Answer #1

#include<algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
using namespace std;
int main () {
ifstream myfile;
vector<string> v;
myfile.open ("file.txt");
if (myfile.is_open())
{
   string line;
    while ( getline (myfile,line) )
    {
      v.push_back(line);
    }
    myfile.close();
}
else cout << "Unable to open file";
vector<string>q;
vector<string>j;
    for (int i = 0; i < v.size(); i++){
       string s=v[i];
        string author1=s.substr(0, s.find("-"));
       string author2=s.substr(author1.length()+1,s.length())   ;
       q.push_back(author2+"- "+author1);
    }
    sort(q.begin(),q.end());
      for (int i = 0; i < q.size(); i++){
       string s=q[i];
        string author1=s.substr(0, s.find("-"));
       string author2=s.substr(author1.length()+1,s.length())   ;
       j.push_back(author2+"-"+author1);
    }
    for(int i=0;i<j.size();i++){
       cout <<j[i]<< "\n";
   }
return 0;
}

Know the answer?
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
Similar Questions
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix?...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix? #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" int main() {       int n;    int a[10], i;    int...
Below is the problem that I have and here is the code that I have in...
Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code. A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that prompts the user to enter the number of students...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. I also want to do this program using multiple threads concurrently in order to speed up the sorting process. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
Suppose that the file inData.txt contains the following data: 3   4 5 15.6 “Mark” “Taylor” 28...
Suppose that the file inData.txt contains the following data: 3   4 5 15.6 “Mark” “Taylor” 28 18500 3.5 B The numbers in the first line represent the Side A, Side B and Side C of a Triangle. The number in the second line represents the radius of a circle (Assume that p = 3.1416). The third line contains the first name, last name, and the age of a person. The first number in the fourth line is the savings account...
Language:C++ HAS TO WORK IN VISUAL BASIC I have errors on line 102 "expression must have...
Language:C++ HAS TO WORK IN VISUAL BASIC I have errors on line 102 "expression must have a constant value line 107,108,123,124,127,128: array type int[n] not assignable #include<iostream> #include <fstream> using namespace std; #define size 10000 // Displays the current Inventory Data void Display(int box_nums[],int nboxes[],double ppBox[],int n) // Prints Box number , number of boxes and price per box. { cout<<"Box number Number of boxes in stock Price per box"<<"\n"; cout<<"-------------------------------------------------------------\n"; for(int i=0; i<n; i++) { cout<<box_nums[i]<<" "<<nboxes[i]<<" "<<ppBox[i]<<"\n"; }...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
15.4 Zip code and population (class templates) Complete the TODOs in StatePair.h and main.cpp. StatePair.h Define...
15.4 Zip code and population (class templates) Complete the TODOs in StatePair.h and main.cpp. StatePair.h Define a class StatePair with two template types (T1 and T2), and constructor, mutators, accessors, and PrintInfo() member functions which will work correctly with main.cpp. Note, the three vectors (shown below) have been pre-filled with StatePair data in main() and do not require modification. vector<StatePair <int, string>> zipCodeState: ZIP code - state abbreviation pairs vector<StatePair<string, string>> abbrevState: state abbreviation - state name pairs vector<StatePair<string, int>>...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT