Thread: CString return value

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Unhappy CString return value

    Hi,
    I've got a code that needs to return the substr. For this I have the following code

    Code:
    -----------------------------------------------------------------------
    #include <string>
    #include <stdio.h>
    #include <iostream.h>

    #include <afx.h>

    void sub_str(char *pntr)
    {
    CString name = pntr;
    printf("%s\n",name.Mid(2,3));

    };

    void main()
    {
    char temp[20];

    printf("Enter value :");

    scanf("%s",temp);

    sub_str(temp);

    }
    ----------------------------------------------------------------------------------

    How do I return the substr value back to the sub_str() function?

    I've tried a shot using return, but it doesn't work..

    Thanks,
    John

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    printf() prints to the screen.

    You need to return a CString or something like that:

    CString sub_str(char *pntr)
    {
    CString name = pntr;
    return name.Mid(2,3));
    };

    But that is unefficient.

    Maybe you should do it like that:
    void sub_str(char* pntr, char* buffer)
    {
    //copy substring from pntr to buffer
    }

    But you are using C++. So you are better using a string-class and forget the char*

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Re: CString return value

    Originally posted by John Bosko
    Hi,
    I've got a code that needs to return the substr. For this I have the following code

    Code:
    -----------------------------------------------------------------------
    #include <string>
    #include <stdio.h>
    #include <iostream.h>

    #include <afx.h>

    void sub_str(char *pntr)
    {
    CString name = pntr;
    printf("%s\n",name.Mid(2,3));

    };

    void main()
    {
    char temp[20];

    printf("Enter value :");

    scanf("%s",temp);

    sub_str(temp);

    }
    ----------------------------------------------------------------------------------

    How do I return the substr value back to the sub_str() function?

    I've tried a shot using return, but it doesn't work..

    Thanks,
    John
    Thanks Shade for the reply, however, I am stuck with one more question..
    how do I transfer the return value back into my calling program?

    I am pretty newbie with c++. sorry for that!!

    Thanks,
    John

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Re: Re: CString return value

    Originally posted by John Bosko
    Thanks Shade for the reply, however, I am stuck with one more question..
    how do I transfer the return value back into my calling program?

    I am pretty newbie with c++. sorry for that!!

    Thanks,
    John
    Thanks Shade for the help, I discovered it.
    Got the value..

    Many Thanks Once again!!
    Cheers!
    John

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    OK, my first solution should be clear:

    CString substring = sub_str(temp);

    The second one is more difficult (it is a C Solution )

    char substring[100];
    sub_str(temp,substring);


    It is important, that substring is big enough to keep the whole substring (otherwise your program will crash)

    If you are a beginner, you should use solution #1, because it is much more clearer. But keep in mind that you should get fimilar with pointers

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Dont try use objects from MFC without building an MFC project (CString)....just adding afx.h will not work as you need the rest of the MFC framework

    Also drop void main...use int main and return a value

    If you want a string, look no further than the std C++ library's string object...that will give you the substr() function that you crave...
    Code:
    #include <string>
    #include <iostream>
    using std::string;
    using std::cout;
    using std::endl;
    using std::cin;
    
    void sub_str(string str)
    {
    	cout << endl << str.substr(2,3) << endl;
    };
    
    
    int main(){
    
    	string str;
    
    	cout << "Enter value : ";
    
    	cin >> str;
    
    	sub_str(str);
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM