Output error when input isn't a number. C++

前端 未结 5 1873
野性不改
野性不改 2021-01-28 07:47

I am making a function that takes a number from the user\'s input and finds the absolute value of it. I want to make it return an error if the user inputs anything other than j

5条回答
  •  佛祖请我去吃肉
    2021-01-28 08:27

    Pass your number as a reference and return an error code. Using the function argument as an output parameter.

    bool parseNumber(int &n)
    {
    ... 
    //assign to number to n
    //   if number parsing is ok return true;
    ...
       return false; 
    }
    
    int main()
    {
       int number=0;
    
       if(!parseNumber(number))
          std::cout << "Number parsing failed\n";
    }
    

提交回复
热议问题