English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

its ment to have the user input a fraction and return simplified or mixed fraction.


#include
using namespace std;

void mixedFraction (int w, int n, int d);

void main ()
{
int w = -1, n = -1,d = -1;

mixedFraction ( w, n, d );
cout << w << n << '/' << d << endl;

mixedFraction (w, n, d );
cout << w << n << '/' << d << endl;

mixedFraction (w, n, d );
cout << w << n << '/' << d << endl;

mixedFraction (w, n, d );
cout << w << n << '/' << d << endl;

return;
}



void mixedFraction (int w, int n, int d)
{
cout << "Enter numerator";
cin >> n;
cout << "Enter denominator";
cin >> d;

cout << "Enter whole number";
cin >> w;

int wholeNumber = 0;
cin >> w;

cout << "Enter numerrator (>= 0)";
cin >> n;

cout << "Enter denominator";
cin >> d;

}

2007-03-23 13:43:19 · 2 answers · asked by Peter G 1 in Computers & Internet Programming & Design

2 answers

As per the previous answer, you are making a copy of each variable, and sending it to the function. Changes made to those copies never make it back to the original values in main().

But also- aren't you programming in c++ ? (cin, cout commands)

If so, then consider creating a CLASS with w, n, and d as public variables. That way, functions within the class can modify these variables directly.

Otherwise, you are reduced to passing "pointers" to variables, as the other answer said. This is the older, non-c++ way to pass variables.

2007-03-23 16:58:38 · answer #1 · answered by Alan 6 · 0 0

No offense but what do you expect? you're not doing any calculations at all. You're just asking for input and spitting it back out, and not even that. mixedFraction should be this:

void mixedFraction (int& w, int& n, int& d)

so that w, n, and d are returned to main so they can be properly displayed.

2007-03-23 23:12:17 · answer #2 · answered by icnintel 4 · 0 0

fedest.com, questions and answers