C++ motivational thread

I can see your points.

Never having looked at this C++ committee stuff (looking a bit today), I do agree in also wondering how much of the decisions are coming from industry reps protecting their own. I think the same must likely happen in lots of open source software projects.

Anyone know of instances where this has absolutely happened?

One of those going off in the weeds cases. Looking back at operators per variable type, I’m not sure what goes on with the >> and << operators, when not used with cin and cout. They are listed as operators for all types as ‘read from’ and ‘write’ with examples in the operators table such as s >> x. Those examples don’t seem to actually do anything. But the compiler doesn’t complain either.

int a = 1;
int b = 0;
b << a; //write a to b
cout << a <<  " " << b << '\n';

Edit: Bah. Those are bit shift operators. Nothing to see here (except me being blinded in the weeds).

Review question: What terminates input into an int?

Good question, book. :smiley: I guess it is whitespace, same as with a string.

Sorting three numbers. This is super ugly to look at (and write!), but see the comment. Kind of fun having to work it out though. Would you do it different somehow? Maybe this sort of stuff is a waste of time though.

#include "std_lib_facilities.h"
// Sort three numbers; available facilities are limitied to:
// variables, cin, cout, if (no else), while, and basic operators

int main()
{
	int val1;
	int val2;
	int val3;

	cout << "Enter three integer numbers:\n";
	cin >> val1 >> val2 >> val3;
	
	if(val1 <= val2){
		if(val1 <= val3){
			cout << '\n' << val1 << ", ";
			if(val2 <= val3){
				cout << val2 << ", " << val3 << '\n';
			}
			if(val2 > val3){
				cout << val3 << ", " << val2 << '\n';
			}
		}
		if(val1 > val3){
			cout << '\n' << val3 << ", " << val1 << ", " << val2 << '\n';
		}
	}
	if(val1 > val2){
		if(val2 <= val3){
			cout << '\n' << val2 << ", ";
			if(val1 > val3){
				cout << val3 << ", " << val1 << '\n';
			}
			if(val1 <= val3){
				cout << val1 << ", " << val3 << '\n';
			}
		}
		if(val2 > val3){
			cout << '\n' << val3 << ", " << val2 << ", " << val1 << '\n';
		}
	}
			
	return 0;	
}

Same as above for strings. See note in comment. I also wonder here how ints are evaluated.

#include "std_lib_facilities.h"
// Sort three strings; available facilities are limitied to:
// cin, cout, if (no else), while, basic operators
// Note:  comparison operators seem to value strings from lowest to highest
// according to char codes

int main()
{
	string word1;
	string word2;
	string word3;

	cout << "Enter three strings:\n";
	cin >> word1 >> word2 >> word3;
	
	if(word1 <= word2){
		if(word1 <= word3){
			cout << '\n' << word1 << ", ";
			if(word2 <= word3){
				cout << word2 << ", " << word3 << '\n';
			}
			if(word2 > word3){
				cout << word3 << ", " << word2 << '\n';
			}
		}
		if(word1 > word3){
			cout << '\n' << word3 << ", " << word1 << ", " << word2 << '\n';
		}
	}
	if(word1 > word2){
		if(word2 <= word3){
			cout << '\n' << word2 << ", ";
			if(word1 > word3){
				cout << word3 << ", " << word1 << '\n';
			}
			if(word1 <= word3){
				cout << word1 << ", " << word3 << '\n';
			}
		}
		if(word2 > word3){
			cout << '\n' << word3 << ", " << word2 << ", " << word1 << '\n';
		}
	}
			
	return 0;	
}

Supplemental to above example (prints char codes).

#include "std_lib_facilities.h"

int main()
{
	cout << "Prints char codes from 32 to 127:\n\n";
	int i = 32;
	while(i < 128)
	{
		char c = i;
		cout	<< "int ("
				<< i
				<< ") == char (" 
				<< c
				<< ")\n\n";
		++i;
	}
	
	return 0;
}

Anyone know of a source for programming problems by experience level, where some examples are pointed out how the problems are useful for real world use? There is no shortage of programming problems available, but no one seems to have anything to say on how solving the problems will be useful. This is one of those questions for which search engines turn up nothing useful. Query examples:

useful / practical / real world / programming problems
reasons for solving specific programming problems
practice programming problems applied to real problems

It looks like the tutorial is getting the reader to thing about how to sort stuff. It’s good that you are wondering how a string is sorted since this is important stuff. You can define what the comparison (and ‘>>’ etc) operators do for your own types, and the types that come with C++ have these defined differently for different types.

Once we can come up with a sensible way of ordering data from various structures we can think about different sorting (etc) algorithms. Data structures and algorithms is the title of a computer science class! :smiley:

For the record, I would sort something with std::sort. I’ve implemented a couple of sorting algos over the years for exercise, but you wouldn’t solve that solved problem yourself unless you are a wizard doing it with SIMD instructions using 4 bytes of memory or something useful like that. There are nice videos on line that visually show different sorting algorithms at work, here’s one of the more entertaining ones:

1 Like

It’s helpful to think of programming as just doing stuff to data.

(input=) data -> do stuff -> output

The cin stuff tutorials are just one example of a data source, replace the cin source of the data with lots of useful data in a file or object(s) that you need to do something interesting with to store/transmit/display and that’s kind of what programming is. Knowing what programming can and can’t do is crucial and knowing the general hows of how similar problems are solved allows you to dip into the former category and complete a task.

It might seem pointless but it’s not (or it is, depending on perspective). I have to say that learning with something like Reaper where you can use Python, Lua or EEL2 to do genuinely and personally useful and satisfying things is good. But I wouldn’t skip the basics and remember to type and compile!!

I mean for example, fizzbuzz or printing n of a fibonacci sequence. How is solving these sorts of practice problems useful in the real world?

I must have blazed through the part of this book that I worked through the first time around, because I’m noticing quite a few things along the way (that aren’t pointed out) this time around that I don’t remember coming up.

And of course I type and compile everything. Helps to make it stick in the noggin, even though it can be a bit annoying at times.

It gets you used to iterating through data in different ways, asking questions of it and either altering it or creating new data. As well as reading code of course and knowing what it does from that. Well, sometimes you don’t know from a specific chunk because operators like >> or whatever can be defined/overloaded to do anything at all.

For Fibonacci you are doing stuff based on the previous result. Maybe look at that as processing a sample based on whatever the previous sample was. With FizzBuzz you are iterating over a sequence of numbers and doing stuff depending on properties of those numbers, what if those numbers were sprites, or customer records?

But the stuff you are looking at is kind of raw and abstract. I think better examples would be appreciated. For example with inheritance, even hardcore composers admit that OOP inheritance is a good model for GUIs. So make examples about GUIs instead of f-ing Animals!?! :slight_smile:

Yea, that is the problem with those sorts of practice problems. They don’t touch anything real and so don’t lend themselves to clearly transferring to something else real.

100 pages back into this beast of a book tonight. Only about 300 more to go to get back up to the graphics stuff. As far as instructional books go, I haven’t seen anything better on any topic, but it still does get muddled in places, so there is lots of rereading some sections and deciphering of meanings and tinkering to make things clear. It could be also that my high enthusiasm when working with the book for the first time blinded me a bit to the muddled parts, taking away a more favorable impression. Any way, there are no perfect instructional books, and finding one that is even serviceable seems to be a major feat in itself. Week one down. I think this coming week will be slower in terms of pages, since chapter 4 is denser in topics and with less trivial problems to solve. I think this time around with this book I will take some time inbetween to consolidate topics by experimenting more and writing more little programs using the current available facilities. The exercise programs seem fine enough, but working toward more personally interesting things makes the effort less of a chore.

Edit: This post is pretty much completely wrong. Leaving my mistake here (and learning from it).

Probably not terribly interesting I guess, and maybe I blazed over it in the past. I wasn’t aware that an ‘else if’ isn’t an actual control statement. In other words, this:

if(expression)
    statement
else if(expression)
    statement
else
    statement

Is really this, where a second if and else are semantically nested in an else:

if (expression)
    statement
else{
    if(expression)
        statement
    else
        statement
}

The second is a little longer but clearer in it’s meaning, if you ask me. But how is it functionally different than:

if(expression)
    statement
if(expression)
    statement
else
    statement

Maybe I’m missing something here, but ‘else if’ seems redundant compared to an additional ‘if’ and ‘else’. For clarity to me ‘if if else’ is the winner.

Hmmm bit rusty but ex 3 could pose more cpu cycles
If the first if is true, it would still eval the second if

Ex1 and 2 if the first if is true, it doesn’t bother with the rest

Yea, of course you’re right. I went off in the weeds today and lost my shoe in the mud.

Book gives this as an example of an if statement with a useful empty block:

if(a<=b){
}
else{
	int t = a;
	a = b;
	b = t;
}

Maybe I’m missing something. Is that actually useful somehow instead of this?

if(a>b){
	int t = a;
	a = b;
	b = t;
}

I have gotten slowed down this week by very likely what is covid. :confused:

It explicitly covers all of the conditions so, with a “do nothing” comment, it’s blindingly obvious that you do actually mean to do nothing. There are compiler switches that warn/error when not all cases in a switch statement acting on an enum are covered, or if there’s no default case so it’s logically along the same lines.

I’m sure somebody somewhere loves it.

Good luck with the bug, you outran it for long enough!

Gotcha. Thanks.

And it is ironic that I got the bug now. Working in a pretty big shop with max two other people on a daily basis so far, doors wide open and fans blowing for cooling and fresh air. But my girlfriend covered watching her kid sister who had a ‘cold’ over a week back. Then their mom turned up with covid. Then I turned up with it (PCR test result not back yet; rapid test was negative; not that I trust either of them anyway). So it seems clear how I got it. My girl did get vaccinated twice (under pressure), and she is only experiencing mild fatigue so far, days after my symptoms kicked in - elbow ache in one arm, then muscle ache in same arm (thought it was work related!), then a runny nose for a couple of days on top of it, then joint and muscle ache in both arms (all mild), then in leg joints and muscles, then mild fatigue. I’ll take this over the possibility of vaccine injury.

Being unwell with these kind of viruses is alright, I’d much rather be achy with a temperature for a couple of days than have one of those annoying sneezy colds with the sore throat that doesn’t go away for ages.

Yea, same here. So far (knock on wood), this is very mild. Not approaching a flu.

1 Like

Are you taking anything for it, as in C/D/zinc/quercetin?

There’s a list on here somewhere…

So no headache? I thought that was the main symptom, from people I know getting it