C Programming The keypad on your laboratory oven is used to
C++ Programming :
The keypad on your laboratory oven is used to enter the desired temperature and is arranged like the digits on a phone
Unfortunately the circuitry is damaged and the digits in the leftmost column no longer function. In other words 1, 4 and 7 do not work. If an experiment or procedure calls for a temperature that can\'t be entered, then you would like to substitute a temperature that can be entered.
Write a program that prompts the user for a desired temperature. The temperature must be between 0 and 999 degrees Fahrenheit. If the desired temperature does not contain 1, 4 or 7, then output the desired temperature. Otherwise, compute the next largest and the next smallest temperature that do not contain 1, 4 or 7 and output both.
For example, if the desired temperature is 450, then the program should output 399 and 500. Similarly, if the desired temperature is 375, then the program should output 380 and 369.
The user should be allowed to enter as many temperatures as desired. Temperatures should be validated.
SAMPLE OUTPUT:
Enter desired temperature =>450
The next smallest temperature = 399
The next highest temperature = 500
Enter another temperature? (press y for yes) : y
Enter desired temperature =>350
350 is valid for oven
Enter another temperature? (press y for yes) : y
Enter desired temperature =>1023
Please re-enter temperature. (0-999)=>999
999 is valid for oven
Enter another temperature? (press y for yes) : y
Enter desired temperature =>970
The next smallest temperature = 969
The next highest temperature = 980
Enter another temperature? (press y for yes) : y
Enter desired temperature =>111
The next smallest temperature = 99
The next highest temperature = 200
Enter another temperature? (press y for yes) : y
Enter desired temperature =>375
The next smallest temperature = 369
The next highest temperature = 380
Enter another temperature? (press y for yes) : n
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 0 |
Solution
// C++ code
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
bool getcheck147(int digit_ONES, int digit_TENS, int digit_HUNDREDS)
{
if ((digit_ONES==1) || (digit_ONES ==4) || (digit_ONES ==7) ||(digit_TENS==1) || (digit_TENS ==4) || (digit_TENS ==7) ||(digit_HUNDREDS==1) || (digit_HUNDREDS ==4) || (digit_HUNDREDS ==7))
return true;
else
return false;
}
int main()
{
while(true)
{
int input_Temperature, digit_ONES, digit_TENS, digit_HUNDREDS;
bool check147, input;
cout << \"Enter desired temperature => \";
while(true)
{
cin >> input_Temperature;
if(input_Temperature >= 0 && input_Temperature <= 999)
break;
else
cout << \"Please re-enter temperature. (0-999)=> \";
}
int lower_Temperature = input_Temperature;
int higher_Temperature = input_Temperature;
digit_ONES = lower_Temperature % 10;
digit_TENS = (lower_Temperature/10) % 10;
digit_HUNDREDS = (lower_Temperature/100);
check147 = getcheck147(digit_ONES,digit_TENS,digit_HUNDREDS);
while (check147)
{
lower_Temperature--;
digit_ONES = lower_Temperature % 10;
digit_TENS = (lower_Temperature/10) % 10;
digit_HUNDREDS = (lower_Temperature/100);
check147 = getcheck147(digit_ONES,digit_TENS,digit_HUNDREDS);
}
digit_ONES = higher_Temperature % 10;
digit_TENS = (higher_Temperature/10) % 10;
digit_HUNDREDS = (higher_Temperature/100);
check147 = getcheck147(digit_ONES,digit_TENS,digit_HUNDREDS);
while (check147)
{
higher_Temperature++;
digit_ONES = higher_Temperature % 10;
digit_TENS = (higher_Temperature/10) % 10;
digit_HUNDREDS = (higher_Temperature/100);
check147 = getcheck147(digit_ONES,digit_TENS,digit_HUNDREDS);
}
if(lower_Temperature != higher_Temperature)
{
cout << \"The next smallest temperature = \" << lower_Temperature << endl;
cout << \"The next highest temperature = \" << higher_Temperature << endl;
}
else
{
cout << lower_Temperature << \" is valid for oven\ \";
}
char again;
cout << \"Enter another temperature? (press y for yes): \";
cin >> again;
if(again == \'n\')
break;
}
return 0;
}
/*
output:
Enter desired temperature => 450
The next smallest temperature = 399
The next highest temperature = 500
Enter another temperature? (press y for yes): y
Enter desired temperature => 250
250 is valid for oven
Enter another temperature? (press y for yes): y
Enter desired temperature => 1023
Please re-enter temperature. (0-999)=> 999
999 is valid for oven
Enter another temperature? (press y for yes): y
Enter desired temperature => 970
The next smallest temperature = 969
The next highest temperature = 980
Enter another temperature? (press y for yes): y
Enter desired temperature => 111
The next smallest temperature = 99
The next highest temperature = 200
Enter another temperature? (press y for yes): y
Enter desired temperature => 375
The next smallest temperature = 369
The next highest temperature = 380
Enter another temperature? (press y for yes): n
*/


