Thread: Passing Values Question...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    27

    Wink Passing Values Question...

    Hi Everyone!
    This is my question...
    The following program segmant asks the user for his or her first name in main(). Write a function that reverses the name in the array so that main() can print the reversed name. To make it easy, assume that the user name is always five characters long ("Jerry" or "Peter", for example).

    Code:
    #include <iostream.h> // I know it's the old method, but that's what's the book writes
    rev(char name[6]);
    
    main()
    {
         char name[6];
         cout << "What is your first name (5 letters, please)? ";
         cin  >> name;
    
         rev(name);
         cout << "Your name spelled backwards is " << name << "\n";
         return 0;
    }
    //from here is what I added
    rev(char name[6])
    {
          name[0] = name[4];
          name[1] = name[3];
          name[3] = name[1];
          name[4] = name[0]; 
          return 0;
    }
    Is there a way to write it with a loop? My way doesn't work--the last character doesn't change
    Thanks all.

    Tinker.

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, that's because by the time you get to the last statement in your rev() function, name[0] has already been changed. It no longer stores the first number in the name. You assigned name[4] to it. This is a very common problem. The simplest way to understand is to make a copy of the string. See if you can't figure it out yourself with that much to work with.

    And yes, there is a way to loop that. And that would probably be best, since it'll make it easier for you to modify later on, so that you can accept any length of name.

    I also notice that you have failed to specify a return value for both of your functions. Not a good idea.
    Last edited by Jaken Veina; 06-25-2005 at 11:29 PM.
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you could just use strrev();
    that would make your life easier no matter what size the
    array was.


    search google for a more indepth example but it use is pretty
    straight forward

    Code:
    char array[50];
    char array2[50];
    array2 = strrev(array);

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ya you could do it in a loop, this is just code off the top of my head, but you could add to it etc...

    Code:
    ...
    ...
    string buffer = "test";
    string bufferReversed;
    
    for (int loop = buffer.size()-1; loop >= 0; loop--)
       bufferReversed += buffer[loop];
    
    return bufferReversed;
    Btw, i know you are working with char arrays, but you could adapt this technique to it, by creating a bufferReversed array with the same size as buffer, and then instead of += have there be an extra incrementing variable inside the loop so bufferReversed goes up from 0 to the size and buffer goes from size down to zero (index locations), or you could have bufferReversed index be the size - the loop, because starting would be 0 (size - loop, with loop being the size).

    so it would look like this:
    bufferReversed[size-loop] = buffer[loop];
    Last edited by JoshR; 06-26-2005 at 12:55 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    My way doesn't work--the last character doesn't change
    Code:
    //switching two numbers:
    
    int a = 10;
    int b  = 20;
    Get a piece of paper and make two columns:
    Code:
     a=          b=
    ----        ----
    Start off by filling in the starting values for 'a' and 'b':
    Code:
     a=          b=
    ----        ----
     10          20
    Now, if you add this line:
    Code:
    //switching two numbers:
    
    int a = 10;
    int b  = 20;
    
    a = b;
    What does 'a' equal? Cross out the starting value for 'a', and write down the new value for 'a'.

    Now, add this line:
    Code:
    //switching two numbers:
    
    int a = 10;
    int b  = 20;
    
    a = b;
    b = a;
    What does b equal? Look at your paper. Cross out the old value for 'b', and write down the new value. What are the ending values for 'a' and 'b'? Why didn't the numbers get switched?
    Last edited by 7stud; 06-26-2005 at 02:41 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    27

    How about a loop?

    I see...It's seems as if I need a third variable. mmmm...... How do I do it with a loop?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Well you're basically doing two set's of variable swaps, name[0] with name[4] and name[1] with name[3]. See if you can figure out how to swap one variable with another outside a loop, and then try to write the swap inside a loop so you can swap as many variables as you want.
    You're only born perfect.

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream.h> // I know it's the old method, but that's what's the book writes
    rev(char name[6]);
    
    main()
    {
         char name[6];
         cout << "What is your first name (5 letters, please)? ";
         cin  >> name;
    
         rev(name);
         cout << "Your name spelled backwards is " << name << "\n";
         return 0;
    }
    //from here is what I added
    rev(char name[6])
    {
          char backup = name[0];
          name[0] = name[4];
          name[1] = name[3];
          name[3] = name[1];
          name[4] = name[0]; 
          name[4] = backup;
          return 0;
    }
    Last edited by ILoveVectors; 06-27-2005 at 10:36 AM.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by ILoveVectors
    Code:
    #include <iostream.h> // I know it's the old method, but that's what's the book writes
    rev(char name[6]);
    
    main()
    {
         char name[6];
         cout << "What is your first name (5 letters, please)? ";
         cin  >> name;
    
         rev(name);
         cout << "Your name spelled backwards is " << name << "\n";
         return 0;
    }
    //from here is what I added
    rev(char name[6])
    {
          char backup = name[0];
          name[0] = name[4];
          name[1] = name[3];
          name[3] = name[1];
          name[4] = name[0]; 
          name[4] = backup;
          return 0;
    }
    Check your code
    This program is still buggy
    name[1] is overwirtten by name[3] before it's copied.

    Code:
    rev(char name[6])
    {
          char backup = name[0];
          name[0] = name[4];
          name[4] = backup;
    
          backup = name[1];
          name[1] = name[3];
          name[3] = backup;
     
    
          return 0;
    }
    This should work

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Hi Tinkerbell,

    Firstly, you are using deprecated headers.
    It should be:

    Code:
    #include <iostream>
    using namespace std;
    If your teacher/lecturer tells you otherwise he needs to
    be corrected. Why not be the one to correct him.

    Secondly, you asked what is a loop?

    A loop or "for loop" is vitally important in programming.
    Let's say you wanted your program to count from 1 to 1000. Now you could do this:

    Code:
    cout<<1;
    cout<<2;
    cout<<3
    .
    .
    .
    cout<<1000;
    This is obviously tedious? There is a much quicker way which involves using a for loop.

    The way you write it is as follows:
    Code:
    for(int a=0; a<1000; a++)
    {
    cout<<a;
    }
    The important bits.
    Now the "int a=0;" is your starting value.

    The "a<1000" is your end value.

    And the 'a++' tells you how much to add to the previous value. It is similar to saying a= a+1.

    So if you wanted to count up in tens you would write:
    Code:
    for(int a=0; a<1000; a=a+10)
    {
    cout<<a;
    }
    So how does this help you with your example?
    Well you can use the for loop as a pointer to the position in your array.

    So how would you print out the contents of your array, from [0] to [5]?

    for(int a=0; a<6; a++)
    {
    cout<<name[a];
    }

    Ok now all you have to do is figure out what you need to change to print out the array backwards. What would be the start value, the end value and how would the
    a++ differ?

    Have a go.

    Once you've done that then read up on functions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  2. quick question about storing values in an array
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-10-2004, 01:33 PM
  3. Passing Template values on to other classes
    By vinsta18 in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2004, 05:26 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Newbie Question: Passing a File
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 06:17 PM