So I decided to start over. Scrap everything, because apparently I've done it all wrong according to the above 2 posters. I'm going to be working out of a book, called C++ Primer 5th Edition, I'm still going to treat this like a job, but it will be more lesson oriented. I'll be honest, being told, a lot of the stuff you guys told me, discouraged me for a few days. Contemplated giving it up entirely, contemplated a lot of things. But I'm not going to let depression win. As such, I've started going through the C++ Primer 5th Edition, which covers things like C++11, and is a bit slower than Accelerated C++ is. It also clocks in at a massive 1000+ pages. I'm skipping posting a few things, because well, some of the things are boring for you guys. But I will do them, however updates will be a bit slower.
Alright so first question to the programmers out there, C++ Primer has this to say about std::cout,
"The <<operator takes two operands: The left-hand operand must be an ostream
object; the right-hand operand is a value to print. The operator writes the given value
on the given ostream. The result of the output operator is its left-hand operand.
That is, the result is the ostream on which we wrote the given value.
Ouroutput statement uses the <<operator twice. Because the operator returns its
left-hand operand, the result of the first operator becomes the left-hand operand of
the second. As a result, we can chain together output requests."
It then goes onto say, that
std::cout << "Enter 2 numbers: " << std::endl;
is equivalent to
(std::cout << "Enter 2 numbers: ") << std::endl;
Does that mean, when you cout something, and you are chaining things together with << operators, each link in the chain, is basically including all of the previous ones because the operator is returning everything to the left of it? If so, would it be smarter/more efficient for the compiler to write it out as
std::cout << "Enter 2 numbers: ";
std::cout << std::endl;
or even
std::cout << "Enter 2 numbers: \n"
This is just a curiosity. Not something I'm planning on incorporating, literally just a random fit of interest in how it works, but a lack of ability to test it. Though, later on the same page it mentions something about how endl flushes the buffer, and that it's best to flush the buffer, so that if a program crashes it doesn't leave bits hanging around in memory. Hrm, is \n equivalent to an endl, I doubt it. This is rather interesting. A little googling suggested that the third one is more efficient because of how long it takes flush the buffer. So in cases of messing around with pure text, and needing highly performent code, you'd not use std::endl. Because it is the largest reason why C++ IO is generally slower than that of C.
But in regards to the first few exercises, I like that they don't show you hello world, but leave you to infer how to make hello world from a slightly more advanced bit of code.
The code in 1.6 is fine barring the semicolon placement, removing the semicolons makes it print correctly. Alternatively and this is possibly the answer they are looking for, adding std::cout in front of each line would make it work. Here's the code in question they wanted you to state if it would work, what it was supposed to do and how to fix it if it didn't.std::cout<< "The sum of " << v1;
<< " and " << v2;
<< " is " << v1 + v2 << std::endl;
1.7 is just inputting and viewing an error for nested comments, that error looked like this, 1>d:\users\tyler\documents\visual studio 2013\projects\c++primer5th\chapter1\comments.cpp(1): warning C4138: '*/' found outside of comment
1>d:\users\tyler\documents\visual studio 2013\projects\c++primer5th\chapter1\comments.cpp(1): error C2059: syntax error : '/'
1.8 is a bit more interesting,
Exercise 1.8: Indicate which, if any, of the following output statements are
legal:
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;
After you’ve predicted what will happen, test your answers by compiling a
program with each of these statements. Correct any errors you encounter.
I think the first and second ones are the only ones that work. The first one is the definite might, which the second will definitely work.
The fourth ones possibly working. Let's actually figure that out, following along, open comment, close comment, within quotation marks, one quotation mark visible to the program, open comment, invisible because it is within quotations, followed by an open comment within quotation. Will work I think, because it closes the quotes then begins the next set of comments. But very tricky and maybe wrong.
The third definitely won't work, because it's calling cout without closing the output's quotes.
Time to check if those actually were right answers.
First and second worked, third tossed these errors, 1>d:\users\tyler\documents\visual studio 2013\projects\c++primer5th\chapter1\comments.cpp(10): error C2001: newline in constant
1>d:\users\tyler\documents\visual studio 2013\projects\c++primer5th\chapter1\comments.cpp(12): error C2143: syntax error : missing ';' before 'return'
The fourth amazingly works, I was really guessing hard on that one. But I guess I should have guessed easier because of how the first one passed. On to chapter 1.4.