Thread: Using char as a return type

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    Using char as a return type

    Hey guys,

    I am writing a C++ console app that carries on a conversation with the user. I need to be able to easily write functions that might take the user's input (type char), then make some decision about what to say, then finally return a value of type char. I would like to do it something like this... (leaving out irrelevant code)

    Code:
    char someFunction()
    {
         char output;     
         ...
         ... // Decide what to say
         ...
         return output;
    }
    
    int main()
    {
        cout << someFunction() << endl;
        return 0;
    }
    I can get it to compile without any errors, but I don't get the right output, I get goofy control characters. You can put a function inside a cout statement as long as the function return a char, right?

    Just in case that's wrong, I also tried to get around that by doing this...

    Code:
    int main()
    {
        
         char word;
         strcpy(word, someFunction());
         
         cout << word << endl;
    
         return 0;
    }
    Didn't work either. I could just put the cout statement inside the function, but that defeats the whole purpose of using encapsulation. I need to be able to write many small functions that return char because I will probably change this to a GUI later and the cout statements become useless.

    Why is it showing control characters? What am I doing wrong?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    The error is probably in how you're deciding what to say. Chars are valid for couting, as are strings.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> (leaving out irrelevant code)
    The first code you posted is correct, the relevant code is what is missing.

    >> char word;
    >> strcpy(word, someFunction());

    This shows a lack of understanding of what a char is and what a string is. The strcpy function copies an array of characters (referred to as a string), but in that code word and the return of someFunction have the char type which holds only a single character.

    You probably want to use the C++ string class to hold your strings. That can be returned from a function and output with cout.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, looks to me like you're confusing chars with strings... you can use your code with just a few modifications:
    Code:
    #include <iostream>
    
    //this is the function prototype
    void someFunction(char*word);
    
    int main()
    {
    	//here we assume a phrase no bigger than 512 characters
    	char*word=new char[512];
    	//I designed the someFunction function so that we don't need strcpy
    	someFunction(word);
    	//output the response
    	std::cout<<word<<std::endl;
    	//deallocate that space
    	delete[]word;
    	//END.
    	return 0;
    }
    
    //function definition
    void someFunction(char*word)
    {
    	//prompt for input
    	std::cout<<"Hello World\n> "<<std::flush;
    	//get their input, up to 20 chars and stopping when they hit "ENTER"
    	std::cin.getline(word,20,'\n');
    	//assign the response to the parameter
    	strcpy(word,"Thank you for helping me test my AI");
    	//bye.
    	return;
    }
    here's an implementation using stl strings (this may be easier to use):
    Code:
    #include <iostream>
    #include <string>
    
    //this is the function prototype - notice the new ampersand (&)
    void someFunction(std::string&word);
    
    int main()
    {
    	//stl strings take care of most of your size woes
    	std::string word;
    	//I designed the someFunction function so that we don't need strcpy
    	someFunction(word);
    	//output the response
    	std::cout<<word<<std::endl;
    	//notice we dont' need to deallcoate anything
    	//END.
    	return 0;
    }
    
    //function definition - The ampersand (&) means we're passing in the address
    //of the variable.
    void someFunction(std::string&word)
    {
    	//prompt for input
    	std::cout<<"Hello World\n> "<<std::flush;
    	//Notice the changes in this next line - it still stops at ENTER, but
    	//there are no size limitations (theoretically)
    	getline(std::cin,word,'\n');
    	//an easier assignment is now possible thanks to the string class
    	word="Thank you for helping me test my AI";
    	//bye.
    	return;
    }
    Last edited by major_small; 03-10-2006 at 04:22 PM.
    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
    Join Date
    Jan 2005
    Posts
    7,366
    Another example using the string class that more closely mirrors the original code:
    Code:
    #include <iostream>
    #include <string>
    
    std::string someFunction()
    {
    	std::string output;
    
    	//prompt for input
    	std::cout<<"Hello World\n> ";
    	//Notice the changes in this next line - it still stops at ENTER, but
    	//there are no size limitations (theoretically)
    	std::getline(std::cin,output,'\n');
    	//an easier assignment is now possible thanks to the string class
    	output="Thank you for helping me test my AI";
    	//bye.
    
    	return output;
    }
    
    int main()
    {
    	//You don't need strcpy with strings and you don't need a temporary variable
    	std::cout << someFunction() << std::endl;
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    Talking Thanks

    Thanks major_small, you gave me the exact info I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM