Repetition structure (FOR)

Hello,
I have to prepare a program that from the year interval [m;n] selects the year that the festival took place and next to that year also shows which festival was it counting from the first one. The festivals have been held since the 1950s and take place every four years.

Given year interval is [1984;2007].

I made a program like this:

#include <iostream>
#include<cmath>

using namespace std;

int main()

{
int m, n, x, y;

cout<<"Enter first given year"<<endl;
cin>>m;

cout<<"Enter second given year"<<endl;
cin>>n;

y=0;

for (x=1950; x<=n; x++)

{
if (((1950-x)%4==0)&&(y=y+1))
cout<<x<<" "<<y<<endl;
}
}

My main problem is that I get the year starting from 1950, like this:
1950 1
1954 2
1958 3
1962 4
1966 5
1970 6
1974 7
1978 8
1982 9
1986 10
1990 11
1994 12
1998 13
2002 14
2006 15

And what I need for my program to show is this:

1986 10
1990 11
1994 12
1998 13
2002 14
2006 15

I would really appreciate your help, thank you.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

First pass refactoring, including using more descriptive variable names:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>

int main()
{
   const int defaultYear { 1950 };

   std::cout << "Enter starting year: ";
   int startYear { };
   std::cin >> startYear;

   std::cout << "Enter ending year: ";
   int endYear { };
   std::cin >> endYear;

   for (int i { defaultYear }, festivalCount{ }; i <= endYear; i++)
   {
      if (((i - defaultYear) % 4) == 0 && festivalCount++ && i >= startYear)
{         std::cout << i << " " << festivalCount << '\n';}
   }
}
Enter starting year: 1986
Enter ending year: 2007
1986 10
1990 11
1994 12
1998 13
2002 14
2006 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
	int m {}, n {};

	std::cout << "Enter first given year: ";
	std::cin >> m;

	std::cout << "Enter second given year: ";
	std::cin >> n;

	for (int x = 1950, y = 0; x <= n; ++x)
		if (((1950 - x) % 4 == 0) && ++y && x > m)
			std::cout << x << " " << y << '\n';
}



Enter first given year: 1984
Enter second given year: 2007
1986 10
1990 11
1994 12
1998 13
2002 14
2006 15

Thank you, I worked it out. And thank you for mentioning that, my teacher isn’t really teaching those things and never mentioned it so I am glad you told me that it is important, I will be sure to learn those!
Krakenvangh wrote:
my teacher isn’t really teaching those things and never mentioned it

A free online C++ tutorial might be helpful then.

https://www.learncpp.com/
Topic archived. No new replies allowed.