Function not returning desired string

functions can return
python function return variable
void function c++ example
how to return null java
return void function c++
return null c#
return empty string java
python function return multiple values
#include <iostream>
#include <string>

using namespace std;
string wordB(string input);

int main() {
    //ask for word
    cout << "Enter a word\n";

    //get word
    string input = "";
    cin >> input;

    //return with b in between all letters
    cout << wordB(input);
    cout << endl << input;
}

string wordB(string str) {
    string rString = "";

    for (unsigned i = 0; i < str.length(); ++i) {
        rString += "B" + str.at(i);
    }
    cout << endl << rString;

    return rString;
}

Trying to display the word a users enter where between every character there is the letter "B". When I run this with the word "join" I get back "trtr".

"B" + str.at(i); doesn't do what you seem to think it does; it's not string conctatenation. It says: take a char* pointer pointing to the beginning of the string literal "B", advance it by the number of characters equal to the ASCII code of character str.at(i), and treat the resulting pointer as pointing to a nul-terminated string. Unless str.at(i) happens to be '\x0' or '\x1' (unlikely), your program exhibits undefined behavior.

There are many different ways to do what you want. Here's one:

rString.push_back('B');
rString.push_back(str[i]);

Finite Programming in C++, The function returns a string which is either the target or an empty string if the If the first argument is not a valid string, the function may not return the correct result. n is the position of the data desired postcondition: returns the desired data;  Yes it's possible to return a string from a function. Simply declare the function as a char * which would allow you to return a pointer to the desired string.

A particularly nice fix, available from C++14 onwards, is to write

rString += "B"s + str.at(i);

noting the s, which is a user-defined literal. That then forces the overloaded + operator on std::string to be used, rather than the built-in +, which is actually performing dubious (and potentially undefined) pointer arithmetic on the const char[2] literal "B" decayed to a const char*.

Introduction to the Art of Programming Using Scala, The value it returns depends only on the arguments and the function does not do anything You take a medication because it has a certain desired effect. Here is a simple example of such a function. scala> def introduce(name:String):Unit  This made the db connection function NOT function until the RETURN variable was used in another manner (in my case, used in another call to the DBUG function). Once I changed my DBUG function to use write-host instead of standard out, it worked fine.

Admittedly it is a pitfall... in this line

rString += "B" + str.at(i);

the "B" + str.at(i) part is not doing what one might expect: It adds str.at(i) to a char pointer (pointing to the first letter of "B"). The fix is easy:

rString += std::string("B") + str.at(i);
                        //  ^-------------- now calls the correct operator

Just as a curiosity consider this:

(rString += "B") += str.at(i);

I do not recommend to write it (its too obfuscated), but it does the right thing, because there is a std::string::operator+(char*) and a std::string::operator+(char).

Oracle SQL Recipes: A Problem-Solution Approach, In the solution we use the SUBSTR function twice: once with an explicit length in the string and return the remaining portion of the string regardless of length. is three characters instead of two, the solution will not return the desired results. Your function prototype states your function will return a char. Thus, you can't return a string in your function.

What you are seeing is as a result of order of evaluation.

The += operator will force the right-hand-side of the expression to be evaluated and the result will be appended to the string.

This is what causes the problem you are facing because the right-hand-side is not std::string, and therefore the meaning of the + operator in that rhs simply translates to pointer arithemetic not string concatenation as you would expect.

A simple fix is to be more explicit and do this:

rString = rString + "B" + str.at(i);

This will now cause the compiler to first evaluate the right-hand-side of the = operator as a string and then you get concatenation. It also gives the added benefit of allowing the compiler to inform you if the right-hand side is not a string.


Another alternative is to use string streams. I think it looks cleaner so here it is:

#include <sstream>
...

string wordB(string str) {
    std::ostringstream oss;

    for (unsigned i = 0; i < str.length(); ++i) {
        oss << 'B' << str.at(i);
    }
    cout << endl << oss.str();

    return oss.str();
}

Is it better to return NULL or empty values from functions/methods , Personally, I like to return empty strings for functions that return strings to is a possibility that the desired object might not be available (person does not exist,  If you do not want to return blank instead of 0 or #N/A for those two cases. How to use the VLOOKUP function to create a formula in Excel. Assuming that you have a list of data in Range of Cells A1:B5, and you want to find “excel” text string in range A1:B5, and return the corresponding value in column B.

6.2. Functions that Return Values, Python has a built-in function for computing the absolute value: drawSquare is that drawSquare was not executed because we wanted it to compute a value  This means they return the same value any time they are called with a specific set of input values. For more information about function determinism, see Deterministic and Nondeterministic Functions. When string functions are passed arguments that are not string values, the input type is implicitly converted to a text data type.

[PDF] Chapter 7 Functions, The main( ) function in a C++ program is typically a non value returning function (​a void function). A void function does not return a value to its caller. Functions  Len function does not include formatting length. For example the length of “100” formatted as “$100.00” is still 3). If the cell is empty then the Length function return 0 as output. As shown in row three and six empty string has 0 lengths.

Function returns ans rather than actual output variables, %atm the function would not save the output variables, but returns an ans. %​variable instead %if statement to ensure the string is in the desired length. %atm the function would not save the output variables, but returns an ans. %variable instead. function [case_des_string,case_urgency, Case_urg] = HowUrgent (case_des_string); %This function accepts a string from the user, describing the case. %properties, and returns the case urgency as a conversion of the.

Comments
  • Undefined behaviour. str.at(i) is an integral value. "B" + n where n is an integral value accesses the nth character of the string literal "B". Since the string literal "B" is represented as an array of two char, any value of n less than zero or more than 1 falls off the end. Your code appends the result of all that to rString.
  • Or rString += "B" + str.substr(i, 1);
  • I'd recommend using stringstream
  • @smac89: Why, out of interest?
  • @Bathsheba Looks cleaner IMO: ss << 'B' << str.at(i)
  • @Bathsheba good point, but you only get to do the .str() at the end, not between concatenations
  • @smac89 I am with Bathsheba, I dont like the boilerplate that comes with stringstream. But there is still room for another answer ;) go ahead and add it. The more different answers, the better