I'm working on a prototype program for my class assignment. I didn't wanna jump into it quite yet until I understood how functions worked so I decided to make this. It's going pretty well and now what I'm trying to do is make it so the program takes user input for n_1, d_1, n_2, and d_2. I was thinking maybe create a function that asks for the user input but I have no idea how I can do that. Any ideas?

#include <iostream>
using namespace std;

int add(int n_1, int d_1, int n_2, int d_2)
{
	int result_fraction_add;
	result_fraction_add = (n_1 * d_2 + d_2 * n_2) / (d_1 * d_2);
	return (result_fraction_add);
}

int subtract(int n_1, int d_1, int n_2, int d_2)
{
	int result_fraction_subtract;
	result_fraction_subtract = (n_1 * d_2 - d_2 * n_2) / (d_1 * d_2);
	return (result_fraction_subtract);
}

int multiply(int n_1, int d_1, int n_2, int d_2)
{
	int result_fraction_multiply;

	result_fraction_multiply = (n_1 * n_2) / (d_1 * d_2);
	return(result_fraction_multiply);
}

int divide(int n_1, int d_1, int n_2, int d_2)
{
	int result_fraction_divide;

	result_fraction_divide = (n_1 * d_2) / (d_1 * n_2);
	return(result_fraction_divide);
}

int main()
{
	int fraction_addition;
	int fraction_subtraction;
	int fraction_multiplication;
	int fraction_division;

	fraction_addition = add(1, 2, 1, 2);
	fraction_subtraction = subtract(1, 2, 1, 2);
	fraction_multiplication = multiply(1, 2, 1, 2);
	fraction_division = divide(1, 2, 1, 2);

	cout << "Result of adding fractions: " << fraction_addition << endl;
	cout << "Result of subtracting fractions: " << fraction_subtraction << endl;
	cout << "Result of multiplying fractions: " << fraction_multiplication << endl;
	cout << "Result of dividing fractions: " << fraction_division << endl << endl;
	
	return 0; 
}

Recommended Answers

All 2 Replies

Just like cout we ave std::cin & the >> operator

int foo;
cin >> foo

wow i feel dumb lol. but thank you, that seems to work just fine without any fancy function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.