Hello daniweb, I've been having problems with a program I'm trying to make, in my function definitions, I'm trying to reference the output of one function in the other's definition, but not sure how to, I'm guessing pointers could help me but I'm not sure how to use them:

These are the two functions:

string inputsentance(){

		string sentance;
		cout<< "inputsentance";
		cin>> sentance;
	return sentance;
	
string genstring()

{
	string generated_string;
	
	for (int k=0;k<sentance.size();k++ ) // I want it to use "sentance" from the function above
	{
		char c = rand() % 27 + 'A'; //random from "A-["

		if( c == '[' )
			c = ' ';

		generated_string.append(1, c);
	}//cout << generated_string;
	return generated_string;
}
int main(){
srand(time(0));

string sentance;

inputsentance();
genstring();

return 0;
}


}

Is it possible to reference locations in definitions like this?

Many Thanks

To do so, you'd either have to use global variables (not good) or use new and delete to extend object lifetimes.

A simpler way is to just use parameters in your functions. It's what they're for.

string inputsentance()
{ 
    string sentance;
    cout<< "inputsentance";
    cin>> sentance;
    return sentance;
}
     
string genstring(string input)
{
    string generated_string;
     
    for (int k=0;k<input.size();k++ ) // I want it to use "sentance" from the function above
    {
       char c = rand() % 27 + 'A'; //random from "A-["
     
       if( c == '[' )
         c = ' ';
       generated_string.append(1, c);
    }
    return generated_string;
}

int main()
{
  srand(time(0));
     
  string sentance;
     
  sentance = inputsentance();
  string returnedString = genstring(sentance);
  cout << returnedString ;
  return 0;
}

Edited 4 Years Ago by Moschops: n/a

This question has already been answered. Start a new discussion instead.