i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order. - /*
-
*bunch of code
-
*/
-
struct album{
-
string album;
-
string artist[6];
-
string songs[21];
-
}
-
/* morecode */
-
int main(){
-
album p;
-
//more code
then i read in a text file that contains all the struct's info but puts it into an array, then a big horrible if statement that coppies the info from the array to the struct
the problem is that in the txt file the songs are listed as follows
1. Track1
2. Track2
3. Not the actual track names
et cetera. on to Track21.
i have to sort based on song name.
i know i need to search each string for the '.'
then check the next character after the space.
(oh yeah, the sort is a function)
something along the lines of - void ssongssort(string ar[]){
-
bool done;
-
for(int r,done = 0; !done ; ) {
-
for (done = ! ( r = 0 ) ; r < 20 ; r + + ) {
-
string temp1= ar[r].substr(5, 1);// the problem is here
-
string temp2 = ar[r+1].substr(5, 1);// and here
-
temp2.assign(temp2,4,15);
-
if(temp1 > temp2){
-
swap(ar[r],ar[r+1]);
-
done = 0;
-
}
-
}
-
}
-
}
i've seen a nother way, something like
(imagine the strings as " - Track1") - string gettrack(string st){
-
int t = st.find (" - ",0) + 2;// why would lentgh be 0?
-
while (st[t] == ' ') t++;// the 2 would be 2 characters down.
-
return st.substr(t);
-
}
-
//which returns the location of the '-'
however i don't think - p.albumname[0].find(" - ",0)+2;
would work.
8 1918
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order. - /*
-
*bunch of code
-
*/
-
struct album{
-
string album;
-
string artist[6];
-
string songs[21];
-
}
-
/* morecode */
-
int main(){
-
album p;
-
//more code
-
/*then i read in a text file that contains all the struct's info but puts it into an array, then a big horrible if statement that coppies the info from the array to the struct
-
-
the problem is that in the txt file the songs are listed as follows
-
1. Track1
-
2. Track2
-
3. Not the actual track names
-
et cetera. on to Track21.
-
-
i have to sort based on song name.
-
i know i need to search each string for the '.'
-
then check the next character after the space.
-
-
(oh yeah, the sort is a function)
-
something along the lines of
-
void ssongssort(string ar[]){
-
bool done;
-
for(int r,done = 0; !done ; ) {
-
for (done = ! ( r = 0 ) ; r < 20 ; r + + ) {
-
string temp1= ar[r].substr(5, 1);// the problem is here
-
string temp2 = ar[r+1].substr(5, 1);// and here
-
temp2.assign(temp2,4,15);
-
if(temp1 > temp2){
-
swap(ar[r],ar[r+1]);
-
done = 0;
-
}
-
}
-
}
-
}
-
-
i've seen a nother way, something like
-
(imagine the strings as " - Track1")
-
string gettrack(string st){
-
int t = st.find (" - ",0) + 2;// why would lentgh be 0?
-
while (st[t] == ' ') t++;// the 2 would be 2 characters down.
-
return st.substr(t);
-
}
-
//which returns the location of the '-'
however i don't think
p.albumname[0].find(" - ",0)+2;
would work.
Hi, pabl0
First when posting please use code tags(Just select your code copied/wriiten
here and click #),it makes code easier to read.
Secound i don't think that your for loops are good designed,Why are you using
double for loop? You could only use one do-while loop,like: - do{
-
-
string temp1= ar[r].substr(5, 1);
-
string temp2 = ar[r+1].substr(5, 1);
-
temp2.assign(temp2,4,15);
-
if(temp1 > temp2){
-
swap(ar[r],ar[r+1]);
-
r++;
-
done = 0;
-
}else done =1;
-
-
while(done==0);
Hope this helps.
Savage
it's just a bubble sort (and i've seen some mighty fine one line bubble sorts),
and besides thats not the problem.
the string search is.
Just one qeustion(Myabe I haven't understood you well):
Is the txt file with no actual track names one that you put in your arrays or
is that a output txt file
someting along the lines of
ifstream in ("songlist.dat");
string z;
int i = 1;
while (getline(in, z)) {
ar[i] = z;
cout << setw(3) << i++ << " " << z << "\n";
}
someting along the lines of
ifstream in ("songlist.dat");
string z;
int i = 1;
while (getline(in, z)) {
ar[i] = z;
cout << setw(3) << i++ << " " << z << "\n";
}
So correct me if I'm wrong:
It's the file inputed.You have writen one song in one line and used getline to
get song name.You also set field width to 3 using setw,and couted i, then
couted z and forced start of new line.
Can you post one line of songlist.dat just to see how is it formated?
Savage
the .dat file is formattd as follows.
Wolfgang Amadeus Mozart //0th element of array
1. Bassoon Concerto in B flat major, K. 191 //1st
2. Concerto for Harp, Flute and Orchestra, K. 299 //2nd
3. Oboe Concerto in C major, K. 314 //3rd
4. Concerto for Clarinet and Orchestra in A major, K. 622 // 4th
5. Flute Concerto No. 1 in G Major, K. 313 // and so on and so forth...
I'm not sure wether this is written corectly for my purpus:
string temp1= ar[r].substr(5, 1);
string temp2 = ar[r+1].substr(5, 1);
setting the temp values as the 5th character of the string,
then later compares those temp values.
the .dat file is formattd as follows.
Wolfgang Amadeus Mozart //0th element of array
1. Bassoon Concerto in B flat major, K. 191 //1st
2. Concerto for Harp, Flute and Orchestra, K. 299 //2nd
3. Oboe Concerto in C major, K. 314 //3rd
4. Concerto for Clarinet and Orchestra in A major, K. 622 // 4th
5. Flute Concerto No. 1 in G Major, K. 313 // and so on and so forth...
I'm not sure wether this is written corectly for my purpus:
string temp1= ar[r].substr(5, 1);
string temp2 = ar[r+1].substr(5, 1);
setting the temp values as the 5th character of the string,
then later compares those temp values.
Maybe,you don't even have to use strings,instead use char arrays and compare
first char array with another using same array index.
if i knew C i'd mess with the char arrays, but i don't know C.
however i finally got it to work, for the most part. -
void SongSort( album b[], int r){
-
string str[n];
-
for(int j = 0; j < 5; j++){
-
for(int m = 0; m < r; m++){
-
if( b[j].songs[m] != ""){
-
string temp88 = b[j].songs[m];
-
temp88 = temp88.substr(4,temp88.length()-1); // starts at the 4th character
-
str[m] = temp88;// couldn't have b[j].songs[m].substr(4,temp88.length()-1)
-
b[j].songs[m] = temp88;
-
}
-
}
-
for(int i,done = 0; !done;){
-
for (done = !(i = 0); i < r-1; i++){
-
if (str[i] > str[i+1]){
-
swap(str[i], str[i+1]);
-
swap(b[j].songs[i], b[j].songs[i+1]), done = 0;
-
}
-
}
-
}
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Espen Ruud Schultz |
last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string's "content" so that the char pointer can point to
the same text? And vice versa; can I give...
|
by: Nobody |
last post by:
Heres the deal... I have an application where I have a list (as in a Windows
list control, but thats not important) displayed to the user. I sort this
list based on the list controls sort function...
|
by: TituscroW |
last post by:
Hello,
This is my first post, so I hope I include all the right information.
I have a Table which has a list of numbers stored in a string, which are not
in numerical order
For example "10 2 35...
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have...
|
by: Julie |
last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB)
for a given string.
The files are unindexed and unsorted, and for the purposes of my immediate
requirements, can't...
|
by: Chad Myers |
last post by:
I've been perf testing an application of mine and I've noticed that there
are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String
instances being created.
I've done some...
|
by: Don |
last post by:
Is it possible to somehow extend the string class (i.e. add new members to
it)?
|
by: aparnakakkar2003 |
last post by:
can any one tell me if I give the followiing string in input:
ABC
abc
BBC
then how I can get
ABC
abc
BBC
|
by: JosAH |
last post by:
Greetings,
I was asked to write a Tip Of the Week; so here goes: a lot of topics are
started here in this forum (and a lot of other forums too) mentioning a
problem about sorting data.
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| | |