Need Help Rotating these values.

This is a discussion on Need Help Rotating these values. within the C++ Programming forums, part of the General Programming Boards category; My goal with this program is to get input for four values. (A, B , C , D) and just ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Need Help Rotating these values.

    My goal with this program is to get input for four values.
    (A, B , C , D) and just rotate them so that:
    A ----> B
    B ----> C
    C-----> D
    D-----> A
    However in my program, all my code appears correct to me, And yet it keeps messing up the roation of the variables.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <string>
    #include <iomanip>
    
    
    using namespace std;
    
    void screenHeader();
    void getInput(int &a, int &b, int &c, int &d);
    void sendInput(int &a, int &b, int &c, int &d);
    void doRotation(int &a, int &b, int &c, int &d);
    void finalOutput(int a, int b,int c,int d);
    
    
    int main()
    {
     int a, b, c, d;
     system("clear");
     screenHeader();
     getInput(a, b, c, d);
     doRotation(a, b, c, d);
     finalOutput(a, b, c, d); 
     
     return 0;
    }
    
    
    
    
    void screenHeader()
     {
       cout << "------------------------------------------------------------" << endl;
       cout << "------------------------------------------------------------" << endl;
       cout << "--------------------Rotation Program------------------------" << endl;
       cout << "------------------------------------------------------------" << endl;
       cout << "    The purpose of this program is to let the user(you)input" << endl;
       cout << " four values for the variables (A, B, C, D) when you input"   << endl;                                      
       cout << " the numerical value for each of these letters the program"   << endl;
       cout << " will then proceed to rotate the values and assign them to"   << endl;
       cout << " the following variable.                     " << endl;                                            
       cout << "-----------------------Example------------------------------" << endl;
       cout << "                     A -----> B                             " << endl; 
       cout << "                     B -----> C                " << endl;
       cout << "                     C -----> D                " << endl;
       cout << "                     D -----> A                " << endl;
       cout << "------------------------------------------------------------" << endl;
       cout << "------------------------------------------------------------" << endl;
       cout << "------------------------------------------------------------" << endl;
     }
     
    
    
    
        void getInput(int &a, int &b, int &c, int &d)
           {
          cout << "Please enter your input for the A variable."<< endl;   
          cin  >> a;
          cout << "Please enter your input for the B variable." << endl;
          cin  >> b;
          cout << "Please enter your input for the C variable." << endl;
          cin  >> c;
          cout << "Please enter your input for the D variable " << endl;
          cin  >> d;
          sendInput(a, b, c, d);
          }
    
    
        void sendInput(int &a, int &b, int &c, int &d)
           {
        cout << "You have entered: " << a << " for theA variable." << endl;
        cout << "You have entered: " << b << " for the B variable." << endl;
        cout << "You have entered: " << c << " for the C variable." << endl;
        cout << "You have entered: " << d << " for the D variable." << endl;
           }
    
         void doRotation( int &a, int &b, int &c, int &d)
           {
         int temp;
        temp =a;
         a = b ;
         temp =b;
        b = c ;
        temp =c;
         c = d ;
        temp =d;
         d = a ;
         
          }
         void finalOutput(int a,int b,int c,int d)
           {
        cout << "-----------------------------" << endl;
        cout << "-----------------------------" << endl;
        cout << "Your new value for A is now: " << a << endl;
        cout << "Your new value for B is now: " << b << endl;
        cout << "Your new value for C is now: " << c << endl;
        cout << "Your new value for D is now: " << d << endl;
        cout << "----------------------------"  << endl; 
        cout << "----------------------------"  << endl;
          }

    My output keeps giving me this:

    Code:
    ------------------------------------------------------------
    ------------------------------------------------------------
    --------------------Rotation Program------------------------
    ------------------------------------------------------------
        The purpose of this program is to let the user(you)input
     four values for the variables (A, B, C, D) when you input
     the numerical value for each of these letters the program
     will then proceed to rotate the values and assign them to
     the following variable.
    -----------------------Example------------------------------
                         A -----> B
                         B -----> C
                         C -----> D
                         D -----> A
    ------------------------------------------------------------
    ------------------------------------------------------------
    ------------------------------------------------------------
    Please enter your input for the A variable.
    1
    Please enter your input for the B variable.
    2
    Please enter your input for the C variable.
    3
    Please enter your input for the D variable
    4
    You have entered: 1 for the A variable.
    You have entered: 2 for the B variable.
    You have entered: 3 for the C variable.
    You have entered: 4 for the D variable.
    -----------------------------
    -----------------------------
    Your new value for A is now: 2
    Your new value for B is now: 2
    Your new value for C is now: 3
    Your new value for D is now: 4
    ----------------------------
    ----------------------------

    Any suggestions on what could be messing this up would be greatly appreciated. Thanks.

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    First...This is the output I get with your code
    Code:
    You have entered: 1 for theA variable.
    You have entered: 2 for the B variable.
    You have entered: 3 for the C variable.
    You have entered: 4 for the D variable.
    -----------------------------
    -----------------------------
    Your new value for A is now: 2
    Your new value for B is now: 3
    Your new value for C is now: 4
    Your new value for D is now: 2
    And this is why....
    Code:
    void doRotation( int &a, int &b, int &c, int &d)
     {
         int temp;
        temp =a;
        a = b ;
        temp =b;
        b = c ;
        temp =c;
        c = d ;
        temp =d;
        d = a ; <--- a has already been assigned to b, 
                     so in effect you're saying d = the original value of b
    }
    Additionally, replacing temp with each variable will create another logic error. Think about what d should equal when all the swapping is complete.
    Last edited by Scribbler; 02-15-2005 at 08:32 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It looks like you are rotating the wrong direction, but assuming the direction you've coded, you are overcomplicating it. Simply:
    Code:
        int temp;
        temp = a;
        a = b ;
        b = c ;
        c = d ;
        d = temp ;
    Now if you actully want to rotate the other way, you can guess what to do.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    your code here is wrong:
    Code:
    int temp;
    temp = a;
    a = b ;
    temp = b;  //the original a is now thrown away
    b = c ;
    temp = c;  //the original a and b are now gone
    c = d ;
    temp = d;  //the original a,b, and c are now gone
    d = a ;
    here's how it's supposed to look:
    Code:
    int temp;
    temp=a;
    a=b;
    b=c;
    c=d;
    d=temp;
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    you're setting d to equal a, and a has already been changed.

    edit:: well damn, I was running it if everything else was fine...and I guess you guys beat me to it

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >well damn, I was running it if everything else was fine...and I guess you guys beat me to it

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Durrr, Haha thanks everybody. Simple Logic Errors.

    Thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-22-2008, 12:18 AM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 06:01 PM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 07:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21