for loop problem

First, I would like to say hi to all members!
I just recently started my adventure with programming in C++ (and programming in general to be precise), and there's my first problem:

I try to use the a very simple FOR loop. It should return numers from 1 to y, but instead it returns 1 infinitely.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{

    int y;
    cout << "Print numbers from 1 to ";cin >> y;cout << endl;

    for(int x = 1 ; x < y ; x=x+1)
    {
        cout << x;
    }
}
It works, but not 1 to y rather, 1 to (y-1).
To make it 1 to y, change the loop condition to x==y

Edit: I meant <=

Aceix.
Last edited on
Yep that's true should be == or <=.
Main problem was something else, but I just managed to solve it: changed from 'debug' to 'release' build. BUT - is it normal and expected?

EDIT: using CODE::BLOCKS 13.12 if matters
Last edited on
no.
The cin command automatically puts a new newline anyway, so you don't need to put the cout >> endl; after the cin >> y;
closed account (LbXjz8AR)
And one more thing. The condition which u gave is mentioning that the loop will work till a point where x<y. So whatever you input, the loop will never display the given input.
Topic archived. No new replies allowed.