return, cout and two values

This is a discussion on return, cout and two values within the C++ Programming forums, part of the General Programming Boards category; hi guys, I am the worst programmer/learner in the world and this will be obvious in a sec.
My program ...

My first question is: in my main program do I define the strings ie inputText and then use them in replaceSubstrings?

The names of the function parameters are local function variables, i.e. variables that exist inside the function, which store whatever strings you send to the function. Inside the function, you refer to the strings sent to the function by the names: inputText, searchStr, and replaceStr. In main(), you can send arguments to the function which are string literals, i.e. some text surrounded by double quotes, or any variables that are of type string. Here is an example:

The second problem for me is when I define string searchStr, it is to replace all characters

A string variable just holds some data, it doesn't do anything, so it can't replace characters in something else. The same thing here:

// replaceStr returns *'s for all input charachters and !'s for all

replaceStr is a variable which stores an argument sent to your replaceSubstrings() function. Therefore, replaceStr doesn't return anything--it's not a function. Functions have return statements.

Will my while loop achieve that?

I don't think your while loop will work. This replace() function call:

exclamStr.replace(exclamStr[start],exclamStr[finish],"!");

uses the arguments: exclamStr[start], exclamStr[finish], and the string literal "!". The first two are of type char and the last is type const char[], but I don't think there is a form of replace() that takes those types for arguments.

Finally, with my loops, after the while() and {} will cout print the values to screen because each time I write return ie

return starStr;

I recieve an error. Why is that?

Its not really productive to talk about hypothetical code. If you have a question about some specific code, post it.

Finally, how do I assign both exclamStr and starStr to my string replaceStr?

You pretty much assign values to string variables just like any other variable:

All functions require a return type. In the display() function above, the return type is 'void', which stands for 'nada', 'nothing', 'zip'. That means, the function doesn't return anything. Functions also can have things called 'parameters'. The parameter in the function above is 'int n'. That means the function is expecting any integer to be sent to it. The integer value that is sent to the function will be stored in the variable 'n'.

That instructs the program to send the value contained in the variable 'num' to the function named display(), and then execute the code in the function. Since num is equal to 10, 10 is sent to the display() function. The value 10 is then stored in the variable 'n', which is the function parameter. Then, the code in the function is executed. After the last line in the function is executed, execution returns to the place in main() where the function was called, and then the statements thereafter in main() are executed.

Here is another example of a function:

Code:

double multiplyBy3(int x)
{
double temp = x * 3.0;
return temp;
}

This time the return type is double(a double is a number with a decimal point like 2.5). The return type has to be specified as 'double' because the function returns a double value in this line:

return temp;

When the specified return type for a function is something other than void, then the function has to have a return statement, and it must return a value that is the same type as the return type specified for the function.

The multiplyBy3() function also has an int parameter, so it must be sent an int.

Adding the multiplyBy3() function to the previous program looks like this:

The function is sent the int value stored in number. Since number is equal to 5, 5 is sent to the multiplyBy3 function, and then 5 is stored in the variable 'x', which is the function parameter. Then, the code in the function is executed. When the return statment is executed:

return temp;

the function gets the value in temp, and sends it back to main(). Since temp equals 15.0, 15.0 is sent back to main(). When a function returns a value, the function call is replaced by the return value. So, this line in main():

double result = multiplyBy3(number);

becomes:

double result = 15.0;

Then, the line:

cout<<result<<endl;

displays the return value. Note that you cannot do this to display 'result':

display(result);

The display() function requires an int to be sent to it, not a double, and result is type double. Therefore, you would get an error if you tried that: the types have to match.