set and get errors

I set some information using set functions, i call for the set functions in function A. when i get the information using get functions in function A they are the same information. but when i try to get the information i set in A from function B they are not the same.
what can be wrong in my code?

func A:
iter2->setCost(c);

func B:
iter2->getcost()

the implementations:
1
2
3
void Show::setCost(int num){
	cost=num;
}


1
2
3
 int Show::getcost() const{
	 return cost;
 }
Last edited on
Can you show us more of the code with iter2 ?
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
void Auditorium::AddShow(int day,int month,int year) {
	string n,name;
	int c;
	list<Show>::iterator iter2=listOfAllShows.begin();
	list<Concert>::iterator iter=Stamlist.begin();
	list<Customer>::iterator iter3=customers.begin();
	bool i=SearchShow(year,month,day);
	

	cout<< "Is it a Show or a Concert?"<<endl;
	cin>>n ;

	if(n=="Concert" || n=="concert" ||n=="CONCERT" ||n=="Show" ||n=="show" ||n=="SHOW")
	{		
		if (n=="Concert" || n=="concert" ||n=="CONCERT") {
			cout<<"Please insert Concert's name:"<<endl;
			cin>>name;
			
			cout<<"Please insert ticket's cost:"<<endl;
			cin>>c;
		
			/* set information */
			iter->setConame(name);
			iter3->setprice(c);
			iter2->setCost(c);
			iter->SetMusicians();
			cout<<endl;
			cout<< "Enter instruments: "<<endl;
			iter->getinstruments();
			cout<<endl<<"You have added a Concert!"<<endl;
			cout<<"Show's date is:"<<day<<"/"<<month<<"/"<<year<<endl;
			cout<<"Show's name is:"<<name<<endl;
			cout<<"The ticket costs:"<<c<<endl;
			iter->printmusicians();
			cout<<endl<<endl<<endl;
And where do you use iter2->getCost() ?
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
void Auditorium::CancelReservation(long id,int day,int month, int year) {
	/*בהינתן מספר זהות ותאריך מופע - המערכת תבקש את מספר הכרטיסים לביטול ,ותדפיס את
	גובה סכום ההחזר הכספי.*/
	int n,i;
	list<Customer>::iterator iter=customers.begin();
	list<Show>::iterator iter2=listOfAllShows.begin();

		// search for the customer
	iter=find(customers.begin(),customers.end(),id);
	// check if the customer is not found
	 if(iter==customers.end())
	 {
		 throw CustomerNotFound(); // throw exception
	 }

	 cout<<"**************Cancel Reservation**************"<<endl;
	cout<<"How many tickets do you want to cancel?: ";
	cin>>n;

	if(n<=0 || n>200)
	{
		throw numOftickets();// throw exception
	}

	 // check whether there's a show 
	if(SearchShow(year,month,day)==1)
	{
		ShowNotFound();// throw exception
	}

	//editing the number of the sold tickets:
	for(i=1;i<=n;i++)
		iter2->setSold();

	//Is the customer a special customer?
	if(searchforspecialcustomer(id) ==1) //if he is a special customer:
	{
		double sale=iter->getSale();
		cout<<"Refund amount="<<n*(iter2->getcost())*sale<<endl;
	}
	else //if he is not a special customer:
	{
		cout<<"Refund amount="<<n*iter2->getcost()<<endl;
	}

}
When you checked the value returned from iter2->getcost(), did you remember to account for n and sale?

And you are sure that between the time you call AddShow and CancelReservation, you didn't insert any elements to the front of listOfAllShows?
Why inserting elements to the front of the list can make a problem?
the only places i insert elements to this list is in AddShow and the c'tor.
Because you assign iter2 to start at the beginning of the list. If you've inserted elements to the front of the list between your calls, *iter2 would of course be different. There's a different object at the front of the list.
Can the c'tor be a problem?
AddShow doesn't insert any new elements to the list. It assigns new values to the first object in the list.

So maybe you called AddShow more than once, replacing your object each time by the time you called CanvelReservation.
AddShow does add. this is the full function:
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
void Auditorium::AddShow(int day,int month,int year) {
	string n,name;
	int c;
	list<Show>::iterator iter2=listOfAllShows.begin();
	list<Concert>::iterator iter=Stamlist.begin();
	list<Customer>::iterator iter3=customers.begin();
	bool i=SearchShow(year,month,day);
	
	// check if the date is availble to add a show in it
	if(i==0)
	{
	//	throw DateTaken(); // throw exception
		cout<<"Wrong date"<<endl;
	}
	else{

	cout<< "Is it a Show or a Concert?"<<endl;
	cin>>n ;

	if(n=="Concert" || n=="concert" ||n=="CONCERT" ||n=="Show" ||n=="show" ||n=="SHOW")
	{		
		if (n=="Concert" || n=="concert" ||n=="CONCERT") {
			cout<<"Please insert Concert's name:"<<endl;
			cin>>name;
			
			cout<<"Please insert ticket's cost:"<<endl;
			cin>>c;
		
			/* set information */
			iter->setConame(name);
			iter3->setprice(c);
			iter2->setCost(c);
			iter->SetMusicians();
			cout<<endl;
			cout<< "Enter instruments: "<<endl;
			iter->getinstruments();
			cout<<endl<<"You have added a Concert!"<<endl;
			cout<<"Show's date is:"<<day<<"/"<<month<<"/"<<year<<endl;
			cout<<"Show's name is:"<<name<<endl;
			cout<<"The ticket costs:"<<c<<endl;
			iter->printmusicians();
			cout<<endl<<endl<<endl;
			
		}

		else {
			cout<<"Please insert Show's name:"<<endl;
			cin>>name;
		
			cout<<"Please insert ticket's cost:"<<endl;
			cin>>c;
		
			/*set information*/
			iter2->setCost(c);
			iter3->setprice(c);
			iter2->setShowName(name);
			iter2->SetPerformers();
			cout<<endl;
			cout<<"You have added a show!"<<endl;
			cout<<"Show date is:"<<day<<"/"<<month<<"/"<<year<<endl;
			cout<<"Show's name is:"<<name<<endl;
			cout<<"The ticket costs:"<<c<<endl;
			iter2->printperformers();
			cout<<endl<<endl<<endl;
			
		}

		//Add show to the list that contains all the shows:
		Show* newShow= new Show(day,month,year);
		this->listOfAllShows.push_front(*newShow);
		int costis=iter2->getcost();
	}
Ok, I see.

Lines 69 to 72:
You pushed a new object to the front of the list. When you call getCost in CancelReservation, the value will appear different than when you call getCost() in AddShow. Is this what you meant when you said the values were different?
I see no reason for you to allocate memory on line 69, especially since you forgot to free the memory. You can simply have:
1
2
3
4
5
6
Show newShow(day, month, year);
this->listOfAllShows.push_front(newShow);

//Or...

this->listOfAllShows(Show(day, month, year));


Edit:
Your AddShow could be shortened a bit...
-For example, you check if n is either concert or show then if n is concert or show. You would do away with the if condition on line 20 and just have your if else as is (or with another if in case n != "Show").
-I don't get why you couldn't push the new element to the front first before asking the user for information to enter. Instead you have to call AddShow twice in order to push a new element and then assign it new values. And even then, you'd end up with an extra object at the front of the list.
-I would think you would have lines 7 to 15 at the beginning of the function.
-You search if the date has already been taken, but you don't do anything with that information until the very end. Again, you should be inserting the new element first, then having the user enter its information immediately.
-The variable on line 71 seems pointless...
Last edited on
I still get random cost. even after changing lines 69-71 to what you suggested.
And thank you for the improvements.
1
2
3
4
5
6
Auditorium aud; //Assuming the constructor automatically pushes one object
aud.AddShow(1, 2, 3); //Assign new values to first object.
//      Afterward, push a new object to the front of the list. However, the members
//     like cost will be left unitialized
aud.CancelReservation(12, 1, 2, 3); //Assuming the ID is correct
//shows a random cost. Why? Because the object was given no values 


Am I close?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Auditorium::Auditorium(int num){

	Show* newshow= new Show(0,0,0); //Adding the new show to the list of show
	this->listOfAllShows.push_front(*newshow);

	Concert* newconcert= new Concert(); //Adding the new concert to the list of show
	this->Stamlist.push_front(*newconcert);

	Customer* newcus= new Customer("",0); //Adding the new concert to the list of show
	this->customers.push_front(*newcus);
}

Auditorium::~Auditorium(void)
{
	customers.erase(customers.begin(),customers.end());
	specialCustomers.erase(specialCustomers.begin(),specialCustomers.end());
	listOfAllShows.erase(listOfAllShows.begin(),listOfAllShows.end());
}


I had to push a new object so I can initialize the iterator later to list.begin(), otherwise i get a run time error telling me that the iterator is not initialized.
and Auditorium() doesn't do anything. (i dont use it i just added it in case i'll need it)

and yes, cost is unitialized since cost=0; is a compilation error.
...cost is unitialized since cost=0; is a compilation error

I can't tell if you're being sarcastic...


Anyway, then take my suggestions.
1
2
3
4
5
6
void Auditorium::AddShow(int day, int month, int year){
   this->listOfAllShows.push_front(Show(day, month, year));
   auto iter2 = listOfAllShows.begin();
//...Declare other variables...
   //Proceed to ask user for info like cost and assign those values to *iter2 instead of the previous object
}


3
4
5
6
7
8
9
10
11
	Show* newshow= new Show(0,0,0); //Adding the new show to the list of show
	this->listOfAllShows.push_front(*newshow);

	Concert* newconcert= new Concert(); //Adding the new concert to the list of show
	this->Stamlist.push_front(*newconcert);

	Customer* newcus= new Customer("",0); //Adding the new concert to the list of show
	this->customers.push_front(*newcus);

You seriously don't need memory allocation here. It adds that bit of extra slowness, and your current code leaves three memory leaks (you don't free the allocated memory).
Last edited on
assign those values to *iter2 instead of the previous object
can you give an example of what you referred to in this line?
Let's visualize the list:

After creating an Auditorium object:
listOfAllShows --> {Show(date(0, 0, 0), cost == random number)}
One Show object is created.



Now let's call your first version of Auditorium::AddShow, and let's assume
the user entered 200 for the cost and 1, 2, 3 for the date:
1) Assign new cost to the first object in the list
listOfAllShows --> {Show(date(0, 0, 0), cost == 200)}
2) Now create a new object given the new date
listOfAllShows --> {Show(date(1, 2, 3), cost == random number), Show(date(0, 0, 0), cost == 200)}



Calling CancelReservation
Display the cost of the first Show in the list:
iter2 --> Show(date(1, 2, 3), cost == random number)
iter2->getCost() == random number



Now let's say you changed AddShow first to push to the front a new object. 
Again, let's use 200 for the cost and 1, 2, 3 for the date.
1) Create a new object given the new date
listOfAllShows --> {Show(date(1, 2, 3), cost == random number), Show(date(0, 0, 0), cost == random number)}
2) Now assign new cost to the first object in the list
listOfAllShows --> {Show(date(1, 2, 3), cost == 200), Show(date(0, 0, 0), cost == random number)}



Calling CancelReservation
Display the cost of the first Show in the list:
iter2 --> Show(date(1, 2, 3), cost == 200)
iter2->getCost() == 200
Last edited on
OMGGGGG it worked!!! Thank youuu thank youu

finally!! You are amazing.
Have a nice day.
Topic archived. No new replies allowed.