Thread: passing a character array through a function

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    passing a character array through a function

    I keep getting conversion errors. What should I put as the return statement in the function definition?

    Code:
    #include <iostream>
    using namespace std;
    
    char capitalize(char []);
    
    int main()
    {
    	char string[81];
    	
    	char c;
    	
    	cout << "Enter a sentence" << endl;
    	cin.getline(string, 81);
    
    	c = capitalize(string);
    
    	cout << endl << c << endl;
    	
    	return 0;
    }
    
    char capitalize(char string[])
    
    {
    	register int i = 0;
    	
    	while (string[i++])
    	{
    		string[0] = toupper(string[0]);
    
    		if (string[i] == ' ')
    			string[i + 1] = toupper(string[i + 1]);
    	}
    	
    	return string;
    }
    This is the only solution I can come up with, but I want the cout statement that displays the string to be in main() instead of the function definition of capitalize().

    Code:
    #include <iostream>
    using namespace std;
    
    void capitalize(char []);
    
    int main()
    {
    	char string[81];
    	
    	cout << "Enter a sentence" << endl;
    	cin.getline(string, 81);
    
    	capitalize(string);
    
    	return 0;
    }
    
    void capitalize(char string[])
    
    {
    	register int i = 0;
    	
    	while (string[i++])
    	{
    		string[0] = toupper(string[0]);
    
    		if (string[i] == ' ')
    			string[i + 1] = toupper(string[i + 1]);
    	}
    	
    	cout << endl << string << endl;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay a made a few changes.

    Code:
    #include <iostream>
    using namespace std;
    
    char* capitalize(char []);
    
    int main()
    {
      char *lpString = NULL;
      char string[81];
    
      cout << "Enter a sentence" << endl;
      cin.getline(string, 81);
    
      lpString = capitalize(string);
    
      // Make sure our pointer is valid
      if( lpString != NULL )
        cout << lpString << endl;
    
      return 0;
    }
    
    char* capitalize(char string[])
    {
      register int i = 0;
    
      while (string[i++])
      {
        string[0] = toupper(string[0]);
    
        if (string[i] == ' ')
          string[i + 1] = toupper(string[i + 1]);
      }
    
      return string;
    }
    Lets talk about your first implementation. That was really close except that in your capitalize function you were trying to return a single char. This should have given you an error when you were trying to return string at the end of the function. Something along the lines of unable to convert from char[] to char. Anyways, since we can't return an array from a function we need to return a pointer to it. That's what I have done in my function. Then you can either assign that pointer to the result and output it like that or if you don't need to save the pointer for later use you can simply do:

    Code:
    cout << capitalize( string ) << endl;
    Hope this helped.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Thanks MrWizard. Oh yeah, you're that Digipen student...

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by volk
    Thanks MrWizard. Oh yeah, you're that Digipen student...
    ??? What's that supposed to mean?

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    It means that of course you were able to easily solve that problem since you're a digipen student. Those students are supposedly very talented, right? You probably receive a very high level of training over there. Why else would that school be so frigin' expensive?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM