Thread: I think i jacked this up completely. Help?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    I think i jacked this up completely. Help?

    I am trying to get this to shift ABCDE...BCDEA...CDEAB. Whats up with the program?

    Can anyone tell me where my mistakes are?


    Code:
    #include<stdio.h>
    
    void shift(char *p1, char *p2, char *p3, char *p4, char *p5);
    void shift(&c1,&c2,&c3,&c4,&c5);
    
    int main()
    {
    void shift(char *p1, char *p2, char *p3, char *p4, char *p5);
    void shift(&c1,&c2,&c3,&c4,&c5);
    
    
    shift(char *p1, char *p2, char *p3, char *p4, char *p5)
    {
        #define END_INDEX 4
        int iCurrentShift = 0, iCurrentCharIndex = 0 ;
    	char c1='A', c2='B', c3 ='C', c4 ='D', c5 ='E' ; 
        char cFirstLetter ; 
    
        for( iCurrentShift = 1 ; iCurrentShift <= iShiftCnt ; iCurrentShift++) 
    
        {
            cFirstLetter = letter[ 0 ];  /* save the first as we're moving something in there */
            for( iCurrentCharIndex = BEGIN_INDEX ;  /*if BEGIN_INDEX = 0 & its equal to currentcharindex then goto next statement*/
                    iCurrentCharIndex <= END_INDEX - 1;
                        iCurrentCharIndex++)
            {
                letter[ iCurrentCharIndex ] = letter[ iCurrentCharIndex + 1];
            } /* end for icurrentcharindex */
     
            letter[END_INDEX ] = cFirstLetter ;
       return 0;
    }
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    you cannot define functions inside other functions. it is not wise to put #define and other preprocessor commands inside of functions because preprocessor commands don't obey the normal flow of the function. they're evaluated even before it looks at the actual program.

    i recommend rewriting the program. for a small program like this it helps get a grip on what a program is and does. i haven't looked at your program in detail, so i don't know if you're on the right track. here's something to help you in any case:
    say that this is your string:
    Code:
    char a[6] = {'A','B','C','D','E','\0'};
    this is what you want to do:
    1) store the first letter of the string
    2) for every letter of the string except the null terminator, make it equal to the next letter in the array. (the array is now "BCDE\0")
    3)replace the last letter with the letter you stored in the beginning.
    4) lather, rinse, and repeat

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    5
    You can have a look at this code:

    void main()
    {
    char s[] = {"ABCDE"};

    for(int i=0; i<5; i++)
    {
    for(int j=i; j<i+5; j++)
    {
    cout<<s[j%5];
    }
    cout<<endl;
    }
    }

    rgds,
    S.Guruprasanna

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708



    Code:
    
    char *Shift( char[] );
    
    int main()
    {
    
     char string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
     int i = 53;
    
     while(--i)
     {
       clrscr();
       printf("%s", Shift( string ));
       Sleep(100);
       //getch();  //...use this if not including <windows.h>...
      }
    
    getch();   
    
    return 0;
    }
    
    
    
    
    char* Shift( char str[] )
    {
     int i;
     int length = strlen(str);
     char temp = str[0];        //...copy first char and save...
     
     for(i = 0;  i < length - 1;  i++)  //...carefull with bounds...
      {
        str[i] = str[i+1];           
      }
     
      str[i++] = temp;
      
      str[i] = 0;                      //...null terminate, to be sure...
    
    return str;                     //...return for printing...
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sguruprasanna
    You can have a look at this code:

    void main()
    {
    char s[] = {"ABCDE"};

    for(int i=0; i<5; i++)
    {
    for(int j=i; j<i+5; j++)
    {
    cout<<s[j%5];
    }
    cout<<endl;
    }
    }

    rgds,
    S.Guruprasanna
    Hi, S.Guruprasanna. I see this is your first post on the board... welcome.

    Allow me to point out a couple of problems...
    >void main()
    is wrong, main returns an int. It should be
    >int main(void)

    and the cout function is part of a C++ class. This is board is here to talk about C specifically, so please don't provide answers with C++ code, or else you'll confuse the newbies
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    5
    Hammer,

    Thanks for your suggestions...

    Regards,
    S.Guruprasanna

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Completely Clueless!!
    By jap2010 in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2009, 02:30 AM
  2. Serial port: How to know when data has been completely sent
    By arjunajay in forum Windows Programming
    Replies: 8
    Last Post: 03-25-2009, 08:18 AM
  3. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11:51 AM
  4. Completely Automatic Mesh Morphing Algorithm
    By Zeeshan in forum Game Programming
    Replies: 5
    Last Post: 05-15-2008, 07:05 AM
  5. Completely modular motherboards?
    By SMurf in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-23-2003, 12:56 PM