Sort string array by alphabet and display values - c++

I am trying to sort an array with is filled with string values by alphabet. It already works with integers. I think the problem isn’t about the sorting, it is about displaying it.
But here is the code:
#include <iostream>
#include <algorithm>
using namespace std;
class Ticket {
int ticketnr;
string name;
public:
Ticket() {
ticketnr = 0;
name = "NN";
};
Ticket(int _tickernr, string _name) {
ticketnr = _tickernr;
name = _name;
}
friend bool upSort(Ticket a, Ticket b);
};
bool upSort(Ticket a, Ticket b) {
return (a.name > b.name);
}
int main() {
Ticket vip(1435, "Beckenbauer");
Ticket frei;
Ticket array[10] = {vip, Ticket(2100, "Maier")};
sort(array, array + 10, upSort);
for (int i = 0; i < 10; i++) cout << array[i] << endl;
}
Xcode says: invalid operands to binary expression
Thank you and best regards
Flo

There is probably nothing wrong with the sort. What is clearly wrong is that you don't have an ostream& operator<< for Ticket, so you cannot do this:
for (int i = 0; i < 10; i++) cout << array[i] << endl;
// ^^^^^^^^^^^^^^^^
So,
friend
std::ostream& operator<<(std::ostream& o, const Ticket& t)
{
return o << t.ticketnr << " " << t.name;
}

Related

Trouble with operator << in class StringSet

I am defining my own string class called StringSet using a vector of strings. I am assigned to overload the >>, <<, ==, >, >=, +, += and * operators, and ran into a problem with <<. The output should be:
Welcome to stringset
hi everyone
"all" does not exist in the set.
hi
But it seems to be skipping the second and third lines. I am very new to overloading operators, so I am probably overlooking an obvious mistake.
header and class declaration:
#include <iostream>
#include <vector>
#include<string>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
class StringSet
{
public:
//Constructor
StringSet();
//Copy Constructor
StringSet(const StringSet& s);
//Default constructors
StringSet(string initialStrings[], const int ARRAYSIZE);
//Destructor
~StringSet();
void add(const string s);
void remove(const string s);
//Returns length
int size()
{
return length;
}
// Overload the << operator so that it outputs the strings
friend ostream& operator <<(ostream& outs, const StringSet& s);
private:
//size of the vector
int length;
// Vector to store strings
vector <string> data;
};
function definitions:
ostream& operator<<(ostream& outs, const StringSet& s)
{
outs << "\n";
for (int i = 0; i < s.length; i++)
{
outs << s.data[i] << " ";
}
outs << "\n";
return outs;
}
//Add a string to the vector
void StringSet::add(const string s)
{
bool c = check(s);
if (c == false)
{
data.push_back(s);
}
else
{
cout << "\"" << s << "\" already exists in the set.";
}
}
// Remove a string from the vector
void StringSet::remove(const string s)
{
bool c = check(s);
if (c == true)
{
vector<string>::iterator position = search(s);
data.erase(position);
}
else
{
cout << "\"" << s << "\" does not exist in the set\n";
}
}
StringSet::StringSet()
{
length = 0;
}
StringSet::StringSet(string initialStrings[], const int ARRAYSIZE)
{
for (int i = 0; i < data.size(); i++)
{
initialStrings[i] = " ";
}
}
// Copy constructor
StringSet::StringSet(const StringSet& s)
{
for (int i = 0; i < data.size(); i++)
{
data[i] = s.data[i];
}
}
StringSet::StringSet()
{
length = 0;
}
StringSet::StringSet(string initialStrings[], const int ARRAYSIZE)
{
for (int i = 0; i < data.size(); i++)
{
initialStrings[i] = " ";
}
}
// Copy constructor
StringSet::StringSet(const StringSet& s)
{
for (int i = 0; i < data.size(); i++)
{
data[i] = s.data[i];
}
}
// Check if a string exists in the vector
bool StringSet::check(const string s)
{
vector<string>::iterator it = find(data.begin(), data.end(), s);
if (it != data.end())
{
return true;
}
else
{
return false;
}
}
Main function:
int main()
{
ofstream outs;
ifstream ins;
StringSet doc1, doc2, query
cout << "Welcome to stringset\n";
doc1.add("hi");
doc1.add("everyone");
outs << doc1;
doc1.remove("everyone");
doc1.remove("all");
outs << doc1;
}
If you use a variable that stores the size of the set, you should increment/decrement it when adding/removing elements. You can also change the definition of the StringSet::size():
int size() const
{
return static_cast<int>(data.size());
}

I have a problem with reading & printing my class

I have a problem with printing my class. I want this class to read a binary number and then print it. I am a Beginner, so here can be a really dumb mistake.
This code has a wrong output, but a correct input.
I tried to fix this, but I couldn't. I hope you will find the mistake.
Please help. Thanks!
Here is the code:
#include <iostream>
using namespace std;
class Binary
{
int len;
bool* arr;
public:
Binary();
Binary(const Binary&);
friend istream& operator >> (istream&, Binary&);
friend ostream& operator << (ostream&, const Binary&);
};
Binary::Binary()
{
len = 0;
arr = new bool[0];
}
Binary::Binary(const Binary& b)
{
len = b.len;
arr = new bool[len];
for (int i = 0; i < len; i++) {
arr[i] = b.arr[i];
}
}
istream& operator>>(istream& in, Binary& b)
{
char line[101];
in.getline(line, 100);
b.len = strlen(line);
b.arr = new bool[b.len];
for (int i = 0; i < b.len; i++) {
b.arr[i] = (line[i] == '0' ? 0 : 1);
}
return in;
}
ostream& operator<<(ostream& out, const Binary& b)
{
for (int i = 0; i < b.len; i++) {
out << b.arr;
}
return out;
}
int main() {
Binary a;
cin >> a;
cout << a;
return 0;
}
The problem is with this line of code:
out << b.arr;
You are printing the array pointer, b.arr, instead of a value in the array.
This will work:
out << b.arr[i] ? '1' : '0';
You should also consider writing a destructor to free your previously allocated memory, and also free the previous array before overwriting it's pointer on this line:
b.arr = new bool[b.len];

Compilation error in C++ when using an assignment operator =?

I have written a program which was given to me as a homework assignment (it's a bit longer). The issue is that it compiles in CodeBlocks but it does not compile in Visual Studio 2017 it says - binary '=': no operator found which takes a right-hand operand of type 'CAutomobile' (or there is no acceptable conversion.
I would like to ask why is that because I could not myself find the error? I tried commenting the operator =function but still the error remained.
#include <iostream>
#include <algorithm>
#include <string>
#include <stdlib.h>
using namespace std;
class CVehicle {
string name;
int year;
public:
CVehicle() {
name = "Car";
year = 1990;
}
CVehicle(string n, int y) {
name = n;
year = y;
}
CVehicle(const CVehicle& vc) {
name = vc.name;
year = vc.year;
}
void setName(string n) {
name = n;
}
void setYear(int y) {
year = y;
}
string getName() {
return name;
}
int& getYear() {
return year;
}
virtual void Print(ostream& os) = 0;
};
class CAutomobile :public CVehicle {
double litres;
public:
CAutomobile() :CVehicle() {
litres = 7.2;
}
CAutomobile(string nm, int yr, double l) :CVehicle(nm, yr) {
litres = l;
}
void setLitres(double l) {
l = litres;
}
double& getLitres() {
return litres;
}
void Print(ostream& os) override {
os << getName() << endl;
os << getYear() << endl;
os << litres << endl;
}
friend bool operator< (CAutomobile a1, CAutomobile a2) {
if (a1.litres < a2.litres) {
return true;
}
return false;
}
CAutomobile operator= (CAutomobile& at) {
CAutomobile au;
au.getName() = at.getName();
au.getYear() = at.getYear();
au.getLitres() = at.getLitres();
return au;
}
CAutomobile operator+(CAutomobile aut) {
CAutomobile a;
a.getLitres() = getLitres() + aut.getLitres();
return a;
}
friend ostream& operator<< (ostream& o, CAutomobile a) {
o << a.getName() << endl;
o << a.getYear() << endl;
o << a.getLitres() << endl;
return o;
}
};
int main()
{
CAutomobile a[] = {
CAutomobile(),
CAutomobile("Wolkswagen",1970,80.5),
CAutomobile("Fiat",1979,21.9),
CAutomobile("Opel",1978,13.7)
};
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
cout << "Name" << ' ' << a[i].getName() << endl;
cout << "Year" << ' ' << a[i].getYear() << endl;
cout << "Litres" << ' ' << a[i].getLitres() << endl;
}
int range = 2016 - 1990 + 1;
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
a[i].setLitres(rand() % 100 + 1);
a[i].setYear(rand() % range + 1996);
}
//сортираме масива по литри и извеждаме
//най малкия (първия) му елемент
for (int i = 0; i < sizeof(a-1); i++) {
for (int j = 0; j < sizeof(a-1); j++) {
if (a[j].getLitres() > a[j + 1].getLitres()) {
swap(a[j], a[j + 1]);
}
}
}
cout << a[0] << endl;
CAutomobile k = a[0] + a[3];
cout << k.getLitres() << endl;
}
CAutomobile::operator = is completely wrong. It takes a non-const reference and assignes its field to a new object. Instead it should take a const reference and modify current object.
CAutomobile & operator =(CAutomobile const & other)
{
assert(this != ::std::addressof(other)); // check for self-assignment
SetName(other.getName());
SetYear(other.getYear());
SetLitres(other.getLitres());
return *this;
}
This will bring up another problem: getters are not const-qualified, so they should be fixes as well:
string const & getName(void) const {
return name;
}
int const & getYear(void) const {
return year;
}

C++ Debugging console app

I'm working on a debugging console app for school, and I get an error when compiling the program.
//DEBUG9-4
//This program creates Student objects
//and overloads < to compare student year in school
#include<iostream>
#include<string>
using namespace std;
class Student
{
private: //MISSING :
int stuID;
int year;
double gpa;
public:
Student(const int i, const int y, const double g); // NO VARIABLES DECLARED HERE
void showYear(); //MISSING THE CURLY BRACES
bool operator < (const Student);
};
Student::Student(const int i, const int y, const double g) // VARIABLES WERE NOT MATCHING
{
stuID = i;
year = i;
gpa = g; // VARIABLE g WAS POST TO BE THE "gpa" VARIABLE
}
void Student::showYear()
{
cout << year;
}
int Student::operator < (const Student otherStu)
{
bool less = false;
if (year < otherStu.year)
less = true;
return less;
}
int main()
{
Student a(111, 2, 3.50), b(222, 1, 3.00);
if(a < b)
{
a.showYear();
cout << " is less than ";
b.showYear();
}
else
{
a.showYear();
cout << " is not less than ";
b.showYear();
}
cout << endl;
return 0;
}
Line: 28 Error: prototype for 'int Student::operator < (const Student& otherStu)' dose not match any in class Students.
Line: 16 Error: candidate is: bool Student::operator<(Student).
You wanted to implement it like this:
bool Student::operator < (const Student otherStu)
{
bool less = false;
if (year < otherStu.year)
less = true;
return less;
}
Because now your definition and declaration doesnt match, thay have different return value.
or you can doi just:
bool Student::operator < (const Student b)
{
return year < b.year ? true : false;
}

C++ HW - defining classes - objects that have objects of other class problem in header file (out of scope?), (also redefining operators, read in out)

This is my first time with much of this code. With this instancepool.h file below I get errors saying I can't use vector (line 14) or have instance& as a return type (line 20). It seems it can't use the instance objects despite the fact that I have included them.
#ifndef _INSTANCEPOOL_H
#define _INSTANCEPOOL_H
#include "instance.h"
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
class InstancePool
{
private:
unsigned instances;//total number of instance objects
vector<instance> ipp;//the collection of instance objects, held in a vector
public:
InstancePool();//Default constructor. Creates an InstancePool object that contains no Instance objects
InstancePool(const InstancePool& original);//Copy constructor. After copying, changes to original should not affect the copy that was created.
~InstancePool();//Destructor
unsigned getNumberOfInstances() const;//Returns the number of Instance objects the the InstancePool contains.
const instance& operator[](unsigned index) const;
InstancePool& operator=(const InstancePool& right);//Overloading the assignment operator for InstancePool.
friend istream& operator>>(istream& in, InstancePool& ip);//Overloading of the >> operator.
friend ostream& operator<<(ostream& out, const InstancePool& ip);//Overloading of the << operator.
};
#endif
Here is the instance.h :
#ifndef _INSTANCE_H
#define _INSTANCE_H
///////////////////////////////#include "instancepool.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Instance
{
private:
string filenamee;
bool categoryy;
unsigned featuress;
unsigned* featureIDD;
unsigned* frequencyy;
string* featuree;
public:
Instance (unsigned features = 0);//default constructor
unsigned getNumberOfFeatures() const; //Returns the number of the keywords that the calling Instance object can store.
Instance(const Instance& original);//Copy constructor. After copying, changes to the original should not affect the copy that was created.
~Instance() { delete []featureIDD; delete []frequencyy; delete []featuree;}//Destructor.
void setCategory(bool category){categoryy = category;}//Sets the category of the message. Spam messages are represented with true and and legit messages with false.//easy
bool getCategory() const;//Returns the category of the message.
void setFileName(const string& filename){filenamee = filename;}//Stores the name of the file (i.e. “spam/spamsga1.txt”, like in 1st assignment) in which the message was initially stored.//const string& trick?
string getFileName() const;//Returns the name of the file in which the message was initially stored.
void setFeature(unsigned i, const string& feature, unsigned featureID,unsigned frequency) {//i for array positions
featuree[i] = feature;
featureIDD[i] = featureID;
frequencyy[i] = frequency;
}
string getFeature(unsigned i) const;//Returns the keyword which is located in the ith position.//const string
unsigned getFeatureID(unsigned i) const;//Returns the code of the keyword which is located in the ith position.
unsigned getFrequency(unsigned i) const;//Returns the frequency
Instance& operator=(const Instance& right);//Overloading of the assignment operator for Instance.
friend ostream& operator<<(ostream& out, const Instance& inst);//Overloading of the << operator for Instance.
friend istream& operator>>(istream& in, Instance& inst);//Overloading of the >> operator for Instance.
};
#endif
Also, if it is helpful here is instance.cpp:
// Here we implement the functions of the class apart from the inline ones
#include "instance.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
Instance::Instance(unsigned features) { //Constructor that can be used as the default constructor.
featuress = features;
if (features == 0)
return;
featuree = new string[featuress]; // Dynamic memory allocation.
featureIDD = new unsigned[featuress];
frequencyy = new unsigned[featuress];
return;
}
unsigned Instance::getNumberOfFeatures() const {//Returns the number of the keywords that the calling Instance object can store.
return featuress;}
Instance::Instance(const Instance& original) {//Copy constructor.
filenamee = original.filenamee;
categoryy = original.categoryy;
featuress = original.featuress;
featuree = new string[featuress];
for(unsigned i = 0; i < featuress; i++) {
featuree[i] = original.featuree[i];
}
featureIDD = new unsigned[featuress];
for(unsigned i = 0; i < featuress; i++) {
featureIDD[i] = original.featureIDD[i];
}
frequencyy = new unsigned[featuress];
for(unsigned i = 0; i < featuress; i++) {
frequencyy[i] = original.frequencyy[i];}
}
bool Instance::getCategory() const { //Returns the category of the message.
return categoryy;}
string Instance::getFileName() const { //Returns the name of the file in which the message was initially stored.
return filenamee;}
string Instance::getFeature(unsigned i) const { //Returns the keyword which is located in the ith position.//const string
return featuree[i];}
unsigned Instance::getFeatureID(unsigned i) const { //Returns the code of the keyword which is located in the ith position.
return featureIDD[i];}
unsigned Instance::getFrequency(unsigned i) const { //Returns the frequency
return frequencyy[i];}
Instance& Instance::operator=(const Instance& right) { //Overloading of the assignment operator for Instance.
if(this == &right) return *this;
delete []featureIDD;
delete []frequencyy;
delete []featuree;
filenamee = right.filenamee;
categoryy = right.categoryy;
featuress = right.featuress;
featureIDD = new unsigned[featuress];
frequencyy = new unsigned[featuress];
featuree = new string[featuress];
for(unsigned i = 0; i < featuress; i++) {
featureIDD[i] = right.featureIDD[i]; }
for(unsigned i = 0; i < featuress; i++) {
frequencyy[i] = right.frequencyy[i]; }
for(unsigned i = 0; i < featuress; i++) {
featuree[i] = right.featuree[i]; }
return *this;
}
ostream& operator<<(ostream& out, const Instance& inst) {//Overloading of the << operator for Instance.
out << endl << "<message file=" << '"' << inst.filenamee << '"' << " category=";
if (inst.categoryy == 0)
out << '"' << "legit" << '"';
else
out << '"' << "spam" << '"';
out << " features=" << '"' << inst.featuress << '"' << ">" <<endl;
for (int i = 0; i < inst.featuress; i++) {
out << "<feature id=" << '"' << inst.featureIDD[i] << '"' << " freq=" << '"' << inst.frequencyy[i] << '"' << "> " << inst.featuree[i] << " </feature>"<< endl;
}
out << "</message>" << endl;
return out;
}
istream& operator>>(istream& in, Instance& inst) { //Overloading of the >> operator for Instance.
string word;
string numbers = "";
string filenamee2 = "";
bool categoryy2 = 0;
unsigned featuress2;
string featuree2;
unsigned featureIDD2;
unsigned frequencyy2;
unsigned i;
unsigned y;
while(in >> word) {
if (word == "<message") {//if at beginning of message
in >> word;//grab filename word
for (y=6; word[y]!='"'; y++) {//pull out filename from between quotes
filenamee2 += word[y];}
in >> word;//grab category word
if (word[10] == 's')
categoryy2 = 1;
in >> word;//grab features word
for (y=10; word[y]!='"'; y++) {
numbers += word[y];}
featuress2 = atoi(numbers.c_str());//convert string of numbers to integer
Instance tempp2(featuress2);//make a temporary Instance object to hold values read in
tempp2.setFileName(filenamee2);//set temp object to filename read in
tempp2.setCategory(categoryy2);
for (i=0; i<featuress2; i++) {//loop reading in feature reports for message
in >> word >> word >> word;//skip two words
numbers = "";//reset numbers string
for (int y=4; word[y]!='"'; y++) {//grab feature ID
numbers += word[y];}
featureIDD2 = atoi(numbers.c_str());
in >> word;//
numbers = "";
for (int y=6; word[y]!='"'; y++) {//grab frequency
numbers += word[y];}
frequencyy2 = atoi(numbers.c_str());
in >> word;//grab actual feature string
featuree2 = word;
tempp2.setFeature(i, featuree2, featureIDD2, frequencyy2);
}//all done reading in and setting features
in >> word;//read in last part of message : </message>
inst = tempp2;//set inst (reference) to tempp2 (tempp2 will be destroyed at end of function call)
return in;
}
}
}
and instancepool.cpp:
// Here we implement the functions of the class apart from the inline ones
#include "instancepool.h"
#include "instance.h"
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
InstancePool::InstancePool()//Default constructor. Creates an InstancePool object that contains no Instance objects
{
instances = 0;
ipp.clear();
}
InstancePool::~InstancePool() {
ipp.clear();}
InstancePool::InstancePool(const InstancePool& original) {//Copy constructor.
instances = original.instances;
for (int i = 0; i<instances; i++) {
ipp.push_back(original.ipp[i]);
}
}
unsigned InstancePool::getNumberOfInstances() const {//Returns the number of Instance objects the the InstancePool contains.
return instances;}
const Instance& InstancePool::operator[](unsigned index) const {//Overloading of the [] operator for InstancePool.
return ipp[index];}
InstancePool& InstancePool::operator=(const InstancePool& right) {//Overloading the assignment operator for InstancePool.
if(this == &right) return *this;
ipp.clear();
instances = right.instances;
for(unsigned i = 0; i < instances; i++) {
ipp.push_back(right.ipp[i]); }
return *this;
}
istream& operator>>(istream& in, InstancePool& ip) {//Overloading of the >> operator.
ip.ipp.clear();
string word;
string numbers;
int total;//int to hold total number of messages in collection
while(in >> word) {
if (word == "<messagecollection"){
in >> word;//reads in total number of all messages
for (int y=10; word[y]!='"'; y++){
numbers = "";
numbers += word[y];
}
total = atoi(numbers.c_str());
for (int x = 0; x<total; x++) {//do loop for each message in collection
in >> ip.ipp[x];//use instance friend function and [] operator to fill in values and create Instance objects and read them intot he vector
}
}
}
}
ostream& operator<<(ostream& out, const InstancePool& ip) {//Overloading of the << operator.
out << "<messagecollection messages=" << '"' << '>' << ip.instances << '"'<< endl << endl;
for (int z=0; z<ip.instances; z++) {
out << ip[z];}
out << endl<<"</messagecollection>\n";
}
This code is currently not writing to files correctly either at least, I'm sure it has many problems. I hope my posting of so much is not too much, and any help would be very much appreciated. Thanks!
You created an Instance type but are trying to use instance. Case matters.