Tuesday, June 7, 2011

We have been discussing arrays in the past week.
Our assignment was to create a program that took the first and last name of the user and output it in reverse order.



 
   

   
    cout << "please enter your first name: " << endl;
    char str1[30];
    cin >> str1;
   
   
    cout << "please enter your last name: " << endl;
    char str2[20];
    cin >> str2;
   
   
    cout << "Your name before manipulation: " << str1 << " " << str2 << endl;
   
   
    cout << "your name after: " << str2;
    strcpy(str2, str1);
    cout << " " << str2 << endl;
   
   
comments:
I could have eliminated 2 separate lines for the cin input, and just did,
cin >> str1 >> str2
char str1[30];
char str2[25];

or something close to that.
I also feel it kind of redundant the way we have to reverse order.
Cout str2 and then copy the value of str1 into str2 and output it again.
A better way I think is we could just cout >> str2 >> str1
This eliminates extra code, along with if you have to use str2 again latter on the in the program you can use it as the "real" value of str2
   
   
   
   
 

No comments:

Post a Comment