函数的问题,以确定int是否包含某个数字 我正在C++中做作业。这是我的任务:

函数的问题,以确定int是否包含某个数字 我正在C++中做作业。这是我的任务:,c++,C++,烤箱上的键盘用于输入所需的烘焙温度,其排列方式与手机上的数字类似。不幸的是,数字1、4和7不起作用。 当配方要求输入无法输入的温度时(由于按键不起作用),您需要替换可以输入的温度 编写一个程序,将所需温度作为用户的输入。温度必须在0到999度之间 如果所需温度不包含1、4或7,则显示所需温度。如果包含1、4或7,则计算并显示不包含1、4或7的下一个最大温度 例如,如果所需温度为450,则程序应输出500。同样,如果所需温度为375,则程序应输出380 在您的程序中,包括并使用一个名为“conta

烤箱上的键盘用于输入所需的烘焙温度,其排列方式与手机上的数字类似。不幸的是,数字1、4和7不起作用。
当配方要求输入无法输入的温度时(由于按键不起作用),您需要替换可以输入的温度

编写一个程序,将所需温度作为用户的输入。温度必须在0到999度之间

如果所需温度不包含1、4或7,则显示所需温度。如果包含1、4或7,则计算并显示不包含1、4或7的下一个最大温度

例如,如果所需温度为450,则程序应输出500。同样,如果所需温度为375,则程序应输出380

在您的程序中,包括并使用一个名为“containsDigit”的函数,声明如下。如果输入的数字包含数字,则函数应返回true。否则,函数将返回false

我的问题是,有没有一种方法可以编写一个函数来搜索输入中的1,4,7或某个值?
我是初学者。
它还没有完成,我正试图一个接一个地写每一节,这就是我被卡住的地方

这是我到目前为止所拥有的

// Module1assignment.cpp : Defines the entry point for the console application.//

#include "stdafx.h"
#include<iostream>

using namespace std;

int number;
int digits;
int temp;




bool containsDigits(int number, int digits)

{
    if (bool containsDigits = false)
        cout << "Please enter a valid number" << endl;
        cin >> temp;
}


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Please enter the temperature." << endl;
    cin >> temp;

    //This is to make sure the users enters the temp within the desired range//

    if (temp < 0 && temp >999)
    {
        bool containsDigits(int number, int digits);
        cout << "Please enter a valid temperature. " << endl;
        cin >> temp;
     }
    //This is where I want to write a function to search the input for a 1,4and,7//
    //My ideal is if digit = 1 then it changes to 2 if digit = 4 then it changes to 5
    and if digit =7 it changes to 8. That is my ideal. Is it possible to do this 
    without dividing the input into three separate inputs//

    else if (temp )
    
    


    return 0;
}
//Module1assignment.cpp:定义控制台应用程序的入口点//
#包括“stdafx.h”
#包括
使用名称空间std;
整数;
整数位数;
内部温度;
布尔包含数字(整数,整数位数)
{
if(bool containsDigits=false)
库特温度;
}
int _tmain(int argc,_TCHAR*argv[]
{
库特温度;
//这是为了确保用户输入的温度在所需范围内//
如果(温度<0&&temp>999)
{
布尔包含数字(整数、整数位数);
库特温度;
}
//这就是我想写一个函数来搜索1,4和7的输入//
//我的理想是,如果数字=1,那么它变为2,如果数字=4,那么它变为5
如果数字=7,它会变成8。这是我的理想。有可能做到吗
无需将输入分成三个单独的输入//
否则,如果(临时)
返回0;
}
以下是我的做法:

/**this function should check wheter number have the digit digit**/
bool containsDigit(int number, int digit) 
{
    //let's say number is 982, then...
    int a = number/100; //a is 9
    int b = (number/10)%10; //b is 8
    int c = number%10; //c is 2
    if (a==digit || b==digit || c==digit)
        return true;
    else
        return false;
}
那么,大体上,你应该:

cout << "Please enter the temperature." << endl;
cin >> temp;

//This is to make sure the users enters the temp within the desired range//

if (temp < 0 && temp >999)
{
    bool containsDigits(int number, int digits);
    cout << "Please enter a valid temperature. " << endl;
    cin >> temp;
 }

 while(containsDigit(temp, 1) || containsDigit(temp, 4) || containsDigit(temp, 7))
     temp++; //keeps incrementing temp until it doesn't contain 1 ,4 nor 7

 cout << "The right temperature is :" << temp;
cout-temp;
//这是为了确保用户输入的温度在所需范围内//
如果(温度<0&&temp>999)
{
布尔包含数字(整数、整数位数);
库特温度;
}
while(containsDigit(temp,1)| | containsDigit(temp,4)| | containsDigit(temp,7))
temp++//不断增加温度,直到它不包含1、4或7

cout我将int转换为字符串,然后更改1->2,4->5,7->8

bool isSpoiltDigit (char digit){

    return digit == '1' || digit =='2' || digit =='3';
}

char convertToNextHighest (char input){

      switch (input){
         case '1': return '2';
         case '4': return '5';
         case '5': return '8'; 
}

int convertToNextHighest (int temperature){

   ostringstream oss;
   oss<< temperature;

   string s = oss.str();
   size_t size = s.size();

   if (size ==1){
      s[0] = convertToNextHighest (s[0]);
      return stoi (s);
   }else if (size ==2){

      if ( isSpoiltDigit (s[0])){ 
          s[0] =  convertToNextHighest (s[0]);
          s[1] = '0';
      }else {
          s[1] = convertToNextHighest (s[1]);
      }
      int result = stoi(s);
      return result;
   }else { //if (size ==3) 

      if ( isSpoiltDigit (s[0])){ 

          s[0] =  convertToNextHighest (s[0]);
          s[1] = '0';
          s[2] = '0';
      }else if  isSpoiltDigit (s[1])){ 

          s[1] =  convertToNextHighest (s[1]);
          s[2] = '0'; 
      } else {
          s[2] = convertToNextHighest (s[1]);

      }
      int result = stoi(s);
      return result;
   } 
bool-isSpoiltDigit(字符数字){
返回数字=='1'| |数字=='2'| |数字=='3';
}
char convertToNextHighest(字符输入){
开关(输入){
案例“1”:返回“2”;
案例“4”:返回“5”;
案例“5”:返回“8”;
}
int CONVERTTONEXT最高(int温度){
ostringstream oss;

OSS到目前为止您尝试了什么逻辑?所以这里不是为您编写代码的。在我提到的注释中,我想让if digit=1然后digit=2,但我的意思是,如果it=1,则更改为0,对于4和7,仍然更改为下一个数字4和8。我只需要知道是否有一些东西可以用于搜索某些数字。将值转换为string,搜索单个字符。从一个简单的函数开始,比如一个取一个数字并返回该数字加1的函数。但首先,从一开始就阅读你的书,然后做你跳过的所有练习。这是错误的。如果temp为199,这将生成299,而所需的输出是200。@LGenzelis,这是正确的。我已经重置了将所有子编号设置为“0”。