2 Replies - 5252 Views - Last Post: 28 March 2010 - 05:30 PM Rate Topic: -----

#1 angelofdzire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-September 09

pass value from one function to another

Posted 28 March 2010 - 04:05 PM

I am having a difficult time passing the value from one function to another. I've rewritten this program so many times... that before I pulled any more hair out ( >.< ) I wanted to see if I can get some constructive feedback.
In my program, I am asking for the user to input a string, then pass that string to the getLength function, and then display the functions return value. However, if I did cin before referencing the getLength function, it would tell me invalid conversion. Even if I changed the parameter type to match, I am still getting the same error messages.
Now in the getLength function, I know I need to return some value... what I have in there now I know doesn't work, it's just showing that I understand I need to return some value back to the main function.
Can someone please point me in the right direction?
Thank you =)

#include <iostream>
using namespace std;

int *getLength(int);

int main()
{
    int townLength;
    int *ptr;
     
    cout << "Enter your towns name.\n";
    
    getLength(townLength);
    
    ptr = getLength(townLength);
    cout << "Your town has" <<  ptr << "characters" << endl;
    
    delete [] ptr;
    ptr = 0;
    system ("PAUSE"); 
      
    return 0;
}
//*********************************************
// Function nameLength                        *
// This function counts the number of         *
// characters in a string.                    *
//                                            *
//*********************************************



int *getLength(int)
{
   string townName;
   int *ptr;

   cin >>townName;  //Read as a string
    
   ptr = townName.size();
   
   return ptr;
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: pass value from one function to another

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: pass value from one function to another

Posted 28 March 2010 - 04:10 PM

int *getLength(int)
{
   string townName;
   int *ptr;

   cin >>townName;  //Read as a string
    
   ptr = townName.size();
   
   return ptr;
}


For starters, the size() method of a string object does NOT return a pointer to an integer, it returns a size_t. Why do you need to pass an argument at all? You're not using it; in fact, you're not even giving it a name?

EDIT: How about reading our tutorial on functions?

This post has been edited by JackOfAllTrades: 28 March 2010 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: pass value from one function to another

Posted 28 March 2010 - 05:30 PM

like jack said, why even pass in a variable. You should refresh yourself on pointers too.

heres what would make sense
int *getLength(void)
{
	int *nameSize = new int; //so memory does not go out of scope
	string townName;
	cin>>townName;
	*nameSize = townName.length();
	return nameSize;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1