I have an assignment due tonight at 12am est, I am completely lost here..... Can anyone point me to the right direction?
"Write a C++ program that inputs two integers then calls a function subtract, use pass by reference and allow the user to change his/her input values, then subtract the two numbers and return the difference."
I do not understand if I am supposed to input the two integers into the actual code, or as for two integers from the user. Also how am I supposed to give the user the opportunity to change his/her values, I was under the assumptions that when you pass by reference, you have the opportunity to swap the two numbers, not completely enter two new numbers
I have came up with this so far.....I dont know how to actually change the values besides swap them....Does this look anything like it is supposed to??
#include <iostream>
usingnamespace std;
int change(int& x, int& y)
{
int temp=0;
cout << "Function(before change) before subtraction: " << x << " " << y << endl;
temp = x;
x = y;
y = temp;
cout << "Function(after change) before subtraction: " << x << " " << y << endl;
}
int main()
{
int x,y;
cout << "Enter two numbers which you would like to subtract. Enter your first number: ";
cin >> x;
cout << "Enter your second number: ";
cin >> y;
cout << "Main (before change) after subtraction: " << x - y << endl;
change(x, y);
cout << "Main (after change) after subtraction: " << x - y << endl;
system("Pause");
return 0;
}
Im still not sure if these is even what i am supposed to be doing, I only swapped out the two values with each other, I am not sure how to completeley allow the user to input two new numbers (this does not make any sense because they already are given the option to input their own numbers, why would they want to change them?)
#include <iostream>
usingnamespace std;
// function prototypes go here
int main()
{
// declare integers, may as well initialize them to 1, can't hurt
// need to declare a variable for user input
char cont = 'y';
while (cont == 'y')
{
// display current values of x, y to the user
// ask them if they want to change the values
// get input from the user
while (input != 'y' && input != 'n') // your choice if you want to include this
{ // this basically clears incorrect input
cin.clear();
cin.ignore(1000, '\n');
cout << "Incorrect input. Would you like to change the values of x and y? (y/n): ";
cin >> input;
}
if (input == 'y')
{
// user wants to change the values, prompt them to do so
// call the function to change the values here
while (cin.fail())
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Incorrect input. Enter value of x and y: ";
// call the function to change the values here
}
}
cout << x << " - " << y << " = " << subtract(x, y) << endl;
// ask the user if he wants to continue processing numbers
// get user's answer via console input
while (cont != 'y' && cont != 'n') // another error checking loop
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Incorrect input. Would you like to continue? (y/n): ";
cin >> cont;
}
}
cin.clear();
cin.ignore(1000, '\n');
cin.get(); // so you can see the end result
return 0;
}
int subtract (constint & x, constint & y) // you'll want const integers for this
{
// you want to return the result of x - y
}
void change_values (int & x, int & y) // const integers would be very bad for this of course
{
// here you want to get console input from the user twice, once for x, once for y
}