Thread: error when return string from a function

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    17

    Lightbulb error when return string from a function

    Hi everyone,
    I just write a function that return string as below in visual studio for C++:
    Code:
    string func(int i1, int i2){
    if(condition1)
        return "A";
    else if( condition2)
        return "B";
    else 
        return "C";
    
    }
    out side of this function, I defined a variable
    string a;
    Code:
    void main(){
    a = func(i1,i2);
    }
    the program was halted, and the break point stop at a line of a function as below in xstring file local on VS/include/xstring

    Code:
    void _Assign_rv(_Myt&& _Right)
            {    // assign by moving _Right
            if (_Right._Myres < this->_BUF_SIZE)
                _Traits::move(this->_Bx._Buf, _Right._Bx._Buf,
                    _Right._Mysize + 1);
            else
                {    // copy pointer
                this->_Getal().construct(&this->_Bx._Ptr, _Right._Bx._Ptr);
                _Right._Bx._Ptr = pointer();
                }
            this->_Mysize = _Right._Mysize;
            this->_Myres = _Right._Myres;
            _Right._Tidy();
            }
    Please take a look and help me to solve my probelm.
    Thank you so much!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post the smallest and simplest program that demonstrates the problem, not some theoretical example that isn't intended for compilation, in fact will not compile, and yet you tell us that "the program was halted" and then show us part of your standard library implementation instead of your own code! The bug almost certainly lies in your code, not your standard library implementation, so that is a rather futile approach.

    Here's an example that demonstrates that there is no problem at all with your code, if only it were written correctly:
    Code:
    #include <iostream>
    #include <string>
    
    using std::string;
    
    string func(int i1, int i2) {
        if (i1 < i2)
            return "A";
        else if (i1 == i2)
            return "B";
        else
            return "C";
    }
    
    int main() {
        int i1 = 0;
        int i2 = 1;
        string a = func(i1, i2);
        std::cout << a << std::endl;
    }
    The above program, when run, prints "A" for me. Of course, it halts, but it halts when expected, i.e., when control has reached the end of the main function. Notice that I correctly wrote the return type of main as int, not void.

    As for what is the problem with your code: I have no idea, because I have no idea what is the code that you compiled and ran.

    Also, it would be good to tell us what version of Visual Studio (or MSVC in particular) that you are using, and also tell us if you are compiling with respect to C++11 or later. I chose to write my example in pre-C++11 because I suspect that you may be using an older version of the compiler, as is fairly likely in an academic context where they don't update their tools for years.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Thank for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return a string from C function
    By FernandoBasso in forum C Programming
    Replies: 13
    Last Post: 11-30-2011, 01:41 PM
  2. How to return a string from a function
    By Richardcavell in forum C Programming
    Replies: 4
    Last Post: 04-20-2011, 06:20 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. A function to return a string
    By earth_angel in forum C Programming
    Replies: 20
    Last Post: 06-10-2005, 11:03 AM
  5. Why won't this function return the new string value?
    By Jacquiline in forum C Programming
    Replies: 1
    Last Post: 04-06-2003, 08:45 AM

Tags for this Thread