Thread: return, cout and two values

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    return, cout and two values

    hi guys, I am the worst programmer/learner in the world and this will be obvious in a sec.
    My program has two strings in a library, the second of which has three substrings.
    ie:
    Code:
    replaceSubstrings(string inputText, string searchStr, string replaceStr)
    My first question is: in my main program do I define the strings ie inputText and then use them in replaceSubstrings?

    The second problem for me is when I define string searchStr, it is to replace all characters in two strings (input) and (reverseString) with *'s and !'s respectively.

    Will my while loop achieve that?

    Code:
     // replaceStr returns *'s for all input charachters and !'s for all 
    // reverseString characters.
    
    string starStr = input ;
    string exclamStr = reverseString ;
      int start = 0,
          finish = input.size()-1;
    
      while (start < finish)
        {
          exclamStr.replace(exclamStr[start],exclamStr[finish],"!");
          starStr.replace(starStr[start],starStr[finish],"*");
          start++;
          finish--;
        }
    Finally, with my loops, after the while() and {} will cout print the values to screen because each time I write return ie

    Code:
    return starStr;
    I recieve an error. Why is that? return is in all of the textbooks and I can't get it. I have (out of error frustration) just used cout, but I am wondering how this will affect my program.

    Finally, how do I assign both exclamStr and starStr to my string replaceStr? I hope this all makes sense.

    Sorry about the stupid questions, and thankyou any good person who helps me sort out my frustrations!!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    I'm not sure what the heck your description means here:
    My program has two strings in a library, the second of which has three substrings.

    replaceSubstrings(string inputText, string searchStr, string replaceStr)
    but that looks like it could be a function you defined that requires three arguments, which are all string types. However, a function definition has to have a return type, e.g.:
    Code:
    string replaceSubstrings(string inputText, string searchStr, string replaceStr)
    {
    
        //code here
    }
    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:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void replaceSubstrings(string inputText, string searchStr, string replaceStr)
    {
    	cout<<inputText<<endl
    		<<searchStr<<endl
    		<<replaceStr<<endl;
    }
    
    int main()
    {
    
    	string str1 = "Hello";  
    	string str2 = "world";
    
    	replaceSubstrings(str1, str2, "Goodbye.");
    
    	return 0;
    }
    This doesn't make any sense:
    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:
    Code:
    int num = 10;
    int sum = num + 20;
    
    string str1 = "some text";
    string str2 = " other text";
    string str3 = str1 + str2;
    Here is some additional information about using functions:

    Here is an example of a very simple function:
    Code:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    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'.

    Here is what the function looks like as part of a program:

    Code:
    #include <iostream> //contains cout
    
    using namespace std; 
    //makes it so you don't have to write 'std::cout'
    //instead you can just write 'cout'
    
    
    //function:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main()
    {
    	int num = 10;
    	display(num);
    
    	return 0;
    }
    In main(), the function is called by this line:

    display(num);

    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:

    Code:
    #include <iostream> //contains cout
    
    using namespace std; 
    //makes it so you don't have to write 'std::cout'
    //instead you can just write 'cout'
    
    double multiplyBy3(int x)
    {
    	double temp = x * 3.0;
    	return temp;
    }
    
    void display(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main()
    {
    	int num = 10;
    	display(num);
    
    	int number = 5;
    	double result = multiplyBy3(number);
    	
    	cout<<result<<endl;
    
    	return 0;
    }
    The function multiplyBy3() is called in main() by this line:

    double result = multiplyBy3(number);

    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.
    Last edited by 7stud; 05-15-2005 at 02:12 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    Thankyou so much!! That makes so much sense!! It was of course a total misunderstanding on my part on what I was trying to achieve but now I think I get it!! That is so much easier!!

Popular pages Recent additions subscribe to a feed