There must be a more simple way..

Hello,

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.

thankX,,

AeoN
Ahh, ok so there is a single command 'swap' there..

Thanks garob.

haha, why use pointers when you do not have to..
http://www.cplusplus.com/reference/algorithm/swap/
http://www.cplusplus.com/reference/string/string/

Also, char *x = *&ia; and the one beneath it is technically write, but don't you see that the asterix and ampersand cancel eachother out?
'Also, char *x = *&ia; and the one beneath it is technically write, but don't you see that the asterix and ampersand cancel eachother out?'

I do now, thanks.


should have realized that,, remind me to stop drinking.(!)
Topic archived. No new replies allowed.