How can I apply loop precondition i am a bit confused

I am a beginner so please bare with me.. This is my code I have to get unlimited inputs but when the difference between the two recent one's equal 15 it outputs those two numbers. What should I do this one is wrong!!

#include <iostream>

using namespace std;
int main()
{
int first = 0;
int second = 0;
while (1)
{
cout << "Enter some integers" << endl;
cin >> second;

if (second - first >= 15)

cout << "The difference between " << first << "and " << second << "is greater than or equal to 15." << endl;


first = second;
}

return 0;
}
Last edited on
I have got this will anybody help me in this??

#include <iostream>

using namespace std;
int main()
{
int first = 0;
int second = 0;
while (1)
{
cout << "Enter some integers" << endl;
cin >> second;
if (second < first)
{
swap(first, second);
}

if (second - first >= 15)

cout << "The difference between " << first << "and " << second << " is greater than or equal to 15." << endl;


first = second;

}

return 0;
}
Thanks everyone for not replying I really really appreciate it. Thank you very much.
Then mark your thread as solved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << "enter some number of integers, end input with a non-numeric character:\n" ;

    int first ;
    std::cin >> first ; // read in the first integer

    int second ;

    while( std::cin >> second ) // while second is successfully read
    {
        if( std::abs( first-second ) > 14 ) // http://en.cppreference.com/w/cpp/numeric/math/abs
            std::cout << "The difference between " << first << " and " << second << " is greater than or equal to 15.\n" ;

        first = second ; // next time around the loop, first would be the current second
    }
}
Thanks @JLBroges..
Topic archived. No new replies allowed.