1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
// Ex4_07.cpp
// Initializing pointers with strings
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void Swap(char **S1, char **S2)
{
cout << "\tSwapped " << *S1 << " and " << *S2 << endl << endl;
char *B(*S1);
*S1 = *S2;
*S2 = B;
}
void Print(char** start, char **last)
{
while(start != last)
cout << *start++ << ", ";
cout << endl << endl;
}
int main()
{
char* pstr[] = {"Robert Redford","Hopalong Cassidy", "Lassie",
"Slim Pickens",
"Boris Karloff",
"Oliver Hardy" };// Initializing a pointer array
char* pstart("Your lucky star is ");
//char *temp;
int dice(0);
cout << endl
<< "Pick a lucky star!"
<< "Enter a number between 1 and 6: ";
cin >> dice;
cout << endl;
if(dice >= 1 && dice <= 6){ // Check input validity
cout << pstart << pstr[dice - 1]; // Output star name
}
else{
cout << "Sorry, you haven't got a lucky star."; // Invalid input
}
cout << endl << endl;
Print(pstr, pstr+6);//Initial is unchanged
Swap(pstr+1, pstr+3);//Swap 2nd and 4th persons
Print(pstr, pstr+6);
Swap(pstr, pstr+3);//Swapped 1st and 4th persons
Print(pstr, pstr+6);
Swap(pstr+2, pstr+5);//Swapped 3rd and last persons
Print(pstr, pstr+6);
return 0;
}
|