Hello Euno,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <limits>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main() // <--- If you do not use "argc" and "argv" you do not need it here.
{
int num1 = 0, num2 = 0, total = 0;
std::cout << "\n Please enter an integer.: "; // <--- Puts "cin" on the same line.
std::cin >> num1;
std::cout << " Please enter an other integer.: ";
std::cin >> num2;
for (int i = 0; i < 1; i++)
{
//std::cout << Total << '\n';
//Total += i;
total += num1;
total += num2;
}
std::cout << "\n " <<total << '\n';
std::cout << "\n The sum of " << num1 << " + " << num2 << " = " << num1 + num2 << "." << std::endl;
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
About your program:
Line 5 of your code the "argc" and "argv" are not used, so you do not really need it. It is OK if you leave it, but it is reserving space for the variables that is never used.
When it comes to variable names try to avoid using a single letter. In larger programs it is hard to keep track of what it is for. For a program like this "num1" and "num2" are much easier to follow in the code.
Next is the capital letters. "X" and "Y" lead me to believe that these variables are defined as a constant that can not be changed. Kind of misleading
As
Repeater once wrote:
You know you're not being charged for every letter you use, right? If you want people to be able to read your code (which you do), use variable names that help people understand what you're trying to do.
|
It is generally accepted that regular variables start with a lower case letter and if you have a name with two or more parts, (words), "camelCase" is used to make a distinction between the words. The underscore is also an accepted method, (cames_case). Starting a variable name with a capital letter is used for classes and structs and sometimes I use this for functions. All capital letters are used for variables that start with "const" or "constexpr" and help to remind you that these variables are constants the can not be changed.
I have also found it to be a good practice to initialize the variables when defined. This is not always necessary, but at least you have the peace of mind of knowing that they do not contain a garbage value from being uninitialized. "std::string", "std::vector" along with other containers are empty when defined and do not need initialized, but you can if you need the variable to start with a given value.
For your prompts notice the difference between you did and what I did. Leaving off the "std::endl" puts the "std::cin" on the same line as the prompt.
Your for loop starts at what would be (5) and goes to (13). Be careful using "<=". Quite often this will do one more loop than you want. There are some times when it is useful, but nor all the time.
Inside the for loop you are adding to "total" (5, 6, 7 ... 13) because you are adding "i" to "total" each time through the loop. Since you only have two numbers to add together the for loop is not the solution to the problem unless that is what you want to do, but I would consider reversing the lines.
I did adjust the for loop to add the two numbers together, but as you look at it the loop is only one time. Not much point to it when the last "std::cout" can add the numbers there.
A last note: a few blank lines make the code easier to read. The compile does not care about white space, blank lines or comments. These are basically ignored at compile time. The easier your code is to read the quicker your responses can be.
Mostly with time and practice you will learn where to put blank lines to break up your code.
Hope that helps,
Andy