Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

11-01-2016 , 08:08 AM
I got a for loop in class a) that sends a stringbuilder sb object to class b)

I could do it in the follow ways :

a)

Code:
Class_b b = new Class_b();

for
{
does something with a StringBuilder sb
b.method1(sb);
b.method2(sb);
... for a total of 20 times
}
b)

Code:
Class_b b = new Class_b();

for
{
does something with a StringBuilder sb
b.helpermethod(sb);

}
and in b.helpermethod(sb) I do the 20 calls.

or

c)

Code:
for
{
does something with a StringBuilder sb
new Class_b(sb);
}
and I do the calls in the constructor of b

It's pretty much only a decision between b and c as having 20 methodcalls in a for loop would be really ugly to read. I'm just not sure if c) will give me any problems.
Programming homework and newbie help thread Quote
11-01-2016 , 11:17 AM
I think the first thing you need to do is figure out why the hell you have 20 method calls, and if there's not some way you can drastically refactor that.

If you HAVE to have the 20 calls, then I pass stringbuilder to the class. (usually. In some instances this might violate an encapsulation principle. Kinda depends on the rest of your code)
Programming homework and newbie help thread Quote
11-05-2016 , 02:30 PM
Learning about operator overloading for the first time and having an issue.

I have a function to overload the "+" operator for my class Fraction that looks like this:
Code:
Fraction Fraction::operator+(const Fraction& fract) const
{
	Fraction tempFract;
	
	tempFract.numerator = numerator + fract.numerator;
	tempFract.denominator = denominator + fract.denominator;

	return tempFract;
}
As part of my assignment I'm supposed to create a driver program, but at this point I'm testing things out to make sure they work, so to test this I have:
Code:
using namespace std;

int main()
{
	Fraction z(1, 3);
	Fraction y(2, 5);
	Fraction x;

	x = y + z;
	cout << x;
}
I successfully overloaded the extraction operator, so when I do something like Fraction z(1, 3), cout << z, that works fine. But when I try to do what I have above, I get an error saying "no operator '+' matches these operands, operand types are Fraction and Fraction." What I've done is almost a direct copy out of my text book for basic overloads so I don't get where I've gone wrong here.
Programming homework and newbie help thread Quote
11-05-2016 , 03:35 PM
As a quick follow up, as I've continued to write out code for my homework, I keep getting the same error message for every operator I've written, with the exception of the '<<' operator. So I'm going to post two more examples, one of my overload for the '<<' operator and another one for a boolean operator that also doesn't work.

Code:
ostream& operator<<(ostream& osObject, const Fraction& fract)
{
	int numer = fract.numerator;
	int denom = fract.denominator;
	int wholePart = numer / denom;
	bool greaterThanOne = wholePart > 1;

	switch (wholePart > 1)
	{
	case true:
		cout << wholePart << " ";
		numer = numer - (wholePart * denom);
    case false:
		cout << numer << "/" << denom << endl;
		break;
	}

	return osObject;
}
And another failure
Code:
bool Fraction::operator==(const Fraction& fract) const
{
	return (numerator == fract.numerator && denominator == fract.denominator);
}
And in case it helps, the overall assignment is to define a class called 'Fraction' that has a member variable for both numerator and denominator. I have to overload the arithmetic and boolean operators to be able to add, compare etc. Fraction objects. When they need to be displayed, they should be reduced to lowest terms and if greater than one be displayed like '2 3/4'.

To be clear, I've also overloaded >> and that doesn't work either. Only << works.

Last edited by LBloom; 11-05-2016 at 03:41 PM.
Programming homework and newbie help thread Quote
11-05-2016 , 03:56 PM
Send me the complete code and I'll take a look. Off hand what you have looks right.

(send to me@rustybrooks.com)
Programming homework and newbie help thread Quote
11-05-2016 , 04:27 PM
Thank you, I appreciate that. I will do so as soon as I'm able to. The assignment isn't due for ten days so I'm in no real rush; whenever you can get to it will be great.
Programming homework and newbie help thread Quote
11-06-2016 , 07:13 PM
Don't mean to spam but figured this thread would probably have a good chance of having people in it who could help me out;
http://forumserver.twoplustwo.com/17...coach-1637758/

Its sort of a homework assigned but somewhat 'large' and with a rather short deadline. Thanks a bunch.
Programming homework and newbie help thread Quote
11-12-2016 , 05:09 PM
I'm overloading the '>>' operator in C++ and it doesn't seem to be working correctly. I thought this part of my assignment was pretty straightforward but I guess not.

What I have to do is read data from an input file, create Fraction objects from that data, do some calculations, and display them to the screen. A line from the data file would look like:
a 4 10 3 5

The 'a' stands for add, 4/10 would be one fraction, and 3/5 would be the other. I've tested my program out in terms of the arithmetic through hard coding and I don't seem to have problems there. I'm pretty positive the problem is somewhere in these areas. First, my overloaded >> operator:

Code:
istream& operator>>(istream& isObject, Fraction& fract)
{
	isObject >> fract.numerator >> fract.denominator;

	return isObject;
}
So in the main function of my program, I have something like this to read through the data and display it as necessary. Here's the start of it:
Code:
while (!inFile.eof())
	{
        inFile >> fractionCode;

		switch (fractionCode)
		{
		case 'a':
		{
			Fraction x;
			Fraction y;
			Fraction z;

			inFile >> x >> y;

			z = x + y;
			cout << x << " + " << y << " = " << z << endl;

			break;
		}
And so on. But when I run this with a sample data file, all I get are a bunch of zeros in the output. Does there appear to be anything obvious I'm just overlooking here?
Programming homework and newbie help thread Quote
11-12-2016 , 05:42 PM
Try making your overloader friendly.

friend istream& operator>>(istream& isObject, Fraction& fract)
Programming homework and newbie help thread Quote
11-12-2016 , 06:17 PM
Already had that. I just found the mistake, which in fact was in a different part of the code and was a very simple very stupid mistake.
Programming homework and newbie help thread Quote
11-12-2016 , 06:39 PM
Quote:
Originally Posted by LBloom
Already had that. I just found the mistake, which in fact was in a different part of the code and was a very simple very stupid mistake.
Some how doing integer division?
Programming homework and newbie help thread Quote
11-12-2016 , 06:58 PM
Quote:
Originally Posted by LBloom
Already had that. I just found the mistake, which in fact was in a different part of the code and was a very simple very stupid mistake.
Heh, most are. I'd say over 50% of my debug efforts end with me exclaiming "oh you idiot!".
Programming homework and newbie help thread Quote
11-12-2016 , 08:34 PM
"I'll remember to not make that mistake again" (I wish)

I make 2 python mistakes at least once a week. One is having a function like "foo()" and calling it like
foo
(with no parens). This is a valid line of python that does literally nothing at all.

Another is accidentally having a comma at the end of a line, like
a = foo,
which makes a a tuple equal to (foo, )
Programming homework and newbie help thread Quote
11-13-2016 , 03:41 AM
"Stupid compiler, you knew what I meant!"
Programming homework and newbie help thread Quote
11-13-2016 , 12:52 PM
Recent versions of gcc are getting more helpful. If you mispell a variable or function name instead of just complaining that it can't figure out what symbol you mean, it suggests "did you mean foo?"

I don't remember what it is now, but for the longest time gcc would give an error that was literally "you wrote foo but you meant bar" If you know that, why are you telling me, just do it?
Programming homework and newbie help thread Quote
11-13-2016 , 03:52 PM
Quote:
Originally Posted by RustyBrooks
Recent versions of gcc are getting more helpful. If you mispell a variable or function name instead of just complaining that it can't figure out what symbol you mean, it suggests "did you mean foo?"

I don't remember what it is now, but for the longest time gcc would give an error that was literally "you wrote foo but you meant bar" If you know that, why are you telling me, just do it?
Are you kidding? The idea of making warnings fatal should be one of the least surprising things in the world to any serious programmer.

Code meant to read, understood, and changed by people. You shouldn't expect the code to be changed based on a "just to it" philosophy.
Programming homework and newbie help thread Quote
11-13-2016 , 05:25 PM
Quote:
Originally Posted by au4all
Are you kidding? The idea of making warnings fatal should be one of the least surprising things in the world to any serious programmer.

Code meant to read, understood, and changed by people. You shouldn't expect the code to be changed based on a "just to it" philosophy.
I wasn't that serious but also I wasn't joking.

Actually, I remember one now. Visual Studio decided to rename some key functions, I think the sprintf series - they were replacing insecure versions with secure versions that acted the same, but without the over-run exploits possible.

Using the "old" one caused the compiler to fail, saying, "we replaced X with Y. Use Y instead"

This is stupid. It would make way more sense to make the secure version use the same name, and if for some reason you just *had* to have the old version, have it available with a different name.
Programming homework and newbie help thread Quote
11-14-2016 , 08:32 PM
Hopefully my last question about my Fraction assignment.

My Fraction class has two variables the constructor initializes: numerator and denominator. Is there a simple way for me to just exit the program from the constructor if the denominator == 0? For the purpose of this assignment I am not supposed to do something like change the denominator to 1, just end the program.
Programming homework and newbie help thread Quote
11-14-2016 , 09:27 PM
Yeah, you can use "assert", like
assert(denominator != 0);
which will cause the program to exit if it is 0.

You could also use exit() which takes an integer as it's argument. It exits the program and returns the integer to the operating system. exit(0) sort of means "everything was OK but the program is done" and anything else means a problem of some kind. It's common, if there are many ways your program could have an error, to have a distinct exit code for each, so that people who call your program can look up why it exited.
Programming homework and newbie help thread Quote
11-14-2016 , 09:37 PM
You could also probably raise an exception, which is probably the nicer way to do it. That way, callers of your class could choose to catch the exception and handle it themselves. I always hate it when people put asserts in their libraries personally.
Programming homework and newbie help thread Quote
11-14-2016 , 09:43 PM
Putting code not related to constructing the object into a constructor is a big nono. Throw a std::invalid_argument. In your code that creates the instance of a Fraction, catch the exception and handle it appropriately (print an error message and end the program).
Programming homework and newbie help thread Quote
11-14-2016 , 09:47 PM
I would recommend outputting this image to the screen to notify the user that zero is an invalid argument.

Programming homework and newbie help thread Quote
11-14-2016 , 10:23 PM
Quote:
Originally Posted by ChrisV
Putting code not related to constructing the object into a constructor is a big nono. Throw a std::invalid_argument. In your code that creates the instance of a Fraction, catch the exception and handle it appropriately (print an error message and end the program).
I think the exception is the best alternative, however, I think you can safely break this rule if you are literally exiting the program.
Programming homework and newbie help thread Quote
12-03-2016 , 10:55 PM
I'm working with vectors in c++ for the first time. In my assignment, I have to load a vector with objects I create with data from a text file. So what I am trying to do is read a line of data from a text file, create an object with that data, put the object into the vector, and then repeat. I (attempt to) do this with a function that looks like this:
Code:
int loadInventory(vector<Item>& stock, string file)
{
	Item inputItem;
	ifstream inFile;
	int count = 0;
	
	inFile.open(file);
	
	inFile >> inputItem;

	stock.push_back(inputItem);

	while (inFile >> inputItem)
	{
		stock.push_back(inputItem);
		count++;
	}
	cout << count;
	inFile.close();

	sort(stock.begin(), stock.end());
	
	return 0;
}
For a reason that I'm sure is pretty simple but I just can't see, all this is doing is reading the first line of the file, creating that object, and storing it in the vector. I know that it is reading that line and creating the object because other parts of the program work correctly if I'm trying to access the data stored in that first object. For some reason, no other data is being read in. The "while(inFile >> inputItem) loop does not get entered at all and I don't see why. I used the count variable for the purpose of checking that. If I comment out the first "infile >> inputItem", then the while loop gets entered once and I still get that first line of data and nothing else.

Instead of "while(inFile >> inputItem)" I had "while(!inFile.eof()), but that was leading to an infinite loop for some reason.

I'm really at a loss here. This seems so simple and I can't understand why it isn't working, especially the way it isn't working.

BTW, the function is an int because of error handling that I got rid of to make this shorter. I know the error handling code isn't somehow the problem because I've tried it with that code removed.
Programming homework and newbie help thread Quote
12-03-2016 , 11:06 PM
Did you write a stream operator overload for your Item class? Does it, uh, work properly?
Programming homework and newbie help thread Quote

      
m