Jump to content
Sign in to follow this  
CoolMaster

Classes?

Recommended Posts

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

infile >> ID;
infile >> purmonth >> purday >> puryear;
infile >> price;

getline  (infile,name);
getline  (infile,album);
infile >> relmonth >> relyear;
infile.ignore(500, '\n');
getline  (infile,name);
getline  (infile,album);
infile >> relmonth >> relyear;
infile.ignore(500, '\n');
getline  (infile,talk);
infile >> rating;
infile >> saleprice;

}

Why are so many of these lines repeated? Why are you reading the name, album, relmonth, and relyear twice?

Share this post


Link to post
Share on other sites

ya I suck i was like reading stuff in twice, after this ill jsut have the default void function left, which is basically just printed out (it's not compared cause thatd be dumb)

 

and yeah my teachers lame, i mean van halen.......lol j/k ahha

 

 

EDIT: woot got it working, just had to remove some readins and ignores YAY

 

ok onto the next thing

 

ill just need to make a void function like void isuck or whatever

and then...wouldnt I just need to set the id's and everything right there?

 

like id = 1

purchase price = 1

 

etc?

 

or am i way off

 

 

EDIT: crap i forgot about the compare records. oh well ill get to that very last

Edited by CoolMaster

Share this post


Link to post
Share on other sites
and yeah my teachers lame, i mean van halen.......lol j/k ahha

Well, I would agree that Van Halen is lame... but that's not what I was going for :P

 

Giving an example where two different data members have the same value makes the read-in process more ambiguous, and for no good reason. There's plenty of other crappy Van Halen albums out there that have unique names. He should have picked one of them. Or better yet, a DECENT band :P

 

Seriously though, in my next class, I would raise my hand, and when called on I would ask: "Don't you think that choosing a self-titled album makes this example a bit confusing? Why would you intentionally make this example ambiguous like that?"

 

It's stupid, and poor foresight on his part as a teacher.

 

 

EDIT###

What is this void function supposed to do?

Share this post


Link to post
Share on other sites

Basically this void function is like adefault listing?

like look

http://www.cs.uky.edu/~ryan/CS215_F07/labs/Lab2.htm

 

and in the sample listing

 

http://www.cs.uky.edu/~ryan/CS215_F07/labs/L2_output.txt

 

it basically contains exactly this

 

 

ID: 111111111

Purchase Date: 1 1 1900

Purchase Price: $0.0

Band/Artist: Band

Album: Album

Release Date: 1 1900

Description of the Album Cover:

album cover

Condition Rating: unopened

Sale Price: $0.0

 

 

and is printed along with the other stuff, BUT the price is not modified (since it's nothing)

and it's not compared to the others.

 

WHY? i dunno i dont see the point of it lol

 

but I already tried being like ID = etc....doesnt work? any ideas?

Share this post


Link to post
Share on other sites

man well at least all this stuff will make convering my program 1 to classes for program 2, except ill have to change my stuff to functions which aint hard, thank god I dont have till next friday till it's due, but Im def starting on it early lol

Share this post


Link to post
Share on other sites

Riiiiiiiight... again, I'm going to say that you need to spend some time learning about classes, because it seems like you're missing some important info.

 

That is not a void function. Notice how other function prototypes start with "void", but that one doesn't? There's a reason. That's a "constructor". The method is called whenever you make a new object of that type. It sets the initial values of the variables in the object.

 

So all you have to do is set the prototype just like the lab says to, and then set all the values equal to what you listed. It's just like the first read-in function, but this time you don't need to get the values from the user, because you already know them.

Share this post


Link to post
Share on other sites

I just called it void defaultrecord()

actually nm the ID = 1 works....so. when I call it I can just be like temp3? or something?

 

void vinylRecord::defaultRecord()

 

i didnt set any parameters to it, so couldnt I be like temp3.defaultrecord() when I print it?....or something....or am I dumb

 

 

EDIT: ok sorry i didnt see that before I posted, but when i set it, it says "(blah missing type, int assumed etc)?

Edited by CoolMaster

Share this post


Link to post
Share on other sites

AH I see u set the name = to the class name......nice nice, ok. then ill just be like id = etc.

 

 

and when i want to print this stuff I can just be like

 

temp3.printrecord()

 

or.....something to that effect?

Edited by CoolMaster

Share this post


Link to post
Share on other sites

No no no. On this one, you don't get to pick the function name. That's just how it goes.

 

If the class is called vinylRecords, then the constructor has to be named vinylRecords()

 

As far as setting the values, you do it just like you did in your other read functions. It should work exactly the same.

 

EDIT###

Lol, you're posting too fast for me to keep up!

 

It knows it's a constructor because the name is the same. :)

Share this post


Link to post
Share on other sites

ahah sorry

ok

vinylRecord::vinylRecord()
{
ID=111111111;
purmonth=1;
purday=1;
puryear=1900;
price= 0.0;
name= "Band";
album= "Album";
relmonth=1;
relyear=1900;
talk="album cover";
rating=6;
saleprice=0.0;




}

 

no errors sweet, how would I print this stuff out in my original code, can i make another temp like temp3 and be like temp3.printfunction()

or whatever?

or will i have to make a new print function just for this?

 

 

edit: i tried it and it seemed to work?

 

 

edit2 : yup thats worked.....hmm weird how classes work im definetly going to see my professor about this. UNFORTUNATLY theirs still the part of comparing the two actual vinyls, the one I read in the file and the one i typed up, but I have to go work at the best buy till close (aka ill be back here at 11)

so hopefully while Im working ill think up something, but if u have any ideas feel free to throw them out while Im gone, so i can take a looksie when I get back.

 

but thank you so much verran u saved my @$$, and my sanity.....

 

but all this makes me think....should I really be having this much trouble, i mean I love programming, but it's hard for me to do. i mean once I get stuff im usually ok, but some stuff i just have trouble with, i prolly need to hardcore read on functions, im gonna go to the bookstores tomm to look at C++ books they have. but does this mean im destined to suck at programming forever since this "simple" assignment took me so long, i mean I did it but without examples and stuff i would've been beating my head on the table for hours

 

(sigh)

Edited by CoolMaster

Share this post


Link to post
Share on other sites

The smartest way to show that the constructor really works is to do something like this:

vinylRecord temp1;
temp1.printRecord();
temp1.readRecord();
temp1.printRecord();

 

That would show the "before and after" sort of approach.

Share this post


Link to post
Share on other sites

btw this is the error with the comparefunction thingy

 

1>.\functions.cpp(136) : error C2511: 'int vinylRecord::compareRecords(vinylRecord,vinylRecord,int)' : overloaded member function not found in 'vinylRecord'

1> e:\lab1\lab1\vinylRecord.h(5) : see declaration of 'vinylRecord'

 

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';

}

 

and theirs the function

 

here's the main function calls of it

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

 

int compareRecords(vinylRecord temp2,int num);

 

^^ and the prototypes

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  

×