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)
{
result_fraction_add = (n_1 * d_2 + d_2 * n_2) / (d_1 * d_2);
}

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_subtraction;
int fraction_multiplication;
int fraction_division;

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

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;
}``````

Edited by rebellion346: n/a

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by rebellion346

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.