6 Replies - 340 Views - Last Post: 01 March 2018 - 10:43 PM Rate Topic: -----

#1 TrevMan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 18

Printing contents from a Vector (C++)

Posted 28 February 2018 - 09:12 PM

Hey, I am wanting to print the contents of a 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<char>') 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<<Print_Roll();", but that did not work either. Here is the code:

#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;
	//later we will see how to do these things using files & loops
	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;
        }
    }
    for(int i=0;i<R.size();i++){
        cout<<"ID: " << R[i].ID << " Score: " << R[i].Score << cout << " Grade: " << R[i].Grade;
    }
}//End of Print_Roll



Any help with me to better understand how to print the correct vector would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing contents from a Vector (C++)

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 825
  • View blog
  • Posts: 3,337
  • Joined: 13-June 14

Re: Printing contents from a Vector (C++)

Posted 28 February 2018 - 11:40 PM

Line 27 is the problem. The compiler doesn't know what it means to pass an S_type to <<. So, you either need to overload << for your class or instead on that line, say, make a string containing the output you want for each thing in Roll.

Also, about your naming: why aren't S_type and G_type called Student and Grade, respectively? That would make the code more expressive, since it's clear what they're modelling.

This post has been edited by ndc85430: 01 March 2018 - 01:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 TrevMan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 18

Re: Printing contents from a Vector (C++)

Posted 01 March 2018 - 12:41 PM

How would I overload <<?

This post has been edited by Skydiver: 01 March 2018 - 02:36 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Printing contents from a Vector (C++)

Posted 01 March 2018 - 01:14 PM

Your naming is rather painful.

Right, I'd start like so:
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

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

struct Student {
    int id;
    int score;
    GradeType grade;
    // having a contructor would be awesome here
    Student();
    Student(int id, int score);
};

typedef std::vector<Student> Students;

// this shouldn't be here
// vector<S_type> Roll; // vector to store student records

// declare all function prototypes
void Init_Roll(Students &);
int  Avg_Score(const Students &);
void Assign_Grades(Students &, int Avg);
void Print_Roll(const Students &);
void Show(const Students &);
// if you don't want to, or know how to, overload student, then
void Show(const Student &);

int main() {
    Students students;

    Init_Roll(students);
    Show(students);

    return 0;
} // end of main

  // define all your functions below
void Init_Roll(Students &xs) {
    // no S_type s;
    // write a bloody constructor
    // then
    xs.push_back(Student(108, 85))
    // s.ID = 108; s.Score = 85; s.Grade = Undefined;



Hope this helps.

This post has been edited by baavgai: 01 March 2018 - 01:14 PM
Reason for edit:: bad grammar

Was This Post Helpful? 0
  • +
  • -

#5 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 366
  • Joined: 09-September 16

Re: Printing contents from a Vector (C++)

Posted 01 March 2018 - 01:56 PM

@baavgai,
I thought in modern C++ we don't need ctors - or at least not often.
struct Student 
{
int id = 0;
int score = 0;
GradeType grade = Undefined;
};

// create a Student
Student s{1, 89, Okay};


Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Printing contents from a Vector (C++)

Posted 01 March 2018 - 04:27 PM

@Thomas1965: Indeed. But, even now, will students get to use cool C++11 stuff? The older, and therefore more common, constructor form will still to be covered, maybe exclusively.
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 825
  • View blog
  • Posts: 3,337
  • Joined: 13-June 14

Re: Printing contents from a Vector (C++)

Posted 01 March 2018 - 10:43 PM

View PostTrevMan, on 01 March 2018 - 07:41 PM, said:

How would I overload <<?


What have you gone and found out about doing that? I'd expect there's something on this in whatever book you're using to learn the language.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1