my teacher asked me to swap to values using two variables, and a temporary variable,im not sure how to "swap " them, im kinda stuck and not sure what i should do next...
#include <iostream>
using namespace std;
int main()
{
char response;
double temp = X;
double numberx = X;
double numbery = Y;
cout << " enter a numberx: ";
cin >> numberx;
cout << " enter another numbery: ";
cin >> numbery;
cout << " X = " << numberx << endl;
cout << " Y = " << numbery << endl;
cout << " lets swap the two vaules! " << endl;
cin >> response;
return 0;
}
If you look at the way std::swap is implemented, it gives you a good clue to the answer. (I trimmed out more advanced complexities like move(), noexcept(), and templates b/c you can ignore them at this stage of learning):
1 2 3 4 5 6
void swap(Type& a, Type& b)
{
Type temp = a;
a = b;
b = temp;
}
Just understand that "Type" represents some unspecified data-type. The key point to notice is that when a variable is assigned to a value, including another variable, it's previous value is overwritten, that is why the temporary is needed as a "placeholder". You should be able to deduce the rest from here ;).