returning structs and manipulation

Can someone lend me a hand with this? I'm trying to return a struct. (technically two different data , not necessarily struct)
so I have a few issues. First C++ doesn't let me return structs. 2nd when I try to input values into struct it tells gives me an error as the values are not consts or statics. How can I make this code work?

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
struct VWAP()
{
	ifstream in;
	string name1, name2;
	cout <<"What's the name of the file for volume? ex:data.txt "<<endl;
	cin >> name1;
	
	cout << "What's the name of the file for price?ex:data.txt "<<endl;
	cin >>name2;

	bool more;
	int junk;
	vector <int> VOLUME;
	vector <int> PRICE;
		for (int i=0 ;i<2; i++){
			more = true;
			if(i ==0) {in.open(name1);}
			else {in.open(name2);}
			while(more)
			{	
			in >> junk;
			if(i==0){VOLUME.push_back(junk);}
			else{PRICE.push_back(junk);}
				if(in.fail())
				{						//handles bad input and eof
				more = false;
				if(in.eof()!=1)
				{cout<<"\nBad input data. Possible entry of wrong name" << endl;}
				}
			}
			in.close();
		}
		int sumVol=0;
		int sumVolTimesPrice=0;

		for(int j=0; j<VOLUME.size();j++)
		{
			sumVol = sumVol + VOLUME[j];
			sumVolTimesPrice = sumVolTimesPrice +VOLUME[j]*PRICE[j];
		}
		
                struct output{
		vector <double> volPercent;
		double vwap;
		} ;

		for(int j=0; j<VOLUME.size();j++)
		output.volPercent.push_back(VOLUME[j]/sumVol);
		
		
		output.vwap = sumVolTimesPrice/sumVol;

		return output;
}


Thanks for any help
Last edited on
The compiler does not know what shall it return because you did not specify return type of the function.

struct VWAP();
Last edited on
C++ certainly does let you return a struct, you just have to tell it what kind of struct. Giving a return value of struct is too vague.

Furthermore, if the struct is only defined inside that function, how do you expect the returned value to be used by the calling function.

You need to give your struct a broader scope:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

struct mystruct
{
  vector <double> volPercent;
  double vwap;
};

mystruct VWAP()
{
  //...
  mystruct output;
  output.whatever = whatever;

  return output;
}
Thank you. Have it working.
Topic archived. No new replies allowed.