References with structures problem

Aug 2, 2013 at 1:30am
I am getting an error at the calling function in main. Can anybody help me. Can provide more source code if needed.
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
struct Information
{
	string name;
	unsigned int age;
	unsigned int duration;
};
.
.
.
double calculate(Information & Stay);
.
.
.
int main()
{
  double cost = 0.0; 
  .
  .
  cost = calculate(Customer->duration);
  .
  .
}
.
.
.
 double calculate(Information & Stay)
{

    return (Stay * FEE); 
}


Also can somebody explain,
why this can't work

1
2
3
4
double calculate(Information & Stay)
{
  return (Stay * FEE); 
}

Thanks in advance.

Aug 2, 2013 at 1:35am
What is FEE?? You must define this before used or it is a global variable.
Last edited on Aug 2, 2013 at 1:37am
Aug 2, 2013 at 1:39am
and function calculate have a parameter which is a reference of the struct Information. but in function main(), its parameter is a member of the Information. your parameter is wrong.
Aug 2, 2013 at 1:44am
You'd better describe that what specific error you got i think.

For the code posted, there is no definition of Customer, so we cannot know the type of Customer->duration.

The parameter of double calculate(Information & Stay) is passed by reference, so please make sure that passing directly Customer->duration can be treated as a reference.
Aug 2, 2013 at 1:56am
@vuongang1993, forgot to put that in. FEE is a global const
 
const double FEE= 50.00


@aidyszh, I am not quite sure by what you mean by definition in terms of Customer, but I will try..
 
Information * Customer = new Information[SIZE];


The error that I am getting is
a reference of type "Information &" (not const qualified) cannot be initialized with a value of type "unsigned int"
Aug 2, 2013 at 2:06am
@Czar05:
I think it can help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<Windows.h>
using namespace std;
struct Infomation{
	string name;
	unsigned int age;
	unsigned int duration;
	Infomation(string a, unsigned int x, unsigned int y): 
		name(a), age(x), duration(y){}
};
const double FEE=50.0;
double calculate(Infomation&);
int main()
{
	Infomation Info("abc", 1,4);
	Infomation* p=&Info;
	cout<<calculate(Info)<<endl;
	cout<<calculate(*p)<<endl;	
	system("pause");
	return 0;
}
double calculate(Infomation& t){
	return(t.duration*FEE);
}
Aug 2, 2013 at 3:15pm
@vuonganh1993, thanks for the help. Let me just provide you with the full source code, to give you an idea of what I'm trying to do.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>


using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::getline;

struct Information
{
	string name;
	unsigned int age;
	unsigned int duration;

};

void BFMenu();
void LHMenu();
void DRMenu();
double calculate(Information & Stay);

const int SIZE = 4;
const double FEE = 50.00;
int main()
{
	bool Room[SIZE] = {false, false, false, false};
	bool running = false;
	int meal_chose = 0;
	char payment;
	char reser;
	int i = 0;

	double cost = 0.0;   // For Stay. Cost = Duration * fee; 

	Information * Customer = new Information[SIZE];

	while(!running)
	{
	   Customer[i + 1];  // Move this
	   cout << " -> House Inn Reservation <- " << endl;
	   cout << "Would You Like To Make A Reservation? (Y <Yes> or N <No> )  -> ";
	   cin >> reser;
	   if(reser == 'Y' || reser == 'y')
	   {
	     cout << "Enter Name -> ";
	     getline(cin, Customer->name);
	     cout << "Enter Age -> ";
	     cin >> Customer->age;
	     cout << "Stay Duration -> ";
		 cin >> Customer->duration;
		 cost = calculate(Customer->duration);
		 cout << endl;
		 cout << "Congragulations on your reservation!" << endl;

	   }
	   else if(reser == 'N' || reser == 'n')
		   break;
	   else
	   {
	     cout << "Error! Try Again" << endl;
		 continue;
	   }
	   cout << "Enter Name-> ";
	   getline(cin, Customer->name);
	   cout << "Enter Age-> ";
	   cin >> Customer->age;
	   cout << "Stay Duration: ";
	   cin >> Customer->duration;

	}

	system("Pause");
	return 0;
}

void BFMenu()
{
	cout << "Option #1:    " << endl;
	cout << "A) Eggs, Ham, bacon and hashbrowns" << endl;
	cout << "Option #2:    " << endl;
	cout << "B) Cereal an with apple" << endl;
	cout << "Option #3:    " << endl;
	cout << "C) Waffles/Pancakes with bacon" << endl;
}
void LHMenu()
{
	cout << "Option #1:     " << endl;
	cout << "A) Ceaser Salad " << endl;
	cout << "Option #2:     " << endl;
	cout << "B) Hamburger with fries " << endl; 
	cout << "Option #3:      " << endl;
	cout << "C) Turkey/ Roast Beef sandwich " << endl;

}
void DRMenu()
{
	cout << "Option #1:    " << endl;
	cout << "A) Chicken Piccota" << endl;
	cout << "Option #2:    " << endl;
	cout << "B) Pot Roast  " << endl;
	cout << "Option #3:    " << endl;
	cout << "C) Meatloaf   " << endl;
}

double calculate(Information & Stay)
{

    return (Stay * FEE); 
}



NOTE: The program isn't finished.
Aug 2, 2013 at 3:27pm
In your calculate function, Stay is a struct, not a number. If you want to multiply a struct by a number, you'll need to define your own multiplication operator, as the compiler can't possibly know what it means to multiply your struct by a number.

Edit: Did you mean to pass a number into calculate, rather than a struct? Or did you mean to multiply a specific member of your struct by FEE?

Edit 2: Also, you've defined calculate to take a struct as an argument, but on line 53, you're attempting to call it with an int as the argument.
Last edited on Aug 2, 2013 at 3:32pm
Aug 2, 2013 at 4:53pm
Wow, thanks @MikeyBoy. I understand what my error was now. I needed to pass the actual member function instead of the all structure...like what you said. In hindsight, this problem was rather silly now that I think about it.

@MikeyBoy
Are you capable of making your own mathematical operators in C++? I think I have seen this in action with classes but I am not a 100% sure.
Aug 2, 2013 at 4:59pm
You're welcome! Yes, you can define your own mathematical operators for your types. You can create them as members of your class, or as free functions that operate on your class.
Aug 2, 2013 at 5:07pm
A little and commonly used mathematical operator which is overridden is the Left Shift operator.
1
2
3
4
int myNumber = 24;
myNumber = myNumber << 1; // Shift myNumber left 1 bit

std::cout << "MyText"; // Calls std::ostream::operator<<(const char*) 
Aug 2, 2013 at 5:07pm
@vuonganh1993 and @aidyszh thanks for the responses and help. I think I could have made it easier on you guys if I gave you guys the full source code. My mistake.
Aug 3, 2013 at 4:37am
One more thing, why is this part of my code being skipped over...
1
2
cout << "Enter Name -> ";
getline(cin, Customer->name);


Is it because that one of the arguments comes from a struct?
I'm also getting an error message that reads
 
Run-Time Check Failure #3 - The variable 'reser' is being used without being initialized. 
Last edited on Aug 3, 2013 at 4:53am
Aug 3, 2013 at 9:26am
One more thing, why is this part of my code being skipped over...

I recommend you step through your code with a debugger, to find out what's happening.
Topic archived. No new replies allowed.