Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <vector>
using namespace std;

      struct CD_TYPE
      {
          string title, artist;
          int year;
          double cost;
      } ;

    void printData(struct <CD_TYPE> &collection[]); 
    
int main()
   {
       vector <CD_TYPE> collection(2);
       collection[0].title ="That's All Folks";
       collection[0].artist = "Porky Pig";
       collection[0].year = 2000;
       collection[0].cost = 12.95;
       collection[1].title ="I Thought I Saw A Putty Cat";
       collection[1].artist = "Tweety Bird";
       collection[1].year = 2001;
       collection[1].cost = 14.50;
       printData(collection);
   }   
   
void printData(struct <CD_TYPE> &collection[])
    {
        for (int i = 0; i < 5; i++)// loops are always or normally used to Print the data
        {
        cout<<collection[i].title<<"\n"<< collection[i].artist<<"\n"<<collection[i].year<<"\n"<<collection[i].cost<<endl;
        }
    }


What I have tried:

I have tried to resolve this issue by removing the function and the code then works, but I need this code to have a function, but for some reason, there is an error with my prototyping or something. Help would be greatly appreciated as this is a summative assignment, Thank you.

My Explanation
For some reason, my code is not working and there are 5 errors coming that are all related to the

functions of my code. I believe I did no properly pass the function from the main or that I did not

prototype them correctly.

Errors:
main.cpp:11:16: error: variable or field ‘printData’ declared void
void printData(struct <cd_type> &collection[]);
^ main.cpp:11:16: error: expected primary-expression before ‘struct’
main.cpp: In function ‘int main()’:
main.cpp:24:25: error: ‘printData’ was not declared in this scope
printData(collection);
^ main.cpp: At global scope:
main.cpp:27:16: error: variable or field ‘printData’ declared void
void printData(struct <cd_type> &collection[])
^ main.cpp:27:16: error: expected primary-expression before ‘struct’
Posted
Updated 3-Nov-20 11:32am
v2

You are trying to pass a vector reference to the printData function, so you should use the correct declaration:
C++
void printData(vector<CD_TYPE>& collection);

Also in your printData function you are trying to print 5 entries, but the collection only contains 2. You should use the vector's length property to find out how many items it contains. You could probably do without a vector as you are only using it as a simple array.
 
Share this answer
 
v2
Comments
CPallini 3-Nov-20 17:18pm    
5.
Member 14982907 3-Nov-20 18:12pm    
Thank you very much, you were able to fix the problem with the prototyping of my function and now I can submit my assignment.
Richard MacCutchan 4-Nov-20 2:25am    
You are welcome.
In addition to Richard's solution, I'd:

1) make collection const.
void printData(const vector<CD_TYPE>& collection);
2) Iterate the collection using:
for (const auto& item : collection)
3) Add
void print() const
method to CD_TYPE.

4) Use an unsigned for cost and store it in cents (I'd name the member costInCents) to avoid problems with floating point and money.
 
Share this answer
 
Comments
CPallini 3-Nov-20 17:19pm    
5.
You could also overload the insertion operator << in order to pass directly CD_TYPE objects to cout. I could show you the technique... And I'm going to show it, even if the 'don't do their homework' police is still around :-)
C++
#include <iostream>
#include <vector>
using namespace std;

struct CD_TYPE
{
  string title, artist;
  int year;
  double cost;
};

ostream & operator << ( ostream & os, const CD_TYPE & cd)
{
  os << cd.title << "\n" << cd.artist << "\n" << cd.year << "\n" << cd.cost;
  return os;
}

ostream & operator << ( ostream & os, const vector< CD_TYPE> & vcd)
{
  for (auto cd : vcd)
    os << cd << "\n";
  return os;
}

int main()
{
  vector <CD_TYPE> collection(2);
  collection[0].title ="That's All Folks";
  collection[0].artist = "Porky Pig";
  collection[0].year = 2000;
  collection[0].cost = 12.95;
  collection[1].title ="I Thought I Saw A Putty Cat";
  collection[1].artist = "Tweety Bird";
  collection[1].year = 2001;
  collection[1].cost = 14.50;

  cout << collection << endl;
 }
 
Share this answer
 
Comments
Joe Woodbury 3-Nov-20 17:34pm    
Much better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900