Please help with assignment

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 16
Reputation: kpillsb39 is an unknown quantity at this point 
Solved Threads: 0
kpillsb39 kpillsb39 is offline Offline
Newbie Poster

Please help with assignment

 
0
  #1
Jun 16th, 2007
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.



  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;

  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. <span class="ad_notxt"><code class="inlinecode">cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";</code></span>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please help with assignment

 
0
  #2
Jun 16th, 2007
Do you know how to compare strings?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help with assignment

 
0
  #3
Jun 16th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

Re: Please help with assignment

 
0
  #4
Jun 16th, 2007
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.

  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;

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help with assignment

 
0
  #5
Jun 16th, 2007
  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:
  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.
  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:
  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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 16
Reputation: kpillsb39 is an unknown quantity at this point 
Solved Threads: 0
kpillsb39 kpillsb39 is offline Offline
Newbie Poster

Re: Please help with assignment

 
0
  #6
Jun 16th, 2007
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
Press any key to continue.
With this, why is it not searching the array?
or is it and not finding the album name.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help with assignment

 
0
  #7
Jun 16th, 2007
You'll need to post your current code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 16
Reputation: kpillsb39 is an unknown quantity at this point 
Solved Threads: 0
kpillsb39 kpillsb39 is offline Offline
Newbie Poster

Re: Please help with assignment

 
0
  #8
Jun 16th, 2007
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help with assignment

 
0
  #9
Jun 16th, 2007
You need to call your search function. Just defining it doesn't do anything.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 16
Reputation: kpillsb39 is an unknown quantity at this point 
Solved Threads: 0
kpillsb39 kpillsb39 is offline Offline
Newbie Poster

Re: Please help with assignment

 
0
  #10
Jun 16th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC