Calculating from - to via user input

Hi all!

First, I am new here, but I have read the forum as a guest from time to time.

I am also quite new to C++, but have learnt a little, and solved a couple of obstacles in the way. Until now. At this point I am completely stuck, hence my appearance at this forum.

I am certain my question is ”simple” for those of you who are well versed in C++, please bear in mind that I am (as stated above), pretty new to this language.

Anyway, the problem I have is as follows:

A user is asked to input two integers, the function I want to write is supposed to add the numbers, and the ones in between, together, as follows:

1
2
cout << "Please enter two numbers: ";
cin  >> no1 >> no2;


Now, let's say the user input is 2 and 4. The function should then add 2+3+4. If the user enters 1 and 6, the function should do 1+2+3+4+5+6, and so on.
(Even when I look at it now, I go ”this should be EASY!!!”).

In other words, the first number, and all the numbers in between and up until the last number.

I have tried various (all!?!) types of loops (because I'm guessing a loop must be used in order to find out how many times the function should add). One example looks as follows:

1
2
3
4
5
6
  for ( no1; no1 <= no2; no1++)
  {
    result = no1 + no1+1;
  }

  cout << result;



...it doesn't work.

I tried the while variant:

1
2
3
4
  while (no1 <= no2)
  {
    result = no1 + ++no1;
  }


...doesn't work either. I also tried with a do-while loop, but I'll spare you from that.

Now, my guess is that it might not be the loops themselves, rather my logical thinking that is at fault. Furthermore, I am not ”allowed” to use arrays or any fancy stuff in order to solve this dilemma.

Yes folks, I have searched the mighty Google for answers - the closest I came was something called a ”factorial”, but it did not seem to work - mainly because factorials seem to always go from whatever value down to 1. Whereas I need to go from ”point a to b” so to speak.

Ok, this turned into quite a long first post. To sum it up and repeat;

I need a function that adds x+x+x+x dependeding on user input.

I hope I can give back to the community here once I learn a little more.

Thanks for reading this,

HMW
Rather than just give you an answer, I'll give you a hint as to where you're going wrong. You need to keep track of the running total as you go, so each time the new result will be the previous result plus something.

result = result + number;

for example.

Try something like that in your first example code above.
Last edited on
Rather than just give you an answer, I'll give you a hint as to where you're going wrong.


Hey Moschops!

No problem, I am one of those fools who actually want to learn what I am doing (otherwise I wouldn't have spent hours upon hours on trying!).

I appreciate your hint. Will try it out A.S.A.P.

Thank you!

HMW

EDIT: I forgot to mention that it is supposed to work with multiplication as well. The same idea though.
Last edited on
You, sir, are a genious!

I implemented your snippet as suggested, and it works as it should. It now looks like follows:

1
2
3
4
5
6
7
  for (no1; no1 <= no2; no1++)
  {
    result = result + no1;

  }

  cout << result << "\n";


It worked fine for multiplication as well, I just needed to change the initial value of

result

to:

result = 1;

Many thanks from a bleary-eyed and tired HMW!

PS. I still haven't quite figured out why it works, but I'm going to put some cout's in the loop to get the hang of what I was doing wrong in the first place. If you feel like it, please break it down for me, if not - I am still incredibly grateful! DS.

I reckon you should now do a few simple test cases, just to check it's all running smoothly.

What happens, for example, if your start and end value is the same (i.e. no range, just one number)? The answer should just be that number; is it? Does the same initial value for result work for both addition and multiplication?

The key was that you need a running total; each time you added a number, you have to add it to the total you already had, and then that new value became the new total.



Last edited on
What happens, for example, if your start and end value is the same (i.e. no range, just one number)? The answer should just be that number; is it?


Yes, it is. For both + and *.

Does the same initial value for result work for both addition and multiplication?


No. For addition, result needs to be:

result = 0;

For mulitplication, result needs to be:

result = 1;

That, however, is not a problem in this case. Eventually I am going to have to break down the function into an if-switch, depending on whether the user want to add or multiply. That will be a challenge as well, but I think I should manage that. What I really needed to sort out was this dilemma. I couldn't have done it without your help.

Thanks again Moschops!

HMW

Edit: I think I need to get some sleep now. . .
Last edited on
Topic archived. No new replies allowed.