New to C++ and appreciate this site. I attempted to write a program that asks the user to enter in two different words, then calls a function which swaps the two char values (strings).
here is the version I wrote in VS C++;
#include <iostream>
using namespace std;
void swap(char *&st1, char *&st2);
int main() {
char ia[30];
char ib[30];
cout << "Enter in first word:";
cin >> ia;
cout <<'\n'<< "Enter in second word:";
cin >> ib;
char *x=*&ia;
char *y=*&ib;
cout << '\n' << "Ok, the first word x=" << x << " , and the the second word y=" << y <<'\n';
swap(x,y);
cout << "Now the swap function has been called and switched the values located and the addresses of x and y, so x now equals=" << x << " and y now equals=" << y << '\n';
system("PAUSE");
return 0;
}
void swap(char* &st1, char* &st2) {
char *tmp;
tmp = st1;
st1= st2;
st2 = tmp;
}
Now this program works, but I am sure there is a better way of accomplishing the same result without having to use pointers to addresses.
feel free to rip me apart, as it is the only way I will learn.