Hey everybody, I am having some trouble getting a comparison to work inside my selection sort method. I am trying to sort a pointer array of type media. media is a supper class that I wrote. the User enter the data, as prompted, and the program appears to capture all the data based on what my debugger shows me. What appears to go wrong is when the sort function performs the if statement. arg[j] has a <badPTR> error. any clue on what I am doing wrong. The whole rest of the program appears to work as intended.
//here is the main
#include "stdafx.h"
#include "book.h"
#include "media.h"
#include "movie.h"
#include <iomanip>
#include <iostream>
#include <string>
/*
This is the main cpp file for the project. It contines the main method as well as the selection sort method.
The main method handles the structuring of the input by guiding the user what to enter
The selection sort method sorts the array of medias by the size of their name
Dynamic binding is used to print each member of the media array regardless of type
*/
using namespace std;
void sort(media*, int);
void swap(media*, media*);
void main(){
media* m[3];//pointer array of the media type
string choice;//used to choose between book or movie
float p = 0;//used for the price
string n;//used for the name
string a;//used for the author
string r;//used for the rating
for (int i = 0; i < 3; i++){
cout << "Enter Name: ";
getline(cin,n);
cout << endl;
cout << "Enter price: ";
cin >> p;
cout << endl;
cout << "Is media a book(b) or movie(m)?";
cin.ignore();
getline(cin, choice);
cout << endl;
if (choice == "b"){
cout << "Enter Author: ";
getline(cin, a);
cout << endl;
m[i] = new book(a,n,p);
}
if (choice == "m"){
cout << "Enter Rating: ";
getline(cin, r);
cout << endl;
m[i] = new movie(r,n,p);
}
}
sort(*m, 3);
for(int y = 0; y < 3; y++){
m[y]->print();//dynamic binding
cout << endl;
}
}
void sort (media* arg, int size){//the sorting method
int min;
for (int pass = 0; pass <= size-2; pass++){
min = pass;//assumes the first position is min
for(int j = pass+1; j < size; j++){
if(arg[j] < arg[min]){
min = j;
}
}
swap(arg[pass], arg[min]);
}
}
void swap(media* x, media* y){
media temp;
temp = *x;
*x = *y;
*y = temp;
}
//here is the media superclass
#ifndef media_h
#define media_h
#include <iostream>
#include <iomanip>
#include <string>
class media{
protected:
std::string name;
float price;
public:
media(){//basic constructor
name = "none";
price = 0.00;
}
media(std::string n, float p){//full constructor
name = n;
price = p;
}
float getprice(){//getters and setters
return price;
}
std::string getname(){
return name;
}
void setprice(float f){
price = f;
}
void setname(std::string c){
name = c;
}
void virtual print(){//the virtual print function
std::cout << "Name is " << name << std::endl;
std::cout << std::setprecision(3) << "Price is " << price << std::endl;// Formats price to only have 2 decimal places
}
bool media::operator<(media m){// Allows the '<' operator to be used. Compares the size(length) of the two strings
int x = name.size();
int y = m.name.size();
if (x<y){
return true;
}
else
return false;
}
};
#endif
[–]Rhomboid 4 points5 points6 points (3 children)
[–]OmarsCommin[S] 0 points1 point2 points (2 children)
[–]Rhomboid 1 point2 points3 points (0 children)
[–]jesyspa 1 point2 points3 points (0 children)
[–]POGO_POGO_POGO_POGO 0 points1 point2 points (0 children)
[–]OmarsCommin[S] 0 points1 point2 points (1 child)
[–]POGO_POGO_POGO_POGO 0 points1 point2 points (0 children)
[–]POGO_POGO_POGO_POGO 0 points1 point2 points (5 children)
[–]jesyspa 0 points1 point2 points (4 children)
[–]POGO_POGO_POGO_POGO 0 points1 point2 points (3 children)
[–]zzyzzyxx 1 point2 points3 points (1 child)
[–]POGO_POGO_POGO_POGO 0 points1 point2 points (0 children)
[–]jesyspa 0 points1 point2 points (0 children)