Deleting a node with a certain value

I'm writing a program that creates a lists of bikes from a file. Currently, I think I have everything correct up to the sell one bike function. I'm getting some errors and I'm not sure what's going on..
prog3.cpp: In function 'void SellOneBike(Motorcycles*&)':
prog3.cpp:110: error: 'tStockNum' was not declared in this scope
prog3.cpp:111: error: expected primary-expression before '->' token
prog3.cpp:113: error: expected primary-expression before '->' token
prog3.cpp:114: error: expected primary-expression before ';' token
prog3.cpp:115: error: 'Motorcycle' was not declared in this scope
~/cs2020c$


Since I'm new to linked lists, I don't know what these mean. Could someone please help me out here? Here's my program:
See revised program below.

Thank you.

Last edited on
1
2
3
4
struct Motorcycle{
//...
  Motorcycle *next; //this is a bad idea
};
The object should not know that is inside a container. Separate the logic.

1
2
3
4
5
6
7
8
9
10
11
12
void SellOneBike(Motorcycles *&firstMotorcycle)
 {
	indata >> tStockNum; //what is tStockNum? it was never declared
	if (Motorcycles->StockNum == indata >> tStockNum) //Motorcycles is a class, not an object
   {
      firstMotorcycle = Motorcycles->next;
      delete Motorcycles;
      Motorcycles = firstMotorcycle;
      cout << "Bike number " << tStockNum << " has been sold." << endl;
   }
     
}


if (Motorcycles->StockNum == indata >> tStockNum) The insertion operator returns an istream. However, why do you read again?

1
2
3
4
5
6
//typedef node_motorcycle* iterator;
iterator list_motorcycle::find( const Motorcycle & ) const;
list_motorcycle::erase( iterator );

//then you do
m.erase( m.find( Motorcycle(tStockNum) )
Last edited on
Thanks for the info ne555. I've made some changes to my program but I'd like to focus on my SellOneBike function because that's where my errors are coming from.

To be celar, my struct is just supposed to be a struct, not a class. We're just now learning classes.

Here's my program, I've made several changes:
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
113
114
115
116
117
118
119
120
121
//


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;


struct Motorcycles {
	string Manu; 		//Name of manufacture
	string Model;		//Name of bike model
	int StockNum;		//Inventory Number
	string Style;		//Style of bike
	double Price;		//Retail price
	int Hp;			//The amount of horsepower
	Motorcycles* nextBike; //Pointer to next bike
};


Motorcycles *CreateMotorcycle();
Motorcycles *AddToList (Motorcycles* , Motorcycles*);
void SellOneBike (Motorcycles *&);




ifstream indata; //Indata global declaration

int main()
{
	indata.open("prog3st1.txt");
	
	Motorcycles *firstMotorcycle = NULL;
	Motorcycles *newMotorcycle;
	int choice;
	
	indata >> choice; 
	while (choice < 8)
    {
			if (choice == 1)
			{	newMotorcycle = CreateMotorcycle();
				firstMotorcycle = AddToList(firstMotorcycle, newMotorcycle);
			}
			else if(choice == 2)
					SellOneBike(firstMotorcycle);
      		/*else if(choice == 3)
					PrintList(firstMotorcycle);
			else if(choice == 4)
					PriceList(firstMotorcycle);
			else if(choice == 5)
					StyleList(firstMotorcycle);
			else if(choice == 6)
					ManuList(firstMotorcycle); */
			else if(choice == 7)
					exit (0);
			indata >> choice;
    }

	
	

	indata.close();
	return 0;
}

//Create motorcycle function
Motorcycles *CreateMotorcycle()
{
    Motorcycles *newMotorcycle;
    Motorcycles *firstMotorcycle;
    string tbike;
	
while(!indata.eof())
        {       newMotorcycle = new Motorcycles;
                newMotorcycle->nextBike = NULL;
                newMotorcycle->Manu = tbike;
		        indata >> newMotorcycle->Model;
		        indata >> newMotorcycle->StockNum;
			    indata >> newMotorcycle->Style;
		        indata >> newMotorcycle->Price;
			    indata >> newMotorcycle->Hp;
		        AddToList(firstMotorcycle, newMotorcycle);
		        indata >>  tbike;
	    }

    return newMotorcycle;
}

//Add to list function
Motorcycles *AddToList (Motorcycles *firstMotorcycle , Motorcycles *newMotorcycle)
{

	if(!firstMotorcycle)
    firstMotorcycle = newMotorcycle;
   else
   {
      newMotorcycle -> nextBike = firstMotorcycle;
      firstMotorcycle = newMotorcycle;
   }


    cout << endl << newMotorcycle->StockNum << " has been added to the list." <<endl;
    return firstMotorcycle;
 }
//Function used to sell one bike
void SellOneBike(Motorcycles *&firstMotorcycle)
 {
	int tempStockNum;
	indata >> tempStockNum;
	if (Motorcycles->StockNum == tempStockNum)
   {
      firstMotorcycle = Motorcycles->next;
      delete Motorcycles;
      Motorcycles = firstMotorcycle;
      cout << "Bike number " << tempStockNum << " has been sold." << endl;
   }
     
}



Current errors:
~/cs2020c$ g++ prog3.cpp
prog3.cpp: In function 'void SellOneBike(Motorcycles*&)':
prog3.cpp:113: error: expected primary-expression before '->' token
prog3.cpp:115: error: expected primary-expression before '->' token
prog3.cpp:116: error: expected primary-expression before ';' token
prog3.cpp:117: error: expected unqualified-id before '=' token

Last edited on
Topic archived. No new replies allowed.