mercredi 23 juin 2021

Division Function w/ Multiple Return Possibilities

I'm working on a school assignment, but I'm having trouble with a problem.

The problem is as follows:

"Write a function called divideIt that takes two integers, divides them, and returns the result as a float. Have this function skip division when it encounters an unacceptable value, and also retain the decimals. Do you know what to quit on?"

With the code I currently have, it divides and retains the decimal like the problem states, but I don't understand how to skip division. If I skip division because of an unacceptable value, what do I place in return? Should I just use null? Also, I'm not sure about the "Do you know what to quit on?" line.

My Code:

#include <iostream>


using std::cout;
using std::cin;
using std::endl;


//Prototype

float divideIt(int num1, int num2);

int main()
{
    int num1, num2;
    float finalResult;
    finalResult = divideIt(num1, num2);
    cout << "Result: " << finalResult << endl;
}

//Header

float divideIt(int num1, int num2)
{
    cout << "Simple Division\n" << "Input first integer: ";
    cin >> num1;
    cout << "Input second integer: ";
    cin >> num2;
    if(num2 == 0) //I wanted to make ASCII values unacceptable, but because they automatically translated into zeros, so I used that to my advantage on num2.
    {
        cout << "The second value was undefined or unacceptable." << endl;
    }
    return static_cast<float>(num1) / num2;
}

Aucun commentaire:

Enregistrer un commentaire