C++ motivational thread

You’re good at chess so you’d be good at programming!

I’m right there with you. And I’m amazed any time when I tinker with programming that I can manage to do any of it. Ha ha. You might be ace at it.

I did achieve GM level on a no cheat training app [Stockfish] but in a two day tournament I’m over it at the 3/4 mark, just woodshifting at that stage [chess term for careless play]

Coding is interesting, questions about how best to implement plans, which is why I like Backgammon and Euchre, though obviously the latter aren’t objectively important like coding is.

But at this stage Ima try to make 5 albums before I drop off the twig, that’s probably a tall enough order given my progress so far :grinning:

I won’t rule it out, would be interesting and infinitely more productive use of time than gaming. I mainly play games to disrupt the feedback loop of other stuff I work on, music projects etc.

Let us know if you give it a go. It’s always good to know other people who have a shared interest to talk about it with. When I had more free time to put into it (a few years back), I really enjoyed the challenge of it, using the same book that I’m using now. Just getting back to it since that few years hiatus and feeling squeezed for time to put into it. I also enjoyed a C book that I have kicking around here somewhere, but it just covered some basics and I didn’t find a good followup book to it at the time. In my opinion, a good book is a must and most important, over language choice and anything else. Maybe less important if you have access to good mentors.

Also, the bonus is that if you get far enough along into it (hopefully not too long) you get to make useful software for yourself and others, along with the challenge of the programming problems. And if you go far enough with it, maybe even some $.

Today’s effort went to a book exercise. Obviously I have lots of cleaning up to do here and some reorganizing, but it’s a start. This is a stepped exercise, refining things at each step.

#include "../std_lib_facilities.h"
// Compares lengths of different units and converts to meters

int main()
{
	double num_current;
	double num_current_meters;
	double num_smallest;
	double num_smallest_meters;
	double num_largest;
	double num_largest_meters;
	double num_previous;
	double num_previous_meters;
	constexpr double almost_equal = 1.0/100;
	string num_current_unit;
	string num_smallest_unit;
	string num_largest_unit;
	string num_previous_unit;
	
	cout << "\nEnter a number and unit (in, ft, cm, m) or 'q' to quit: ";
	
	// Get input
	bool i = 0; // Flag first run
	while(cin >> num_current >> num_current_unit){

		// Convert current number to meters
		if(num_current_unit == "cm")
			num_current_meters = num_current / 100;
		else if(num_current_unit == "m")
			num_current_meters = num_current;
		else if(num_current_unit == "in")
			num_current_meters = num_current * 2.54 / 100; // To cm, to meters
		else if(num_current_unit == "ft")
			num_current_meters = num_current * 12 * 2.54 / 100; // To in, to cm, to meters
		else
			break;

		cout << "You entered: " << num_current << " " << num_current_unit << " (" << num_current_meters << " meters)\n";
			
		// First run, smallest and largest are the same
		if(i == 0){
			num_smallest = num_current;
			num_smallest_unit = num_current_unit;
			num_smallest_meters = num_current_meters;
			
			num_largest = num_current;
			num_largest_unit = num_current_unit;			
			num_largest_meters = num_current_meters;

			num_previous = num_current;
			num_previous_unit = num_current_unit;
			num_previous_meters = num_current_meters;
			

			i = 1;
		}
		// After first run
		else if(i == 1){
			// Set smallest and largest so far
			if(num_current_meters < num_smallest_meters){
				num_smallest = num_current;
				num_smallest_unit = num_current_unit;
				num_smallest_meters = num_current_meters;
			}
			else if(num_current_meters > num_largest_meters){
				num_largest = num_current;
				num_largest_unit = num_current_unit;
				num_largest_meters = num_current_meters;
				}

			// Check equal
			if(num_current_meters == num_previous_meters)
				cout << "The current number is equal to the previous number.\n";
			// check if almost equal
			if(num_current_meters < num_previous_meters){
				if((num_previous_meters - num_current_meters) < almost_equal)
					cout << "The current number is almost equal to the previous number.\n";
			}
			else if(num_current_meters > num_previous_meters){
				if((num_current_meters - num_previous_meters) < almost_equal)
					cout << "The current number is almost equal to the previous number.\n";
			}
			
			num_previous_meters = num_current_meters;
			num_previous_unit = num_current_unit;
		}
		else
			break;

		cout << "The largest value so far is: " << num_largest << " " << num_largest_unit << " (" << num_largest_meters << " meters)" << '\n';
		cout << "The smallest value so far is: " << num_smallest << " " << num_smallest_unit << " (" << num_smallest_meters << " meters)" << '\n';

		cout << "\nEnter a number ('q' to quit): ";
	}

	return 0;	
}

Remember that there are the true and false keywords in C++, unlike C, so your bool statement can be:

bool i = false; // flag first run

… but preferably something like…

bool has_already_run = false;

… although flipping the logic reads better…

bool is_first_run = true;

That variable can only have two states so you are checking for 0/false and then doing an else if for 1/true with a further unreachable else which is a bit confusing for the reader.

I can sense that the book is going to introduce structs any moment now! :slight_smile:

Good looking out Snookoda. Structs is a little later. Hopefully not too much awful longer to get back there.

Hopefully not too much longer on these too.

2 Likes

Very nice, do you just need to close the lid there and they cook themselves in the baking heat you have? :slight_smile:

It has been surprisingly mild heat this week. Upper 80’s to 90F. The weather here is like a bipolar switch most of the year, but summer is usually baking hot.

Nice indeed, we do that here, veggies on sticks sometimes w chicken and always with satay sauce. I can eat tremendous amounts of it and no weight gain.

I don’t know about satay sauce. Got a recipe for it? I hit some of these with a garlic and herbs premixed spice and others with teriyaki sauce. 3 of them did me in.

I’ll ask the missus later on but it’s basically a peanut sauce like the Chinese use for satay, Fountain brand makes a version of it.

Here’s a version of satay sauce the missus says is bona fide

Ingredients

  • ½ lime, juiced
  • 1 tsp clear honey
  • 1 tbsp soy sauce
  • 1 tbsp curry powder
  • 3 tbsp smooth peanut butter
  • 165ml can coconut milk

Method

  • STEP 1

Mix the lime juice, honey, soy sauce, curry powder and peanut butter in a small bowl. Add a splash of water if it’s too stiff. Transfer to a small pan with the coconut milk, and heat gently for 5 mins stirring continually, until combined.

1 Like

Sweet. Thanks morgo. I’ll give that a go on next round of the 'bobs on the grill.

Still banging on this stepped exercise for which the last posted code was written. At this point the book is asking to reject any input number (length) that isn’t accompanied by a unit (inches, feet, centimeters, meters). Given that the tools covered up to this point are very basic (parsing is still some chapters away), and given that the input operator ‘>>’ ignores whitespace, I’m not seeing a good way to do that. I don’t know what the hell I did for this exercise the first time through this part of the book (maybe skipped it). This time around it seems that I’m being nudged to do something approaching parsing. The pseudo-code plan:

Get input as strings
Concatenate strings
Put characters of string into vector
Step through characters of vector
	Put number characters and '.' into double
		Stop at first character that isn't '0-9' or '.'
	Put remaining characters into unit variable
		Stop at end of vector
Reject if unit variable is empty

Maybe a simpler approach would be to start at the end of the vector. Reject if the last element is a number.

Is it just rejecting ones that don’t have a valid unit?

This step of the exercise has two components, actually. Reject any input number that isn’t accompanied by a unit. And reject invalid units.

This exercise, while not the most interesting topic, is a good one I think. 11 steps of modifying a program for complexity, which makes the reader think about breaking a big problem into smaller problems and each of those smaller problems into even smaller problems.

Maybe I should make a little program for our shop project manager and shop engineer. We have 4 steel racks to finish that were scheduled to be ready for pickup tomorrow. All 4 of them are getting a modification after we built 3 others to the customer’s engineering specs, and the things sagged heavily under load (holding tons of sheet metal material). Each rack has 3 10’ posts to which arms are welded to hold the material. The 4" square tube posts will be modified by welding on 4" ibeam to their backs to strengthen them. 4 racks @ 3 posts each = 12 posts. The modification will require 6" welds at 2’ spacing. So each post needs 5 welds per side, so 10 welds per post. But each of these ‘welds’ actually requires 3 welds to fill in the gaps between the rounded corners of the square tube and ibeam. So 30 welds per post x 12 posts = 360 6" welds. Delivered tomorrow they say…

A shift is 8 hours. Duty cycle for our welders is 2.5 minutes on, 7.5 minutes off. So, 10 minutes per cycle. 8 hours x 60 minutes = 480 minutes. 480 minutes / 10 minute cycle = 48 cycles per shift. 48 cycles per shift x 2.5 minutes duty cycle on time, = 120 minutes total weld time per welder in a shift. Let’s throw out all other variables and be really kind, saying that each 6" weld (which is really 3) requires 1 minute. 2 welders going at it will get 240 welds in perfect robot world.

The real world variables (or mostly constants): Driving to the next town over to pickup a truck and trailer, driving back to the previous town to pickup material from 2 suppliers (one of which is notorious for being very slow to load), driving to the shop to drop off the material, driving back to the next town over to drop off the truck and trailer, driving back to the previous town to the shop. 2 hours driving is likely, given the slow supplier mentioned. Cutting the ibeam to required length. Likely 1.5 hours. We’re almost out of welding wire, and that is a lot of welds. We’re likely also getting low on shielding gas. Likely 1.5 hours going after that. Fitting and clamping up the ibeam to the posts. Let’s be kind and say a few minutes per post x 12 posts = 36 minutes. Moving these massive racks back and forth to the weld station. Probably 5 minutes per rack x 4 racks = 20 minutes. Round off the last two to 1 hour total. Dealing with welder issues (changing wire, cleaning tips, moving them around to reach weld points and to not burn cabling), likely 1 hour each day. Dealing with porosity in welds (hot shop with fans blowing and blowing away shielding gas). Varies by day, but can eat up some time grinding them out and rewelding. Lunch break = 30 minutes. Other things come up. Drivers drop things off that need to be unloaded, people stop by to inquire about things, people call and message, things break along the way.

The program should be called Maybe Tomorrow, which calculates practical work time in a shift vs. scheduled product delivery dates.

A shot from the work day today.

You can iterate through the characters in a string without using a vector so you can get the input into one string and do your checks from there with string[X] or string.at(X). Going from the end would be better as you say, but I think the simplest way along the lines of what you’ve been doing is instead of parsing one string, change both input vars to strings and check if the one that’s supposed to be a number is a number.