Possible While Loop Problem

I am trying to practice doing random c++ programs and in one I came across the problem where my program won't stop accepting inputs. I am using a pearsons site paired with the textbook problem solving with c++ 10th edition and I know my program looks messy, it is just the way the assignment was made, I can not use used defined functions until later. Ill also post my code. Any help is appreciated.

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <iomanip>

using namespace std;

int main()
{
int temp;
int onesDigit;
int tensDigit;
int hundredsDigit;
bool contains147;

cout << "Please type the desired temperature that you would like to use" << endl;
cin >> temp;

int upperTemp = temp;
onesDigit = upperTemp%10;
tensDigit = upperTemp/10%10;
hundredsDigit = upperTemp/100;

if((onesDigit == 1) || (onesDigit == 4) || (onesDigit == 7) ||
(tensDigit == 1) || (tensDigit == 4) || (tensDigit ==7) ||
(hundredsDigit == 1) || (hundredsDigit == 4) || (hundredsDigit == 7))
contains147 = true;
else
contains147 = false;
while (contains147 == true)
{
upperTemp++;
if ((onesDigit == 1) || (onesDigit == 4) || (onesDigit == 7) ||
(tensDigit == 1) || (tensDigit == 4) || (tensDigit ==7) ||
(hundredsDigit) == 1 || (hundredsDigit == 4) || (hundredsDigit == 7))
contains147 = true;
else
contains147 = false;
}
// upperTemp contains the closest upper termperature
//repeat for lower temp
int lowerTemp = temp;
onesDigit = lowerTemp%10;
tensDigit = lowerTemp/10%10;
hundredsDigit = lowerTemp/100;

if((onesDigit == 1) || (onesDigit == 4) || (onesDigit == 7) ||
(tensDigit == 1) || (tensDigit == 4) || (tensDigit ==7) ||
(hundredsDigit == 1) || (hundredsDigit == 4) || (hundredsDigit == 7))
contains147 = true;
else
contains147 = false;
while (contains147 == true)
{
lowerTemp--;
if((onesDigit == 1) || (onesDigit == 4) || (onesDigit == 7) ||
(tensDigit == 1) || (tensDigit == 4) || (tensDigit ==7) ||
(hundredsDigit == 1) || (hundredsDigit == 4) || (hundredsDigit == 7))
contains147 = true;
else
contains147 = false;
}
//lowerTemp now contains closest lower temperature
// output new temperatures
cout << "The lower temperature is: " << lowerTemp << " and the higher temperature is: " << upperTemp << endl;

return 0;
}

The upperTemp++ and lowerTemp-- should iterate through until the value doesnt contain the values 1 4 or 7, the digit values need to stay constant so that it doesn't allow those numbers to show up and outputs the next best number
Last edited on
Once inside your while loop, onesDigit never changes. tensDigit never changes. hundredsDigit never changes.

If they never change, when will the loop stop?
Topic archived. No new replies allowed.