@mathman54,
Relying on whitespace indentation to indicate blocks of code is one day gonna bite you in the
tuchas. The C++ compiler/preprocessor ignores that type of whitespace. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
int main()
{
std::cout << "Enter a number less than 10 or greater than 100: ";
int x { };
std::cin >> x;
std::cout << '\n';
if (x >= 10)
if (x > 100)
std::cout << "More than 100, Thanks!\n";
else // not the else intended!
std::cout << "Less than 10, Thanks!\n";
}
| |
Here's what the code looks like when
properly formatted, this is how the preprocessor/compiler interprets the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
int main( )
{
std::cout << "Enter a number less than 10 or greater than 100: ";
int x { };
std::cin >> x;
std::cout << '\n';
if ( x >= 10 )
if ( x > 100 )
std::cout << "More than 100, Thanks!\n";
else // not the else intended!
std::cout << "Less than 10, Thanks!\n";
}
| |
Your code could be "badly formatted" with/without whitespace and to the compiler the code is still the same:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
int main()
{
std::cout << "Enter a number less than 10 or greater than 100: ";
int x { };
std::cin >> x;
std::cout << '\n';
if (x >= 10)
if (x > 100)
std::cout << "More than 100, Thanks!\n";
else // not the else intended!
std::cout << "Less than 10, Thanks!\n";
}
| |
Explicitly using matching braces to indicate code blocks can serve a dual purpose.
1. they define properly where block(s) of code start and end.
2. If you want to add more lines of code in a block already having braces ensures your block is properly handled when compiled. Adding code to a block without braces puts the line(s) outside the block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
int main()
{
std::cout << "Enter a number less than 10 or greater than 100: ";
int x { };
std::cin >> x;
std::cout << '\n';
if (x >= 10)
{
if (x > 100)
{
std::cout << "More than 100, Thanks!\n";
}
}
else // fixed!
{
std::cout << "Less than 10, Thanks!\n";
}
}
| |
The Moral of the Story is relying on whitespace alone to define blocks of code is fraught with peril. "There Be Dragons There" that can make debugging your code more trouble than it needs to be.
Compiler suites can format code for you, Visual Studio is one. You can set up the formatting options so you can format existing code and format code automatically when pasting chunks of code into your project's files.
I use the formatting option on my code a lot in Visual Studio. Code I write and "found" code from the internet or in books.
As you saw even with simple program requirements such as you have there are multiple ways to write effective code. As you gain more knowledge and experience in writing C/C++ code taking simple and effective shortcuts such as seeplus shows can be a time/keystroke saver, especially when there's a deadline involved.
The tradeoff is one of potential readability. Code written can look like Sanskrit when looking at it after putting it aside and ignoring it for days/weeks/months.
The two bits of code, seeplus' and mine, essentially do the same thing, per your program requirements.
I'm not a professional programmer, merely a self-taught hobbyist, so my code tends to be more verbose than code written by someone who makes a living at
crufting code. I personally prefer more verbose code most of the time.