| | |
Please help with assignment
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 16
Reputation:
Solved Threads: 0
What i have to create is a program that establishes 2 structs with arrays of CD Albums and then search the arrays for album names and display the album name, Artist or group, songs and their track numbers.
My question is; have i missed something in the code?, because it does not display the first album in the array when i do the search, it allows me to enter the name to search for but does not conduct the search, i would like to know what is wrong with the code.
thank you.
My question is; have i missed something in the code?, because it does not display the first album in the array when i do the search, it allows me to enter the name to search for but does not conduct the search, i would like to know what is wrong with the code.
thank you.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<iomanip> #include<cstdlib> #include<string> using namespace std; struct Song { string title; int track; }; struct Album { string albumName; string artistName; struct Song songList[4]; }; int main() { Album collection[5]; collection[0].albumName = "Private Investigations"; collection[0].artistName = "Dire Straits"; collection[0].songList[0].title = "Sultans of Swing"; collection[0].songList[0].track = 2; collection[0].songList[1].title = "Romeo and Juliet "; collection[0].songList[1].track = 4; collection[0].songList[2].title = "Money for Nothing "; collection[0].songList[2].track = 9; collection[0].songList[3].title = "Walk of Life "; collection[0].songList[3].track = 10;
C++ Syntax (Toggle Plain Text)
string albumName; cout<<"what album to search for? "; cin>> albumName; } int search(Album collection[], string albumName) { int location; for(int i = 0; i<5; i++); { if(location == -1) return -1; else <span class="ad_notxt"><code class="inlinecode">cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";</code></span>
•
•
Join Date: Jun 2007
Posts: 59
Reputation:
Solved Threads: 3
the semicolon after the for statement is a really bad idea. This for will only count up to 5, becouse with the ";" you close the loop statement. On the other hand you should compare the elements of the collection with the searched string.
[/QUOTE]
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<iomanip> #include<cstdlib> #include<string> using namespace std; struct Song { string title; int track; }; struct Album { string albumName; string artistName; struct Song songList[4]; }; int main() { Album collection[5]; collection[0].albumName = "Private Investigations"; collection[0].artistName = "Dire Straits"; collection[0].songList[0].title = "Sultans of Swing"; collection[0].songList[0].track = 2; collection[0].songList[1].title = "Romeo and Juliet "; collection[0].songList[1].track = 4; collection[0].songList[2].title = "Money for Nothing "; collection[0].songList[2].track = 9; collection[0].songList[3].title = "Walk of Life "; collection[0].songList[3].track = 10;
C++ Syntax (Toggle Plain Text)
string albumName; cout<<"what album to search for? "; cin>> albumName; } int search(Album collection[], string albumName) { int location = -1; for(int i = 0; i<5; i++) if(strcmp(collection[i].albumName, albumName)==0) { location = i;break; } if (location != -1) { cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<"."; return location; } else return -1; }
Last edited by Chaster; Jun 16th, 2007 at 2:05 pm.
C++ Syntax (Toggle Plain Text)
for(int i = 0; i<5; i++) if(strcmp(collection[i].albumName, albumName)==0) { location = i;break; }
C++ Syntax (Toggle Plain Text)
for(int i = 0; i<5; i++) { if(strcmp(collection[i].albumName, albumName)==0) { location = i;break; } }
C++ Syntax (Toggle Plain Text)
if (location != -1) { cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<"."; return location; } else return -1;
On top of that, you've complicated the function a little too much. It could be as simple as this:
C++ Syntax (Toggle Plain Text)
int search ( Album collection[], int size, string albumName ) { for ( int i = 0; i < size; i++ ) { if ( collection[i].albumName == albumName ) return i; } return -1; }
C++ Syntax (Toggle Plain Text)
int main() { Album collection[5]; collection[0].albumName = "Private Investigations1"; collection[1].albumName = "Private Investigations2"; collection[2].albumName = "Private Investigations3"; collection[3].albumName = "Private Investigations4"; collection[4].albumName = "Private Investigations5"; cout<< search ( collection, 5, "Private Investigations1" ) <<'\n'; cout<< search ( collection, 5, "Private Investigations2" ) <<'\n'; cout<< search ( collection, 5, "Private Investigations3" ) <<'\n'; cout<< search ( collection, 5, "Private Investigations4" ) <<'\n'; cout<< search ( collection, 5, "Private Investigations5" ) <<'\n'; cout<< search ( collection, 5, "Private Investigations" ) <<'\n'; }
I'm here to prove you wrong.
•
•
Join Date: Jun 2007
Posts: 16
Reputation:
Solved Threads: 0
I have added additional album names, artists, songs and track numbers to the array.
I also made the changes that were submitted in this thread and compiled with no errors.
Still no results.
Thank you.
I also made the changes that were submitted in this thread and compiled with no errors.
Still no results.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<iomanip> #include<cstdlib> #include<string> using namespace std; struct Song { string title; int track; }; struct Album { string albumName; string artistName; struct Song songList[4]; }; int main() { Album collection[5]; collection[0].albumName = "Private Investigations"; collection[0].artistName = "Dire Straits"; collection[0].songList[0].title = "Sultans of Swing"; collection[0].songList[0].track = 2; collection[0].songList[1].title = "Romeo and Juliet "; collection[0].songList[1].track = 4; collection[0].songList[2].title = "Money for Nothing "; collection[0].songList[2].track = 9; collection[0].songList[3].title = "Walk of Life "; collection[0].songList[3].track = 10; collection[1].albumName = "The Millennium Collection"; collection[1].artistName = "The Who"; collection[1].songList[0].title = "My Generation "; collection[1].songList[0].track = 1; collection[1].songList[1].title = "Pinball Wizard "; collection[1].songList[1].track = 2; collection[1].songList[2].title = "Who are You "; collection[1].songList[2].track = 3; collection[1].songList[3].title = "Squeeze Box "; collection[2].songList[3].track = 4; collection[2].albumName = "Moving Pictures"; collection[2].artistName = "RUSH"; collection[2].songList[0].title = "Tom Sawyer"; collection[2].songList[0].track = 1; collection[2].songList[1].title = "Rad Barchetta"; collection[2].songList[1].track = 2; collection[2].songList[2].title = "YYZ"; collection[2].songList[2].track = 3; collection[2].songList[3].title = "Limelight"; collection[2].songList[3].track = 4; collection[3].albumName = "The Best of 3 Dog Night"; collection[3].artistName = "3 Dog Night"; collection[3].songList[0].title = "Joy to the World"; collection[3].songList[0].track = 1; collection[3].songList[1].title = "Easy to be Hard"; collection[3].songList[1].track = 2; collection[3].songList[2].title = "Family of Man"; collection[3].songList[2].track = 3; collection[3].songList[3].title = "Sure as i'm sitting here"; collection[3].songList[3].track = 4; collection[4].albumName = "The Best of Deep Purple"; collection[4].artistName = "Deep Purple"; collection[4].songList[0].title = "HUSH"; collection[4].songList[0].track = 1; collection[4].songList[1].title = "Kentucky Woman"; collection[4].songList[1].track = 2; collection[4].songList[2].title = "Black Night"; collection[4].songList[2].track = 3; collection[4].songList[3].title = "Speed King"; collection[4].songList[3].track = 4; string albumName; cout<<"What album to search for? "; cin>> albumName; } int search(Album collection[], int size, string albumName) { for(int i = 0; i<size; i++) { if (collection[i].albumName == albumName) return i; } return -1; }
Thank you.
![]() |
Similar Threads
- Java Assignment Help Needed!!!!!!!!!! (Java)
- Help needed with VB Assignment (Visual Basic 4 / 5 / 6)
- Many Errors while doing this assignment (C)
- I need help on my 'address book' assignment! (C++)
Other Threads in the C++ Forum
- Previous Thread: Coressponding Linux function/library out of Win32...
- Next Thread: Doubts about References
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int integer java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






