0

I am new to the C++ scene and I am wanting to print the contents of my Vector out into the screen. I am having trouble finding the right code of being able to print what is in the vector out, as I keep getting the error: "error: invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'value_type' (aka 'S_type'))|"

I'll give all of the code I have. I have tried different things trying to get to print the vector such as "cout<

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

enum G_type  {Dandy, Okay, Weak, Undefined}; // enumerated type for grades

struct  S_type  // defines a student record
{
    int         ID;
    int         Score;
    G_type      Grade;
};

vector<S_type> Roll; // vector to store student records

// declare all function prototypes
void Init_Roll ( vector<S_type>& R);
int  Avg_Score (vector<S_type> R);
void Assign_Grades (vector<S_type>& R, int Avg);
void Print_Roll (vector<S_type> R);

int main()
{
    for(int i=0; i<Roll.size();i++)
    cout << Roll[i];

return 0;
} // end of main

// define all your functions below
void Init_Roll (vector<S_type>& R){
S_type s; //to assemble temporary record
//use the test data provided to insert 12 student records, one by one;
s.ID=108;s.Score=85;s.Grade=Undefined;
R.push_back(s);
s.ID=122;s.Score=73;s.Grade=Undefined;
R.push_back(s);
s.ID=130;s.Score=62;s.Grade=Undefined;
R.push_back(s);
s.ID=140;s.Score=70;s.Grade=Undefined;
R.push_back(s);
s.ID=147;s.Score=80;s.Grade=Undefined;
R.push_back(s);
s.ID=240;s.Score=92;s.Grade=Undefined;
R.push_back(s);
s.ID=320;s.Score=88;s.Grade=Undefined;
R.push_back(s);
s.ID=325;s.Score=58;s.Grade=Undefined;
R.push_back(s);
s.ID=350;s.Score=67;s.Grade=Undefined;
R.push_back(s);
s.ID=400;s.Score=79;s.Grade=Undefined;
R.push_back(s);
s.ID=420;s.Score=85;s.Grade=Undefined;
R.push_back(s);
s.ID=500;s.Score=72;s.Grade=Undefined;
R.push_back(s);
}//end of Init_Roll

int  Avg_Score (vector<S_type> R){
    int sum = 0;
    for(int k=0;k<R.size();k++)
    sum = sum + R[k].Score;
return sum/R.size();
}//End of Avg_Score

void Assign_Grades (vector<S_type>& R, int Avg){
for(int k=0;k<R.size();k++){

if(R[k].Score<Avg-10)
R[k].Grade = Weak;

else if(R[k].Score>Avg+10)
R[k].Grade = Dandy;

else
R[k].Grade = Okay;
}
}//End of Assign_Grades

void Print_Roll (vector<S_type> R){
for(int k=0;k<R.size();k++){
    switch(R[k].Grade){
    case Dandy: cout<<"Dandy";
    break;

    case Okay: cout<<"Okay";
    break;

    case Weak: cout<<"Weak";
    break;

    case Undefined: cout<<"Undefined";
    break;
    }
}
for(int i=0;i<R.size();i++){
    cout<<"ID: " << R[i].ID << " Score: " << R[i].Score << cout << " Grade: " << R[i].Grade << '\n';
}
}//End of Print_Roll

Question is, how do I get the Roll vector to print out to the screen?

  • What is the question? – Ron Mar 01 '18 at 19:14
  • To print to the contents of the vector to the screen. – T.Drummonds Mar 01 '18 at 19:15
  • Have a look at the [std::vector](http://en.cppreference.com/w/cpp/container/vector) reference and these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Mar 01 '18 at 19:16
  • You never call `Init_Roll()`. All your `main()` does is try to print an empty `vector`. – 001 Mar 01 '18 at 19:16
  • _"To print to the contents of the vector to the screen"_ is not a question... – Lightness Races in Orbit Mar 01 '18 at 19:18
  • Please provide a *minimal* example, stripped of everything that isn't needed. The site guidelines explain how to create an MCVE. – Ulrich Eckhardt Mar 01 '18 at 19:18
  • Well, _HOW_ do I print the contents of the vector to the screen? – T.Drummonds Mar 01 '18 at 19:22
  • @Ulrich, I thought that maybe providing everything I had, and showing I added contents to the vector may allow someone to help answer my question. – T.Drummonds Mar 01 '18 at 19:23
  • Using the post you gave I no longer get the same error. However I still can't get my vector to print. Here is what I have now `enum G_type {Dandy, Okay, Weak, Undefined}; struct S_type{ int ID; int Score; G_type Grade; }; std::ostream &operator<<(std::ostream &os, S_type const &m) { return os << m.ID << m.Score << m.Grade; } vector Roll; void Init_Roll ( vector& R); int main(){ Init_Roll(Roll); for(int i=0; i – T.Drummonds Mar 01 '18 at 19:31
  • @T.Drummonds Your updated code works: https://ideone.com/0BhLk5 – 001 Mar 01 '18 at 19:51
  • @Johnny, thank you for letting me know. Maybe it's just the IDE I'm having to use. I am on Mac and having to use Code::Blocks. – T.Drummonds Mar 02 '18 at 00:23

0 Answers0