944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2127
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 16th, 2007
0

Please help with assignment

Expand Post »
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.



C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cstdlib>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. struct Song
  9. {
  10. string title;
  11. int track;
  12. };
  13.  
  14. struct Album
  15. {
  16. string albumName;
  17. string artistName;
  18. struct Song songList[4];
  19. };
  20.  
  21. int main()
  22. {
  23. Album collection[5];
  24.  
  25. collection[0].albumName = "Private Investigations";
  26. collection[0].artistName = "Dire Straits";
  27. collection[0].songList[0].title = "Sultans of Swing";
  28. collection[0].songList[0].track = 2;
  29. collection[0].songList[1].title = "Romeo and Juliet ";
  30. collection[0].songList[1].track = 4;
  31. collection[0].songList[2].title = "Money for Nothing ";
  32. collection[0].songList[2].track = 9;
  33. collection[0].songList[3].title = "Walk of Life ";
  34. collection[0].songList[3].track = 10;

C++ Syntax (Toggle Plain Text)
  1. string albumName;
  2.  
  3. cout<<"what album to search for? ";
  4. cin>> albumName;
  5. }
  6.  
  7. int search(Album collection[], string albumName)
  8. {
  9. int location;
  10. for(int i = 0; i<5; i++);
  11. {
  12. if(location == -1)
  13. return -1;
  14. else
  15. <code class="inlinecode">cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";</code>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kpillsb39 is offline Offline
16 posts
since Jun 2007
Jun 16th, 2007
0

Re: Please help with assignment

Do you know how to compare strings?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 16th, 2007
0

Re: Please help with assignment

>have i missed something in the code?
Probably. You don't initialize or set the location variable in your search. So basically the entire thing is hopelessly broken as posted.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 16th, 2007
0

Re: Please help with assignment

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.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cstdlib>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. struct Song
  9. {
  10. string title;
  11. int track;
  12. };
  13.  
  14. struct Album
  15. {
  16. string albumName;
  17. string artistName;
  18. struct Song songList[4];
  19. };
  20.  
  21. int main()
  22. {
  23. Album collection[5];
  24.  
  25. collection[0].albumName = "Private Investigations";
  26. collection[0].artistName = "Dire Straits";
  27. collection[0].songList[0].title = "Sultans of Swing";
  28. collection[0].songList[0].track = 2;
  29. collection[0].songList[1].title = "Romeo and Juliet ";
  30. collection[0].songList[1].track = 4;
  31. collection[0].songList[2].title = "Money for Nothing ";
  32. collection[0].songList[2].track = 9;
  33. collection[0].songList[3].title = "Walk of Life ";
  34. collection[0].songList[3].track = 10;

C++ Syntax (Toggle Plain Text)
  1. string albumName;
  2.  
  3. cout<<"what album to search for? ";
  4. cin>> albumName;
  5. }
  6.  
  7. int search(Album collection[], string albumName)
  8. {
  9. int location = -1;
  10. for(int i = 0; i<5; i++)
  11. if(strcmp(collection[i].albumName, albumName)==0)
  12. {
  13. location = i;break;
  14. }
  15. if (location != -1)
  16. {
  17. cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";
  18. return location;
  19. }
  20. else return -1;
  21. }
[/QUOTE]
Last edited by Chaster; Jun 16th, 2007 at 2:05 pm.
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Jun 16th, 2007
0

Re: Please help with assignment

C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i<5; i++)
  2. if(strcmp(collection[i].albumName, albumName)==0)
  3. {
  4. location = i;break;
  5. }
That's a good way to confuse the hell out of readers and introduce subtle scoping bugs. My general guideline for blocks with optional braces is that if there's a one line statement, you can go ahead with the omission. If the block has more than one line, including comments, use braces. So your code would be:
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i<5; i++)
  2. {
  3. if(strcmp(collection[i].albumName, albumName)==0)
  4. {
  5. location = i;break;
  6. }
  7. }
Of course, that brings me to the real error, which is using strcmp with std::string objects. Not such a hot idea, especially since the string class overloads the == operator for comparison. I'm guessing you didn't compile that code.
C++ Syntax (Toggle Plain Text)
  1. if (location != -1)
  2. {
  3. cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";
  4. return location;
  5. }
  6. else return -1;
I think you fell into the trap that I described above. The second if statement isn't inside the loop. The code shouldn't compile because i isn't visible anymore.

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)
  1. int search ( Album collection[], int size, string albumName )
  2. {
  3. for ( int i = 0; i < size; i++ ) {
  4. if ( collection[i].albumName == albumName )
  5. return i;
  6. }
  7.  
  8. return -1;
  9. }
And of course, unless you know for a fact that your code will compile and run as expected (which I do in this case), you should write a test framework. Something like so:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. Album collection[5];
  4.  
  5. collection[0].albumName = "Private Investigations1";
  6. collection[1].albumName = "Private Investigations2";
  7. collection[2].albumName = "Private Investigations3";
  8. collection[3].albumName = "Private Investigations4";
  9. collection[4].albumName = "Private Investigations5";
  10.  
  11. cout<< search ( collection, 5, "Private Investigations1" ) <<'\n';
  12. cout<< search ( collection, 5, "Private Investigations2" ) <<'\n';
  13. cout<< search ( collection, 5, "Private Investigations3" ) <<'\n';
  14. cout<< search ( collection, 5, "Private Investigations4" ) <<'\n';
  15. cout<< search ( collection, 5, "Private Investigations5" ) <<'\n';
  16. cout<< search ( collection, 5, "Private Investigations" ) <<'\n';
  17. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 16th, 2007
0

Re: Please help with assignment

I appreciate all who have posted to this Thread, but when i compile, i get no errors, but when i type in the name of the album to search the array for. it returns the statement
Quote ...
Press any key to continue.
With this, why is it not searching the array?
or is it and not finding the album name.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kpillsb39 is offline Offline
16 posts
since Jun 2007
Jun 16th, 2007
0

Re: Please help with assignment

You'll need to post your current code.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 16th, 2007
0

Re: Please help with assignment

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.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cstdlib>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. struct Song
  9. {
  10. string title;
  11. int track;
  12. };
  13.  
  14. struct Album
  15. {
  16. string albumName;
  17. string artistName;
  18. struct Song songList[4];
  19. };
  20.  
  21. int main()
  22. {
  23. Album collection[5];
  24.  
  25. collection[0].albumName = "Private Investigations";
  26. collection[0].artistName = "Dire Straits";
  27. collection[0].songList[0].title = "Sultans of Swing";
  28. collection[0].songList[0].track = 2;
  29. collection[0].songList[1].title = "Romeo and Juliet ";
  30. collection[0].songList[1].track = 4;
  31. collection[0].songList[2].title = "Money for Nothing ";
  32. collection[0].songList[2].track = 9;
  33. collection[0].songList[3].title = "Walk of Life ";
  34. collection[0].songList[3].track = 10;
  35. collection[1].albumName = "The Millennium Collection";
  36. collection[1].artistName = "The Who";
  37. collection[1].songList[0].title = "My Generation ";
  38. collection[1].songList[0].track = 1;
  39. collection[1].songList[1].title = "Pinball Wizard ";
  40. collection[1].songList[1].track = 2;
  41. collection[1].songList[2].title = "Who are You ";
  42. collection[1].songList[2].track = 3;
  43. collection[1].songList[3].title = "Squeeze Box ";
  44. collection[2].songList[3].track = 4;
  45. collection[2].albumName = "Moving Pictures";
  46. collection[2].artistName = "RUSH";
  47. collection[2].songList[0].title = "Tom Sawyer";
  48. collection[2].songList[0].track = 1;
  49. collection[2].songList[1].title = "Rad Barchetta";
  50. collection[2].songList[1].track = 2;
  51. collection[2].songList[2].title = "YYZ";
  52. collection[2].songList[2].track = 3;
  53. collection[2].songList[3].title = "Limelight";
  54. collection[2].songList[3].track = 4;
  55. collection[3].albumName = "The Best of 3 Dog Night";
  56. collection[3].artistName = "3 Dog Night";
  57. collection[3].songList[0].title = "Joy to the World";
  58. collection[3].songList[0].track = 1;
  59. collection[3].songList[1].title = "Easy to be Hard";
  60. collection[3].songList[1].track = 2;
  61. collection[3].songList[2].title = "Family of Man";
  62. collection[3].songList[2].track = 3;
  63. collection[3].songList[3].title = "Sure as i'm sitting here";
  64. collection[3].songList[3].track = 4;
  65. collection[4].albumName = "The Best of Deep Purple";
  66. collection[4].artistName = "Deep Purple";
  67. collection[4].songList[0].title = "HUSH";
  68. collection[4].songList[0].track = 1;
  69. collection[4].songList[1].title = "Kentucky Woman";
  70. collection[4].songList[1].track = 2;
  71. collection[4].songList[2].title = "Black Night";
  72. collection[4].songList[2].track = 3;
  73. collection[4].songList[3].title = "Speed King";
  74. collection[4].songList[3].track = 4;
  75.  
  76. string albumName;
  77. cout<<"What album to search for? ";
  78. cin>> albumName;
  79.  
  80. }
  81. int search(Album collection[], int size, string albumName)
  82. {
  83. for(int i = 0; i<size; i++)
  84. {
  85. if (collection[i].albumName == albumName)
  86. return i;
  87. }
  88. return -1;
  89. }


Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kpillsb39 is offline Offline
16 posts
since Jun 2007
Jun 16th, 2007
0

Re: Please help with assignment

You need to call your search function. Just defining it doesn't do anything.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 16th, 2007
0

Re: Please help with assignment

Narue,
Sorry but you lost me with the call the search function, i must have missed that portion of the class.

What and where in the code should it go.

Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kpillsb39 is offline Offline
16 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Coressponding Linux function/library out of Win32...
Next Thread in C++ Forum Timeline: Doubts about References





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC