How to write a Repetitive Function

How to write the a repetitive function that
takes two parameters, A and B, and prints all the numbers from A to B inclusive.

Thank you!
You can use for loop and set the initial index to A and increments up to B.
1
2
3
4
int A, B;
	for (int i = A; i < B+1; i++) {
		cout << "Please enter two numbers" << endl;
		cin >> A >> B >> endl;

like this?
it doesnt go through though, is there an error here?
Last edited on
like this?

No. You have your cout and cin in the wrong place. You have to read the numbers BEFORE you start your loop.

1
2
3
4
5
6
  int A, B;
  cout << "Please enter two numbers" << endl;
  cin >> A >> B;
  for (int i = A; i < B+1; i++) 
  {  cout << i << endl;
  }


p.s. You can't do a cin to endl.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Got it! Thank you Anon!
Topic archived. No new replies allowed.