Having a problem with pass by refrence.

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Having a problem with pass by refrence.

Postby Jenn76 » Tue Sep 30, 2003 7:44 am

I cannot get the last function to work. I need to use pass by refrence to calcuate the quotient and modulus in one funtion.

output

21 + 10 = 31
21 - 10 = 11
21 * 10 = 210
21 / 10 = 2 modulus 1


#include <iostream>
#include <iomanip>
using namespace std;

// Prorotype Declarations
int add (int a, int b);
int subt (int a, int b);
int prod (int a, int b);
void division(int& a, int& b);




int main()
{


// User input
cout << "Please enter 2 integer numbers: ";

// Read numbers into a and b
int a;
int b;
cin >> a >> b;

int sum = add (a, b);
int diff = subt (a, b);
int product = prod(a, b);




// Displays output to screen

cout << setw(4) << a <<
" + " << setw(2) << b <<
" = " << setw(4) << sum << endl;

cout << setw(4) << a <<
" - " << setw(2) << b <<
" = " << setw(4) << diff << endl;

cout << setw(4) << a <<
" * " << setw(2) << b <<
" = " << setw(4) << product << endl;






cout << "\n thanks for using my caculator.";
cout << endl;

return 0;
}


// Function adds the numbers
int add (int a, int b)
{
return (a + b );
}


// Function subtracts the numbers
int subt (int a, int b)
{
return (a - b);
}

// Function multiplies the numbers
int prod (int a, int b)
{
return ( a * b);

}

// Function divides numbers
void division(int& a, int& b)
{



}
Jenn76
 

Postby Guest » Tue Sep 30, 2003 7:51 am

i came up with this solution and it worked... would there be a better way to do this.

#include <iostream>
#include <iomanip>
using namespace std;

// Prorotype Declarations
int add (int a, int b);
int subt (int a, int b);
int prod (int a, int b);
void division(int& a, int& b, int& div, int& mod);




int main()
{


// User input
cout << "Please enter 2 integer numbers: ";

// Read numbers into a and b
int a;
int b;
cin >> a >> b;


int div = 0;
int mod = 0;
int sum = add (a, b);
int diff = subt (a, b);
int product = prod(a, b);
division(a, b, div, mod);




// Displays output to screen

cout << setw(4) << a <<
" + " << setw(2) << b <<
" = " << setw(4) << sum << endl;

cout << setw(4) << a <<
" - " << setw(2) << b <<
" = " << setw(4) << diff << endl;

cout << setw(4) << a <<
" * " << setw(2) << b <<
" = " << setw(4) << product << endl;

cout << setw(4) << a <<
" / " << setw(2) << b <<
" = " << setw(4) << div<<
" Modulus " << mod << endl;








cout << "\n thanks for using my caculator.";
cout << endl;

return 0;
}


// Function adds the numbers
int add (int a, int b)
{
return (a + b );
}


// Function subtracts the numbers
int subt (int a, int b)
{
return (a - b);
}

// Function multiplies the numbers
int prod (int a, int b)
{
return ( a * b);

}

// Function divides numbers
void division(int& a, int& b, int& div, int& mod)
{

div = a / b;
mod = a % b;



}
Guest
 

Postby Alvaro » Tue Sep 30, 2003 2:28 pm

void division(int& a, int& b, int& div, int& mod);

Only div and mod need to be references in that function. Othere than that, yes, that is the right thing to do.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 1 guest