Having trouble with Strings and Functions.

Hey, so I just started my assignment for class, and I'm having some trouble getting my function to reference my string. I'd appreciate any help I could get.
Thank you.

Edited code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//An assignment to read in dates, break them down into their components,(Month
//year and day), and then reorganize them. 
#include<string>
#include<iostream>
void readoriginaldate(string &);
using namespace std;
int main()
{
    string originaldate;
    readoriginaldate(originaldate);
    
    
    
    
    system("PAUSE");
}   




///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Functions
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


void readoriginaldate(string &date) 
{
      
     cout<<"Enter a date"<<endl;
     getline(cin,date);
     cout<<"  "<<endl;
     cout<<date<<endl;
     
     return;
}
Last edited on
It appears that your string "origin" is local to the function, not global to the program. You must either make this string global so that the string can be accessed by main, or have your function "readoriginaldate" return the string (i.e. return origin;). In order to take advantage of that, you then have to set the function equal to something.

newstring=readoriginaldate(string& originaldate);
I think part of the problem is the fact that you don't have anything the function's returning. The function doesn't automatically know what to return, you have to tell it what to return. You can either use return (insertstringhere); instead of return;, or you can declare it void readoriginaldate(string& origin) and take out the return statement.

Also, the readoriginaldate at the beginning is inconsistent with its definition. You should have void readoriginaldate(string&) or string readoriginaldate(string&). Not declaring this makes the program think you're declaring a function readoriginaldate that doesn't take any parameters.
How would I go about making the string global?
Ok, I didn't notice that I had the function prototype as void, and the function itself as string. I changed them both to void.
John McFarland wrote:
How would I go about making the string global?


Which string do you mean? originaldate or date? If you mean date, just declare it outside of and before the method main(), and if you mean originaldate, there's no way to do it, and anyways, it would be sort of confusing anyway.
Topic archived. No new replies allowed.