Can't manage it to work...

I have recently started with c++ programming.
I am Reading a book about the subject and only have done this for 3 Days.
So please dont judge me. I know this problem is easy to solve.

I am writng a program which should be capable of Printing out all variabels
between the starting value and the end value. both values is selected by the
user. however, when i input a starting and an ending value, it then prints out the starting value.
here is the code:

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Input any StartValue."<<endl;
char check_ing;
float x;
cin>> x;
cout<<"Input any EndValue"<<endl;
float y;
cin>> y;
if (x<y)
{ cout<<x<<endl;
x=x+1;}
if (x>y)
{ cout<<x<<endl;
x=x-1;}


return 0;
}
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
    int x;
    int y;
    cout<<"Enter a starting value: ";
    cin>>x;
    cout<<"Enter an end value:  ";
    cin>>y;

    for(int c=x; c<=y;c++){
        cout<<c <<endl;
    }

    return 0;
}
In order to solve it you need a loop I.E 'for'. If statements only output one value.
Last edited on
Try the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ( x < y )
{
   while ( x <= y )
   {
      cout << x << endl;
      x++;
   }
}
else
{
   while ( y <= x )
   {
      cout << y << endl;
      y++;
   }
}   
Thank you! it helped me out.
Topic archived. No new replies allowed.