<>Function that gets name and hourly rate from file

Write your question here.

I need to create a function that reads from a file and displays name and hourly rate beneath a header.

Everything is in place, but I have to take my functions for getName() and getRate() and combine them. Any ideas? I tried creating a void readFile(string &name, float &rate), but how do I return values from a void?
I haven't done any of the read / write file tutorials yet.

But, I think labeling something "void" means that nothing will be returned to a function that called it. It just performs actions and that's it.

If you want to get information from a void, you'd have to use a pointer of some sort.

Maybe this will help, idk.

http://www.cplusplus.com/doc/tutorial/polymorphism/
I can't use pointers yet. If we use anything that we haven't learned, her grading criteria becomes harsher.
@Garion You may return using pointers , references , or by actually returning. When you actually return you can only return one thing but using pointers and references you can return as many things as you want. You can return almost anything besides an array int [] function(); is invalid. You can also return references and pointers instead of copies.


@op you are already passing by reference which is the way you return in a function with no return type or you can use pointers:

I'd assume this is what you mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void readFile( string &name , float &rate , const string &filename )
{
    ifstream in( filename );
    in >> name;
    in >> rate;
}

int main()
{
    string filename = "text.txt";
    string name;
    float rate;
    readFile( name , rate , filename );
    cout << "Name: " << name << endl;
    cout << "Rate: " << rate << endl;
}


With references you are really passing the memory address of that variable so when you modify it in the function it modifies it at that memory address so the actual variable being passed is then modified.

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
Topic archived. No new replies allowed.