Help - Want to add two numbers using for loop

Pages: 12
Oct 31, 2019 at 6:04am
I want to add int1 and int2 without using arrays, how can I do so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int X = 0 , Y = 0 , Total = 0;
  cout << "Please enter an integer." << endl;
  cin >> X;
  cout << "Please enter an other integer." << endl;
  cin >> Y;
  for (int i = X ; i <= Y ; i++)
    {
      cout << Total << endl;
      Total += i;
    }
  cout << "The sum between the numbers is " << Total << "." << endl;
  return 0;
}

I don't want to add every integer b/w the numbers but I only want to add those two numbers. E.g
num1 = 5
num2 = 8
answer = 13
Last edited on Oct 31, 2019 at 6:04am
Oct 31, 2019 at 6:18am
You start with num1 ... and you add 1 (ONE) to it num2 times.
Oct 31, 2019 at 6:24am
Please send code, I am unable to understand
Oct 31, 2019 at 9:09am
1
2
  while(Y--) X++;
  cout << X <<'\n';


Please enter an integer.
5
Please enter an other integer.
8
13
Oct 31, 2019 at 9:41am
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
Oct 31, 2019 at 10:08am
Why is for used here?

std::cout << "\n The sum of " << num1 << " + " << num2 << " = " << num1 + num2 << "." << std::endl;

Without for loop, I can get the output using this output i.e num1 + num2
Oct 31, 2019 at 10:42am
Note: Any operation that uses arithmetic operators other than ++ or -- is not allowed.

I want to make this code using these operators only
Last edited on Oct 31, 2019 at 10:48am
Oct 31, 2019 at 12:08pm
If you can only use ++ or --, then presumably you understand that you can only add or subtract 1 to your number at a time. Yes?

If you want to add y to x, and you can only do it by adding 1 at a time, then you have to add 1 to x, y times. Yes?

What do we use if we want to do the same thing several times? A loop.

What sort of loop is most convenient to use when we want to iterate a known number of times? A for loop.
Last edited on Oct 31, 2019 at 12:09pm
Oct 31, 2019 at 3:05pm
lastchance has already posted a solution that has the required operators and a loop.

Just convert the while loop into for loop.
(Conversion is different challenge than deducing necessary logic from scratch, but better than nothing.)
Oct 31, 2019 at 6:02pm
Tell me how to convert that while into for? I tried but failed
Oct 31, 2019 at 6:06pm
Tell me how to convert that while into for?

If you understand how a for loop is constructed, and how a while loop is constructed, there should be no problem at all converting one to the other.

If you don't understand how those two things are constructed, you need to go back to your textbook and learn that. Loops are among the most fundamental concepts in programming, and is vital that you learn and understand, properly, how to write them.

I tried but failed

Telling us you failed is useless. If you show us the code you tried to write, and explain how it failed, then we can help correct any misunderstandings you have.
Oct 31, 2019 at 8:43pm
I suspect that your teacher might have apoplexy, but this sort of meets the instructions:
1
2
3
4
5
6
7
8
#include <iostream>
int main()
{
  int X, Y;
  std::cout << "Please enter two positive integers: ";   std::cin >> X >> Y;
  for ( ; Y--; X++ );
  std::cout << X;
}

Please enter two positive integers: 5 8
13 
Nov 1, 2019 at 4:54am
For subtraction, we will do X- -, Y--
But then what about division and multiplication?
Last edited on Nov 1, 2019 at 5:36am
Nov 1, 2019 at 8:47am
Oh, you are on that course. You are expected to know math.

Remember that 5*8==8+8+8+8+8 (the 8 is added 5 times to the result).
Nov 1, 2019 at 8:57am
That would make it something like
result+= X;

But I am not allowed to use any other op except ++ and - -
Nov 1, 2019 at 9:20am
But you are allowed to run (an outer) loop Y times.
Last edited on Nov 1, 2019 at 9:21am
Nov 1, 2019 at 11:32am
Bro is there any other way to do so? The one I want?
Nov 1, 2019 at 11:36am
What do you mean? Why don't you want to run a loop.

You've already been shown how to do an addition as a series of multiple increments of 1.

You've already been shown how to a multiplication as a series of additions.

Now you just have to combine those techniques, using lastchance's suggestion.
Nov 1, 2019 at 11:43am
I want to run a for loop using only -- and ++ ops
Nov 1, 2019 at 11:44am
Yes, we know. You've already been shown everything you need to be able to do that, as I've just explained in my previous post.
Pages: 12