Problem passing a pointer

This is a discussion on Problem passing a pointer within the C++ Programming forums, part of the General Programming Boards category; All I am trying to do here is change the first letter to a capital. While I'm in the function ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    7

    Problem passing a pointer

    All I am trying to do here is change the first letter to a capital. While I'm in the function makefirstcapital the first letter is changed, the odd part comes when I go back to main firstname is bumped to secondname, and firstname has random stuff in it.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    void makefirstcapital(char*);
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
      char first[20],second[20];
      cout<<"First name:";
      cin>>first;
      cout<<endl<<"Second name:";
      cin>>second;
      makefirstcapital(first);
      cout<<endl<<first;  
    
    
    
      
      cout<<endl;system("PAUSE");	
      return 0;
    }
     
    
    void makefirstcapital(char *name)
    {
      char switchchar[1];
    
      switchchar[0]=name[0];
      strupr(switchchar);
      name[0]=switchchar[0];
    }
    Am I passing the pointer wrong? I appologize if this is a noobish question.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try this instead:
    Code:
    void makefirstcapital(char *name)
    {
      *name = toupper((unsigned char) (*name) );
    }
    strupr() changes the entire string to upper case.

    edit: Just realized that I forgot the assignment back from toupper(). Sorry about that.
    Last edited by Thantos; 07-11-2004 at 06:51 PM.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You need to make switchchar 2 characters long and assign a '\0' to switchchar[1] before calling strupr(switchchar).
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    just for fun -

    Code:
    char * strformal(char * str)
    {
     bool ready = true;
     for(int index = 0; str[index] != 0; ++index) {
      if(isalpha(str[index])) {
       if(ready) {
         ready = false;
         str[index] = toupper(str[index]);
        } else {
         str[index] = tolower(str[index]);
        } 
       } else {
        ready = true;
       }
      }  
     return str;
    }
    
    
    int main(void) {
     char s[] = "joHN s. hiGGens";
     printf("%s\n", strformal(s));
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    35

    Talking

    Code:
    #include <iostream>
    using namespace std;
    
    void strformal( char *str )
    {
    	int i;
    
    	for (i = 0; str[i] != '\0'; i++)
    		if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
    			if (((str[--i] == ' ') & 0x1) ^ (0x1 & (95 > str[++i])))
    				str[i] += 32 * ((str[i] < 95) ? 1 : -1);
    }
    
    
    int main( void )
    {
    	char s[] = " joHN s. hiGGens";
    	
    	strformal(s);
    	cout << s << endl;
    }
    [edit] A space prefix is neccessary. [/edit]
    Last edited by MortalMonkey; 07-11-2004 at 05:04 PM.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    if u only want to make the first char capital u can use too:
    Code:
    void makefirstcapital(char *name)
    {
      char capital = name[0] - 32; //-32 gives you the capital letter of each char(ascii code)
      name[0] = capital;
    //you can change the 0 by a int variable to change any char number in  the string
    }
    
    you can modofy your function too, to receive an integer, this  integer should be the numeber of the char you want capital
    looks better for me

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char capital = name[0] - 32; //-32 gives you the capital letter of each char(ascii code)
    The world is not in ASCII. If you want something portable outside of the character set and locale that you're used to, use toupper.
    My best code is written with the delete key.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I think most of you didn't read what the OP wanted to do.
    All I am trying to do here is change the first letter to a capital.
    Which means using strupr() is out, there is no need for a local variable in the function, nor any need for poorly written trick coding.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    I'm just competing for the Most Crap In Fewest Lines Award

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > strupr(switchchar);
    Should be:
    switchchar[0] = strupr(switchchar[0]);

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Should be:
    >switchchar[0] = strupr(switchchar[0]);
    It depends on the definition of whichever strupr you use. The function isn't standard, and those that I've seen are defined like this:
    Code:
    char *
    strupr(
      char *src
      )
    {
      char *p = src;
    
      while (*p) {
        *p = toupper((unsigned char)*p);
        ++p;
      }
    
      return src;
    }
    src is modified and returned as a pointer to a string, so your idea of passing individual characters won't work. You can pass the address, but that won't have the effect you'd like, I imagine.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It depends on the definition of whichever strupr you use. The function isn't standard, and those that I've seen are defined like this:

    Sorry Prelude, you're right, I was thinking toupper().

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    7
    Thanks alot everyone. I found out what was causing the problem. Thanks for telling me about the toupper(). I'm learnin a little bit more everyday.
    The code I threw up there was just the chunk I had a problem with. It was just a exercise I thought up to use pointers. I found what was causing the problems one was the switchchar[1], should of been switchchar[2](thank you!). The other was my original code I used cin.getline to get first and second, instead of cin>>. For some reason out of the function first was second and second had nothing in it. Thank you again everyone you guys have been a great help.

  14. #14
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by bigbadbowlindud
    The other was my original code I used cin.getline to get first and second, instead of cin>>. For some reason out of the function first was second and second had nothing in it.
    I think (not too sure, maybe someone can correct this), but getline will leave the newline character at the end...so when you called getline or cin again, all you read was the newline? Can someone tell me is this is right????

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    getline() will discard the newline from the buffer
    get() will leave the newline in the buffer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 11:42 AM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  3. Replies: 5
    Last Post: 08-12-2007, 05:26 PM
  4. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 01:45 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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