Pointer Help

This is a discussion on Pointer Help within the C++ Programming forums, part of the General Programming Boards category; hi all, to the point, i wrote this out, but for some reason it wont work... i run xp with ...

  1. #1
    tetra
    Guest

    Pointer Help

    hi all, to the point, i wrote this out, but for some reason it wont work... i run xp with msvc and for some reason it isnt accepting my ' ' declaration.


    the purpose of this program is to convert every other word to capitol letters.

    let me show you

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    #include <cctype>
    
    void convertToUppercase( char * );
    
    int main()
    {
       char string[] = "why is msvc++ gay when it comes to spaces?";
    
       cout << "The string before conversion is:\n\n " << string;
       convertToUppercase( string );
       cout << "\nThe string after conversion is:\n\n  " 
            << string << endl;
       return 0;
    }
    
    void convertToUppercase( char *sPtr )
    {
       while ( *sPtr != '\0' ) {
    
     
    	sPtr++;
    	
              while (*sPtr != ' '){          //  it doesnt  to like the space.
    	     
    
               if ( islower( *sPtr ) )
             *sPtr = toupper( *sPtr );  
    
          ++sPtr;  
       
    }
       }
    }


    now my code crashes when i run it, but compiles with no errors at all........

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It has nothing to do with spaces. It has to do with your bad loops:
    Code:
    void convertToUppercase( char *sPtr )
    {
    	while ( *sPtr != '\0' ) {
    		sPtr++;
    	
    		while (*sPtr != ' '){          //  it doesnt  to like the space.
    			if ( islower( *sPtr ) )
    				*sPtr = toupper( *sPtr );  
    			++sPtr;  
    		}
    	}
    }
    Your inner loop is running off the end of your string because you never check the inner loop for NULL.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    OffTopic: Gee, I hate the kernel style...
    Code:
    while(*sPtr != ' ')
    {
       if(islower( *sPtr ))	*sPtr = toupper(*sPtr);  
       ++sPtr;
    }
    This code will loop only until it finds a space, which means it will go through the NULL terminator and out of bounds, probably resulting in some access violation error.

    Add a condition to make sure it doesn't pass the \0:
    Code:
    if((*sPtr != ' ') && (*sPtr != '\0'))
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    tetra
    Guest
    thanks, but it still doesnt do what i want it to


    i want it to convert every other word to upper case


    so instead of for example

    i like programming.

    it should read after string conversion

    I like PROGRAMMING.



    i put this in the function as you said. with the if statement


    [CODE]
    void convertToUppercase( char *sPtr )
    {
    if((*sPtr != ' ') && (*sPtr != '\0'))

    if ( islower( *sPtr ) )
    *sPtr = toupper( *sPtr );

    ++sPtr;

    }

    [\CODE]

    if you want to see the rest of my code look at my first post :0 i dont wanna take up space

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Nononono, now you have lost your loop. What you want to do is loop through all characters until \0 is encountered, and if the current character is a letter (in your case, not a space) you transform it to uppercase. I may have been a little misleading with the if-code I showed, sorry .
    Code:
    void convertToUppercase(char *sPtr)
    {
       while(*sPtr != '\0')
       {
          if((*sPtr != ' ') && islower(*sPtr))
          {
             *sPtr = toUpper(*sPtr);
          }
          sPtr++;
       }
    }
    (and the final code tag is [/CODE] not [\CODE] )
    Last edited by Magos; 12-16-2002 at 05:31 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    tetra
    Guest
    ok. this must be msvc i had what you did in school, and it wouldnt print out. does msvc have a different code for a space? it really does not work with msvc try it!

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Works for me (notice that I forgot the sPtr++).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    tetra
    Guest
    thats really wierd.... can u post your whole code for me?
    i get the idea, and im only asking because i posted pervious code!

    * tetra puts on his flame proof jacket*

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Here u go...
    Attached Files Attached Files
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should work...

    Code:
    void convertToUppercase( char *sPtr )
    {
      cout << "hallo" << endl;
      while ( *sPtr != '\0' )
      {
        if (*sPtr != ' ') 
        {          
          if ( islower( *sPtr ))
            *sPtr = toupper( *sPtr );
    
          ++sPtr;
        }
        else
          sPtr++;
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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