Jump to content
Sign in to follow this  
CoolMaster

Classes?

Recommended Posts

wait with this would it say vinylRecord is undefined?

since...it's never like declared?

Yes, it certainly will. That's why I said you'll need to fix it so it doesn't refer to the vinylRecord struct anymore. All of the variables are contained within the class, so there's no need for the functions to return anything anymore. They should all return void now.

Share this post


Link to post
Share on other sites

Ah so i got it working,......i think, i mean it works, but did I do it the right way, now I jsut gotta add the 2 other parts

header file

using namespace std;

#ifndef VINYLRECORD_H	//"File Guard" compiler directive
#define VINYLRECORD_H
class vinylRecord{
private:
int ID,purmonth,purday,puryear,relmonth,relday,relyear,rating; 
float price,saleprice;
string name,album,talk;
public:
void printRecord(vinylRecord R);
vinylRecord readRecord();
void ModifyPrice(vinylRecord &R,float percent);
int compareRecords(vinylRecord temp1,vinylRecord temp2,int num);
};



#endif VINYLRECORD_H

 

 

cpp function file

#include <iostream>
#include <string>
#include "vinylRecord.h"
using namespace std;


vinylRecord vinylRecord::readRecord()
{
vinylRecord temprec;
cout << '\n';
cout << "--------------------Entering Data--------------------";
cout << '\n';
cout << "Please Enter a Nine-Digit ID for This Record: ";
cin >> temprec.ID;
cout << "Please Enter the Purchase Date for This Record (MM DD YYYY): ";
cin >> temprec.purmonth >> temprec.purday >> temprec.puryear;
cout << "Please Enter the Purchase Price for This Record: ";
cin >> temprec.price;
cout << "Please Enter the Band/Artist Name: ";
cin.ignore(500, '\n');
getline  (cin,temprec.name);
cout << "Please Enter the Album Name: ";

getline  (cin,temprec.album);
cout << "Please Enter the Album Release Date (MM YYYY): ";
cin >> temprec.relmonth >> temprec.relyear;
cout << "Please Enter a Short Description of the Album Cover: ";
cin.ignore(500, '\n');
getline  (cin,temprec.talk);
cout << '\n' << "Please Select one of the Following Condition Ratings for This Record: " << '\n';
cout << "		0-unacceptable  1-poor  2-average	   3-good " << '\n';
cout << "		4-very good	 5-mint  6-unopened" << '\n';
cin >> temprec.rating;
cout << "Please Enter the Sale Price for This Record: ";
cin >> temprec.saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';
cout << '\n';

return temprec;
}


void vinylRecord::printRecord(vinylRecord R)
{
cout << "--------------------Printing Data--------------------" << '\n';

cout << "ID: " <<R.ID;
cout << '\n';
cout << "Purchase Date: " <<R.purmonth<< " " << R.purday<< " " << R.puryear;
cout << '\n';
cout << "Purchase Price: " <<R.price;
cout << '\n';
cout << "Band/Artist: "<<R.name;
cout << '\n';
cout << "Album: "<<R.album;
cout << '\n';
cout << "Release Date: "<<R.relmonth<< " " << R.relyear;
cout << '\n';
cout << "Description of the Album Cover: " <<R.talk;
cout << '\n';
cout << "Condition Rating: ";
if(R.rating == 0)
{
cout << "unacceptable ";
}
if(R.rating == 1)
{
cout << "poor ";
}
if(R.rating == 2)
{
cout << "average ";
}
if(R.rating == 3)
{
cout << "good ";
}
if(R.rating == 4)
{
cout << "very good ";
}
if(R.rating == 5)
{
cout << "mint ";
}
if(R.rating == 6)
{
cout << "unopened ";
}



cout << '\n';
cout << "Sale Price: "<<R.saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';


}



void vinylRecord::ModifyPrice(vinylRecord &R, float percent)

{
R.saleprice = R.saleprice + (percent/100)*R.saleprice;


}

int vinylRecord::compareRecords(vinylRecord temp1,vinylRecord temp2,int num)

{


 if(num==1)
{
	if(temp1.puryear<temp2.puryear)
	{
	return -1;
	}

	if(temp1.puryear>temp2.puryear)
	{
	return 1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth<temp2.purmonth)
	{
	return -1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth>temp2.purmonth)
	{
	return 1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth==temp2.purmonth && temp1.purday < temp2.purday)
	{
	return -1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth==temp2.purmonth && temp1.purday > temp2.purday)
	{
	return 1;
	}

	if(temp1.puryear==temp2.price && temp1.purmonth==temp2.purmonth && temp1.purday==temp2.purday)
	{
	return 0;
	}


}


if(num==2)
{
	if(temp1.price>temp2.price)
	{
		return -1;
	}
	if(temp1.price==temp2.price)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

 if(num==4)
{
	if(temp1.saleprice>temp2.saleprice)
	{
		return 1;
	}
	if(temp1.saleprice==temp2.saleprice)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}





  if(num==3)
{
	if(temp1.rating>temp2.rating)
	{
		return 1;
	}
	if(temp1.rating==temp2.rating)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}


cout << '\n' << '\n';

}

 

 

main file

/*Matt Smith
Lab 1
Organizing Vinyl Records and Comparing
Section 2
9-11-2007
*/


#include <iostream>
#include <string>
#include "vinylRecord.h"
using namespace std;






int main()
{
vinylRecord classes;
vinylRecord temp1, temp2;

temp1 = classes.readRecord();
cout<<endl<<endl;
temp2 = classes.readRecord();

classes.printRecord(temp1);
cout<<endl<<endl;
classes.printRecord(temp2);

cout<<"\nCompare by purchase date: "<<classes.compareRecords(temp1, temp2, 1);
cout<<"\nCompare by purchase price: "<<classes.compareRecords(temp1, temp2, 2);
cout<<"\nCompare by condition: "<<classes.compareRecords(temp1, temp2, 3);
cout<<"\nCompare by sale price: "<<classes.compareRecords(temp1, temp2, 4);

cout<<"\nReducing price of record 1 by 50%";
classes.ModifyPrice(temp1, -50);
cout<<"\nIncreasing price of record 2 by 50%" << '\n' << '\n';
classes.ModifyPrice(temp2, 50);

classes.printRecord(temp1);
cout<<endl<<endl;
classes.printRecord(temp2);

return 0;
}

 

 

does everything look....correct?

Share this post


Link to post
Share on other sites
does everything look....correct?

no... let's go step by step through what you have...

 

-----------------------------

 

Here's a piece from your driver program:

vinylRecord classes;
vinylRecord temp1, temp2;

temp1 = classes.readRecord();
cout<<endl<<endl;
temp2 = classes.readRecord();

classes.printRecord(temp1);
cout<<endl<<endl;
classes.printRecord(temp2);

To put it bluntly, this code tells me that you don't understand what a class does. I don't mean that to be rude, I'm just being straight. You're calling a method from one class object in order to write data into another class object of the same type. That doesn't make any sense. This could technically work I think, but you're going about it all wrong, and you're making your life a lot harder than it needs to be. I'll try to explain why later, but here's what this same code block should look like:

vinylRecord temp1, temp2;

temp1.readRecord();
cout<<endl<<endl;
temp2.readRecord();

temp1.printRecord();
cout<<endl<<endl;
temp2.printRecord();

All of your methods in your class should return "void", NOT "vinylRecord". Hopefully you can see why here.

 

 

Let's take a look at this function declaration line (called a "prototype", technically):

vinylRecord vinylRecord::readRecord()

Why does this return a record?

 

What this method SHOULD do is return nothing, but instead take all the data and write it to the variables that are contained within the class object. If you declare a vinylRecord object called "temp1", then you should be able to populate the variables by doing "temp1.readRecord()". The readRecord function is included in the temp1 object, so the object is capable of populating itself.

 

 

cin >> temprec.ID;
cin >> temprec.purmonth >> temprec.purday >> temprec.puryear;
cin >> temprec.price;

You don't have a temprec object anymore, so how can this work? :P

 

 

void vinylRecord::printRecord(vinylRecord R)

Same as before, you don't need to pass the R variable into this method. The method is included in the object. So it can be called like this: "temp1.printRecord();".

 

Take a look over this stuff, then look at your code again. You've got a lot of code there, but it doesn't seem like you really understand what it's doing. Again, I don't mean to be rude, but I know from experience that if you don't get a good grip on this stuff up front, the rest of the class is just going to get worse and worse.

Share this post


Link to post
Share on other sites

and what about the compare records? if Im comparing two records what would I do with those?

 

cout<<"\nCompare by purchase date: "<<classes.compareRecords(temp1, temp2, 1);
cout<<"\nCompare by purchase price: "<<classes.compareRecords(temp1, temp2, 2);
cout<<"\nCompare by condition: "<<classes.compareRecords(temp1, temp2, 3);
cout<<"\nCompare by sale price: "<<classes.compareRecords(temp1, temp2, 4);

Share this post


Link to post
Share on other sites
but I have to return something in that readrecord dont i? so how would I do that?

No. readRecord doesn't need to return anything. The whole point of classes is to put the functions and the variables together in the same object. So the readRecord function is a part of the same object that it's trying to put data in. It's putting data into itself. You just read the data in from the cin prompt and write it right to the variable it goes to.

Share this post


Link to post
Share on other sites

Ok but what do I store all this stuff to in the readrecord ? i know i have that "temprec" but....since that wont work what the heck do i store it to, and what to i use to printout?

 

 

and how am I supposed to do the compare records? i mean im comparing two different ones? so i cant say temp1.comparerecords?

 

 

and for the reading in function im passiong (ifstream and &infile)

when I call...the function what do I put as the parameters, since it will not take it with 0 parameters

Edited by CoolMaster

Share this post


Link to post
Share on other sites
Ok but what do I store all this stuff to in the readrecord ? i know i have that "temprec" but....since that wont work what the heck do i store it to, and what to i use to printout?

The values are written directly to the variables, since they're in the class object. They don't need to be returned at all. Like this:

vinylRecord vinylRecord::readRecord()
{
cout << '\n';
cout << "--------------------Entering Data--------------------";
cout << '\n';
cout << "Please Enter a Nine-Digit ID for This Record: ";
cin >> ID;
cout << "Please Enter the Purchase Date for This Record (MM DD YYYY): ";
cin >> purmonth >> purday >> puryear;
cout << "Please Enter the Purchase Price for This Record: ";
cin >> price;
cout << "Please Enter the Band/Artist Name: ";
cin.ignore(500, '\n');
getline  (cin,name);
cout << "Please Enter the Album Name: ";

getline  (cin,album);
cout << "Please Enter the Album Release Date (MM YYYY): ";
cin >> relmonth >> relyear;
cout << "Please Enter a Short Description of the Album Cover: ";
cin.ignore(500, '\n');
getline  (cin,talk);
cout << '\n' << "Please Select one of the Following Condition Ratings for This Record: " << '\n';
cout << "		0-unacceptable  1-poor  2-average	   3-good " << '\n';
cout << "		4-very good	 5-mint  6-unopened" << '\n';
cin >> rating;
cout << "Please Enter the Sale Price for This Record: ";
cin >> saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';
cout << '\n';

}

 

and how am I supposed to do the compare records? i mean im comparing two different ones? so i cant say temp1.comparerecords?

Comparing records is going to be harder, and I suggest you get everything else working before you do that. But basically, you would pass one object into the other object.

 

when I call...the function what do I put as the parameters, since it will not take it with 0 parameters

You can make functions with no parameters. I've done this hundreds of times in C++. If it's not working in your code, something else is wrong.

Share this post


Link to post
Share on other sites
well it's saying since i made the prototype (ifstream &infile) when i try to call it in the main, as 0 parameters it wont let me

OK, well I guess you need to post your most recent code then, because I'm not seeing it in what you posted earlier. Put up your newest stuff and I'll take a look.

Share this post


Link to post
Share on other sites

Ok so I did what u had me do. i think,

here's what i have

main

/*Matt Smith
Lab 1
Organizing Vinyl Records and Comparing
Section 2
9-11-2007
*/


#include <iostream>
#include <fstream>
#include <string>
#include "vinylRecord.h"
using namespace std;






int main()
{

vinylRecord temp1, temp2;

temp1.readRecord();
cout<<endl<<endl;
temp2.readRecord();

temp1.printRecord();
cout<<endl<<endl;
temp2.printRecord();

cout<<"\nCompare by purchase date: "<<classes.compareRecords(temp1, temp2, 1);
cout<<"\nCompare by purchase price: "<<classes.compareRecords(temp1, temp2, 2);
cout<<"\nCompare by condition: "<<classes.compareRecords(temp1, temp2, 3);
cout<<"\nCompare by sale price: "<<classes.compareRecords(temp1, temp2, 4);

cout<<"\nReducing price of record 1 by 50%";
temp1.ModifyPrice(-50);
cout<<"\nIncreasing price of record 2 by 50%" << '\n' << '\n';
temp2.ModifyPrice(50);

temp1.printRecord();
cout<<endl<<endl;
temp2.printRecord();

return 0;
}

cpp function file

 

#include <iostream>
#include <string>
#include <fstream>
#include "vinylRecord.h"
using namespace std;


void vinylRecord::readRecord()
{

cout << '\n';
cout << "--------------------Entering Data--------------------";
cout << '\n';
cout << "Please Enter a Nine-Digit ID for This Record: ";
cin >> ID;
cout << "Please Enter the Purchase Date for This Record (MM DD YYYY): ";
cin >> purmonth >> purday >> puryear;
cout << "Please Enter the Purchase Price for This Record: ";
cin >> price;
cout << "Please Enter the Band/Artist Name: ";
cin.ignore(500, '\n');
getline  (cin,name);
cout << "Please Enter the Album Name: ";

getline  (cin,album);
cout << "Please Enter the Album Release Date (MM YYYY): ";
cin >> relmonth >> relyear;
cout << "Please Enter a Short Description of the Album Cover: ";
cin.ignore(500, '\n');
getline  (cin,talk);
cout << '\n' << "Please Select one of the Following Condition Ratings for This Record: " << '\n';
cout << "		0-unacceptable  1-poor  2-average	   3-good " << '\n';
cout << "		4-very good	 5-mint  6-unopened" << '\n';
cin >> rating;
cout << "Please Enter the Sale Price for This Record: ";
cin >> saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';
cout << '\n';


}


void vinylRecord::readRecord2( ifstream &infile )
{
.open("myfile.txt");












}


void vinylRecord::printRecord()
{
cout << "--------------------Printing Data--------------------" << '\n';

cout << "ID: " <<ID;
cout << '\n';
cout << "Purchase Date: " <<purmonth<< " " << purday<< " " << puryear;
cout << '\n';
cout << "Purchase Price: " <<price;
cout << '\n';
cout << "Band/Artist: "<<name;
cout << '\n';
cout << "Album: "<<album;
cout << '\n';
cout << "Release Date: "<<relmonth<< " " << relyear;
cout << '\n';
cout << "Description of the Album Cover: " <<talk;
cout << '\n';
cout << "Condition Rating: ";
if(rating == 0)
{
cout << "unacceptable ";
}
if(rating == 1)
{
cout << "poor ";
}
if(rating == 2)
{
cout << "average ";
}
if(rating == 3)
{
cout << "good ";
}
if(rating == 4)
{
cout << "very good ";
}
if(rating == 5)
{
cout << "mint ";
}
if(rating == 6)
{
cout << "unopened ";
}



cout << '\n';
cout << "Sale Price: "<<saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';


}



void vinylRecord::ModifyPrice(float percent)

{
saleprice = saleprice + (percent/100)*saleprice;


}

int vinylRecord::compareRecords(vinylRecord temp1,vinylRecord temp2,int num)

{


 if(num==1)
{
	if(temp1.puryear<temp2.puryear)
	{
	return -1;
	}

	if(temp1.puryear>temp2.puryear)
	{
	return 1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth<temp2.purmonth)
	{
	return -1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth>temp2.purmonth)
	{
	return 1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth==temp2.purmonth && temp1.purday < temp2.purday)
	{
	return -1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth==temp2.purmonth && temp1.purday > temp2.purday)
	{
	return 1;
	}

	if(temp1.puryear==temp2.price && temp1.purmonth==temp2.purmonth && temp1.purday==temp2.purday)
	{
	return 0;
	}


}


if(num==2)
{
	if(temp1.price>temp2.price)
	{
		return -1;
	}
	if(temp1.price==temp2.price)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

 if(num==4)
{
	if(temp1.saleprice>temp2.saleprice)
	{
		return 1;
	}
	if(temp1.saleprice==temp2.saleprice)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}





  if(num==3)
{
	if(temp1.rating>temp2.rating)
	{
		return 1;
	}
	if(temp1.rating==temp2.rating)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}


cout << '\n' << '\n';

}

 

header file

 

using namespace std;

#ifndef VINYLRECORD_H	//"File Guard" compiler directive
#define VINYLRECORD_H
class vinylRecord{
private:
int ID,purmonth,purday,puryear,relmonth,relday,relyear,rating; 
float price,saleprice;
string name,album,talk;
public:
void printRecord();
void readRecord();
void readRecord2(ifstream &infile);
void ModifyPrice(float percent);
int compareRecords(vinylRecord temp1,vinylRecord temp2,int num);
};



#endif VINYLRECORD_H

 

 

 

is that what u meant?

cuase the only errors im getting is with the new readin through a file and the compare.

 

btw do u have aim or msn? or anything verran?

 

 

EDIT: without the compare stuff it's working correctly thx, NOW i just have to add the "default" record

http://www.cs.uky.edu/~ryan/CS215_F07/labs/Lab2.htm as it says here

 

which shouldnt be hard i dont think,

 

fix the compare records, and do the readin from file record

Edited by CoolMaster

Share this post


Link to post
Share on other sites

Well, you definitely need to comment these lines out until you fix your compare statement:

cout<<"\nCompare by purchase date: "<<classes.compareRecords(temp1, temp2, 1);
cout<<"\nCompare by purchase price: "<<classes.compareRecords(temp1, temp2, 2);
cout<<"\nCompare by condition: "<<classes.compareRecords(temp1, temp2, 3);
cout<<"\nCompare by sale price: "<<classes.compareRecords(temp1, temp2, 4);

You don't have a "classes" object declared anymore, so that's got to be throwing errors.

 

For the errors with ifstream...

void vinylRecord::readRecord2( ifstream &infile )
{
.open("myfile.txt");

}

This is your problem. The problem isn't with the 0 parameters on your old read, it's on the new one. What are you trying to do with ".open("myfile.txt");"? Shouldn't there be something in front of the dot? Like, maybe the ifstream object that you asked for in the parameters? :)

 

For the compare, the call should probably look something like this:

temp1.compareRecord(temp2, 2);

You just pass one object into the other, like I said.

 

Also, I actually really dislike IM programs, and I never use them. But even if I did, most of my OCC time is actually when I have free time at work, and I can't really run chat programs here :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×