sorting - C++ substr() method seems to return 0 when used -


i'm writing program read in file of 5 buddy holly albums, sort them alphabetically, sort songs within each album alphabetically, , print album name, year came out, , sorted list of songs. catch here i'm supposed keep number next song comes out so:

buddy holly  1958 10.  baby don't care  7.  everyday  1.  i'm gonna love  4.  listen me 12.  little baby  3.  @ me  8.  mailman, bring me no more blues  2.  peggy sue  11.  rave on!  6.  ready teddy   5.  valley of tears   9.  words of love 

the problem comes when try sort songs. professor recommended use substr() method skip past first 4 characters in string start comparison @ first letter, when use it, keeps giving me error:

terminate called after throwing instance of 'std::out_of_range'   what():  basic_string::substr: __pos (which 4) > this->size() (which 0) 

i went , tried .size()/length() method see if got positive values(i did), , set int equal value, tried if statement see if int greater 0. not. i'm @ loss of here, last thing need finish thing.

my code:

#include <iostream> #include <string> #include <string.h> #include <istream> #include <stdio.h> #define 5 using namespace std;  struct buddyh {   string albumname;   string year;   string songs[12];   int count; };   int main()   {     buddyh bh[a], thd;     string st, jam;     bool change = true, change2 = true;      for(int = 0; < a; ++i)     { //start read-in  loop         int k = 0;         getline(cin, bh[i].albumname);         getline(cin, bh[i].year);         while (getline(cin, st))           {             if (st[0] == '=') break;             bh[i].songs[k] = st;             k++;           }         bh[i].count = k;     }//end read-in  loop     while(change)    {     change = false;      for(int j = 0; j < a-1; j++)      {         if(bh[j].albumname > bh[j+1].albumname)         {             thd = bh[j];            bh[j] = bh[j+1];            bh[j+1] = thd;            change = true;         }      }   }    while(change2)   {     int chk1, chk2;     change2 = false;     for(int x = 0; x < a; x++)     {       int s = sizeof(bh[x].songs)/sizeof(bh[x].songs[0]);       for(int y = 0; y < s-1; y++)       {          if(bh[x].songs[y].substr(4) > bh[x].songs[y+1].substr(4))         {           jam = bh[x].songs[y];           bh[x].songs[y] = bh[x].songs[y+1];           bh[x].songs[y+1] = jam;           change2 = true;         }       }    }  }     for(int c = 0; c < a; c++)   {      cout << bh[c].albumname << endl;      cout << bh[c].year << endl;       for(int = 0; < 12; i++)      {           cout << bh[c].songs[i] << endl;      }      cout << endl;   } } 

input file:

that'll day  1958   1.  1 desire  2.  blue days, black nights  3.  modern don juan  4.  rock around ollie vee  5.  ting ling  6.  girl on mind  7.  that'll day  8.  love me  9.  i'm changing changes 10.  don't come knockin' 11.  midnight shift ======================== buddy holly  1958  1.  i'm gonna love  2.  peggy sue  3.  @ me  4.  listen me  5.  valley of tears  6.  ready teddy   7.  everyday  8.  mailman, bring me no more blues  9.  words of love 10.  baby don't care 11.  rave on! 12.  little baby ========================  buddy holly story 1959   1.  raining in heart  2.  in morning  3.  peggy sue  4.  maybe baby  5.  everyday  6.  rave on!  7.  that'll day  8.  heartbeat  9.  think on 10.  oh, boy! 11.  it's easy! 12.  doesn't matter anymore ========================  buddy holly story, vol. 2  1960  1.  peggy sue got married  2.  well, right  3.   4.  makes tough  5.  we're 1  6.  take time  7.  crying, waiting, hoping  8.  true love ways  9.  learning game 10.  little baby 11.  moondreams 12.  that's ========================  reminiscing 1963  1.  reminiscing  2.  slippin' , slidin'  3.  bo diddley  4.  wait 'till sun shines, nellie  5.  baby, won't come out tonight  6.  brown eyed handsome man  7.  because love  8.  it's not fault  9.  i'm gonna set foot down 10.  changing changes 11.  rock-a-bye rock 

i think access array of songs has no element inside (empty string).

may ask purpose of line?

int s = sizeof(bh[x].songs)/sizeof(bh[x].songs[0]); 

if trying find total number of songs, replace line line:

int s = bh[x].count; 

i tried code , works. program sorts songs name described.

your program crashes when substr(4) empty string because not string in songs array filled. of albums contain 11 songs.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -