Hi guys, can someone lend me a hand in the problem below? I'm having difficulties in making the program below.
Build software that sets two numbers, 20 and 10. The software must display the sum, difference, product, and quotient of the provided integers. For this activity, a non-returning function with arguments should be used.
One approach is to use a try/catch while throwing a runtime error of the results. This isn't the most efficient way of doing so, but it would look like:
#include <iostream>
#include <string>
#include <stdexcept>
void PrintResults() {
int x{ 20 }, y{ 10 };
int sum{ x + y };
int difference{ x - y };
int product{ x * y };
int quotient{ x / y };
std::string throwStr;
throwStr.append("Sum: ").append(std::to_string(sum)).append("\n");
throwStr.append("Difference: ").append(std::to_string(difference)).append("\n");
throwStr.append("Product: ").append(std::to_string(product)).append("\n");
throwStr.append("Quotient: ").append(std::to_string(quotient)).append("\n");
throw std::runtime_error(throwStr);
}
int main()
{
try {
PrintResults();
}
catch (const std::exception& e) {
std::cout << e.what() << "\n";
}
}
There are other ways, and it really depends on what context your assignment is in. A word though, there may be more obvious ways that your assignment is looking for in particular, like signal handling and the like
EDIT: if it's any regular non returning type, you could just output those to the console in that case. "void" is a non-returning type.
So really you could just do:
1 2 3 4 5 6 7 8 9 10 11 12
void PrintResults() {
int x{ 20 }, y{ 10 };
int sum{ x + y };
int difference{ x - y };
int product{ x * y };
int quotient{ x / y };
std::cout << "Sum :" << std::to_string(sum) << "\n";
std::cout << "Difference:" << std::to_string(difference) << "\n";
std::cout << "Product:" << std::to_string(product) << "\n";
std::cout << "Quotient:" << std::to_string(quotient) << "\n";
}
Regardless though, I think @keskiverto's response will do you more help than anything here
My edit is after seeing what they linked and realizing I overthought your question lmao
...yeah, don’t do it like that. If I were your professor I’d fail you on that one.
But the idea isn’t actually that far off. Your professor just wants to see that you can create and assign values to some integer variables, then pass them to a function which does the stuff you would have otherwise done in main().
For example, here I create a string variable, assign it value, and pass it to a function that does not return a value:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
void hello( std::string whom )
{
std::cout << "Hello " << whom << "!\n";
}
int main()
{
string name = "SpongeBob";
hello( name );
}
I assume that “non-returning function” means that the function is not meant to return a value. (That is, its return type is void.) It still returns execution to the caller, though.
A true non-returning function does not return to the caller. Instead it explicitly terminates the program with something like exit().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <cstdlib>
#include <iostream>
#include <string>
void hello( std::string name )
{
std::cout << "Hello " << name << "!\n";
std::exit( 0 ); // the program ends here; it does not return to main()
}
int main()
{
hello( "Patrick" );
hello( "Sandy" ); // this line never gets called, because the last call to hello() ended the program
}
I would be surprised if this was what your professor meant by “non-returning”. Alas, vocabulary matters.
@Duthomhas that's entirely fair, I may not be qualified to give legitimate concise answers given I'm still learning the language at a self taught pace 😅 in that regard, thank you for your feedback on that post
@ForgottenLaw, I'd also go with the above explanation as mine is pretty out there
#include <iostream>
void justDoIt( int a, int b )
{
std::cout << a + b << '\n'
<< a - b << '\n'
<< a * b << '\n'
<< ( a + 0.0 ) / b << '\n';
}
int main()
{
justDoIt( 20, 10 );
}
I definitely overlooked the "sets" bit in OP's question 😅 but I take the novelty as a compliment - I've found trying "outside the box" type of stuff has always been a fun approach in learning. Although, if this is actually for an assignment, it's probably not the appropriate approach to use
Although, if this is actually for an assignment, it's probably not the appropriate approach to use
As the saying goes...'Don't try this for production code'.
Experimenting with doing things 'differently' can be a useful learning tool. PROVIDED that the limitations of the expt are known and understood and the 'expected/good practice' method is also known.
This is a very useful debugging technique too — use it to abort your program at any spot by simply throwing an error. I tend to write an “error” function that takes parameter pack arguments, converts them all to string via an ostringstream, and throws the resulting string as a runtime exception:
if (fooey) error( "Quux is ", quux.values(), " when it should be non-manifold." );
I tend to write an “error” function that takes parameter pack arguments, converts them all to string via an ostringstream, and throws the resulting string as a runtime exception:
IMO, it makes more sense to just return the exception object rather than throw it inside the function.
Depending on the function's return type, throwing immediately might preclude its use in conditional expressions like cond? throw error(a, b, c): x (if the return type is void, for example), or mislead about the type or identity of the exception being thrown.
So maybe something like this:
1 2 3 4 5 6
struct my_error: public std::runtime_error { using std::runtime_error::runtime_error; };
template <typename E = my_error, typename... Args>
[[nodiscard]] my_error error(Args&&... args)
{
return my_error((std::ostringstream{} << ... << static_cast<Args&&>(args)).str());
}
Just remember that 'throwing' is 'expensive' in terms of performance... That's one reason why it's usually restricted to 'genuine' exceptions and not 'expected' validation issues.