Infinite loop

I want to get an user input as age and add 100 to it and print out how old they will be every year. My code below does this, but is an infinite loop and does not stop after looping 100 times. I am still a beginner so a brief explanation would be really appreciated.

I want it to print out
"In 2021 you will be 20"
In 2022 you will be 21"
etc.. until 100 "years" pass


1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main()
{ int age, x=2020;
 cout<< "How old are you now in 2020? ";
 cin>> age;
 
 do{ cout<< "\nIn "<< x << " you will be " << age;
 	x++;
 	age++;	}while((x<(x+100)) && (age<(x+100)));

return 0;	
}
Step 1 is learn how to indent code.
Pick a style and stick to it -> https://en.wikipedia.org/wiki/Indentation_style
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
  int age, x = 2020;
  cout << "How old are you now in 2020? ";
  cin >> age;

  do {
    cout << "\nIn " << x << " you will be " << age;
    x++;
    age++;
  } while ((x < (x + 100)) && (age < (x + 100)));

  return 0;
}


(x < (x + 100))
If you keep moving the finish line, how do you expect to reach it?

Better variable names also help.
1
2
3
for ( year = 2020 ; year < 2120 ; year++, age++ ) {
    cout << "\nIn " << year << " you will be " << age;
}

It's a pretty simple reason why you loop condition goes on forever. You want to stop after 100 loops, so you're comparing x to x+100. However, you're also incrementing x every loop, so x is ALWAYS less than x +100. Here's what you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main()
{
     int age, x=2020;
     cout<< "How old are you now in 2020? ";
     cin>> age;
 
     for(int i = 0; i <= 100; i++)
    {
          cout<< "\nIn "<< x << " you will be " << age;
 	  x++;
 	  age++;
    }

     return 0;	
}



Also, try to indent and space things out properly.
I didnt realize you could have botth year ++ and age ++ in one for loop condition statement. Thanks for the help
So it is best to use a for loop in this situation instead of do/while?
Use a for loop if you know in advance how many times you want to loop.
- example, looping through an array.

Use a while loop to loop for an as yet unknown number of iterations, including zero.
- example, traversing a linked list (which may be empty).

Use a do-while loop to loop for an as yet unknown number of iterations, at least once.
- example, a menu Q&A where you need to print the choices and evaluate a response.
Last edited on
^^^ Note that any loop can do anything. Its the one that is most readable/ sensible at a place in your code that matters.

x = 0;
while(x!= 0)
{ cin >>x;}

is the same as
do
{cin >> x}
while(x != 0)

but the second is better.
its also the same as
for(x = 0; x!=0; ) cin >> x;

the for loop is smaller, but its also "slightly" confusing. the do-while is the most readable here, but the for look is 'ok' and the seed first then while is .. noobish. Its probably possible to make the for all in one:
for(cin>>x; x; cin>>x); //<- obnoxious.
Last edited on
Topic archived. No new replies allowed.