Bunch of new questions

Ok, so I'm making this code and I keep trying to make 'checkBalance' a function but it's not coming out right. I have a massive amount of errors and 1 warning that I need help fixing.

Also I'm not quite sure how to make the buyStock function pull the currentPrice of a certain stock from a file so I can add it to the balance from another file and write the new current balance out to a new file. I know I'm doing something wrong but I'm not sure what.

I'll be up all night if anyone can help me.
Thanks in advance;
~A


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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <string>
#include <fstream>

int sellStock();

using namespace std;

class stock{
	
public:
	stock();
	stock(string symbol);
	
	// The properties of the class, often known as the member variables
	std::string nameOfStock;
	double currentPrice;
	double previousClosingPrice;
	double share, balance, amount;
	string symbol;
	
	// The methods I want to be able to call on the class
	// For the difference in value, I should not need to provide any input when I make the 
	//   function call; the object should keep track of its own member variables.
	double getChangePercent();      
};

stock::stock()
{
};

stock::stock(string inputSymbol)
{
    // Lots of code to fetch values etc.
	ifstream in("stock.txt");
	if (in.is_open())
    {
		while(symbol!=inputSymbol)
        {
			in >> symbol;
			in >> previousClosingPrice;
			in >>currentPrice;
        }
    }
	else
    {
		cout << "Cannot open file" << endl;
    }

};


double stock::getChangePercent()
{
	cout << "You have called getChangePercent on the stock object " << endl;
	return 0.0;
};
struct stockAndAmount{
	string stockName;
	int amountOfStock;	
};
	class account{
	public:
	stockAndAmount stock1;
	stockAndAmount stock2;
	stockAndAmount stock3;
};
	account buyStock(account inputAccount);

int main()
{
	ifstream in;
	string userAccount;
	stock userStock;
	
	cout<< "Please enter your four digit account number.\n";

	cin>> userAccount;
	
	if (userAccount =="0000"){
		
		in.open("./Account_0000.txt");
		if (in.is_open()){
		cout <<"Opened account"<<endl;
			//test for file opening
		}
	}		
	else
		if (userAccount=="1234"){
			ifstream in("Account_1234.txt");
		}
		else{
		cout<< "Bad choice " << endl;
		}
		
		// User has chosen their account.
	account usersActualAccount;
	usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.amountOfStock;
	in>> usersActualAccount.stock2.stockName;
	in>> usersActualAccount.stock2.amountOfStock;
	in>> usersActualAccount.stock3.stockName;
	in>> usersActualAccount.stock3.amountOfStock;
	
	// Have just given the user some choices. Need to fetch their choice
		int choice;
		choice=0;
		
		// We can make it loop until the user chooses to exit later 
		//   let's get it working once first
	while (choice!=7){
		cout<<"Main Menu\n";
		cout<<"1: Buy\n";
		cout<<"2: Sell\n";
		cout<<"3: Check balance\n";
		cout<<"4: Check closing price\n";
		cout<<"5: Check current price\n";
		cout<<"6: Check percent change\n";
		cout<<"7: Exit\n";
		
		cin >> choice;
		switch (choice){
			case 1:
				//go to buy option
				buyStock(usersActualAccount);
				break;
			case 2:
				//go to sell option
				cout << "You chose 2" << endl; // To help us see it works
				break;
			case 3:
				//go to check balance option
				checkBalance(usersActualAccount);

				break;
			case 4:
				//go to check closing price option
				cout << userStock.previousClosingPrice << endl; // To help us see it works
				break;
			case 5:
				//go to check current price option
				cout << userStock.currentPrice << endl; // To help us see it works
				break;
			case 6: 
				//go to check getchangePercent
				cout << userStock.getChangePercent() << endl;
				break;
			case 7:
				//Exit
				cout << "You chose to Exit." << endl;
				cout <<"Thank you for your business.\n";
				break;
			}
		}  
	return 0;
}
account usersActualAccount;
account checkBalance(usersActualAccount);	{
	
//brings in users account data from file	
	usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.amountOfStock;
	in>> usersActualAccount.stock2.stockName;
	in>> usersActualAccount.stock2.amountOfStock;
	in>> usersActualAccount.stock3.stockName;
	in>> usersActualAccount.stock3.amountOfStock;
	
	//shows user their balance	
	cout<< usersActualAccount.stock1.stockName<<endl;
	cout<< usersActualAccount.stock1.amountOfStock<<endl;
	cout<< usersActualAccount.stock2.stockName<<endl;
	cout<< usersActualAccount.stock2.amountOfStock<<endl;
	cout<< usersActualAccount.stock3.stockNamee<<endl;
	cout<< usersActualAccount.stock3.amountOfStock<<endl;
}	
	in.open("stock.txt");
	in >> symbol;
	in >> previousClosingPrice;
	in >>currentPrice;
	int buyStock();
	double numberOfStock;
	account buyStock(account inputAccount){
	string userInput;
	stock userStock;
	cout<< "Which stock would you like to buy AAPL, GOOG or INTC?\n";
	userStock.symbol = userInput;
	cin>> userInput;
	
	if ((userInput =="AAPL") ||
		(userInput =="aapl") ||
		(userInput =="GOOG") ||
		(userInput =="goog") ||
		(userInput =="INTC") ||
		(userInput =="intc")){
		
		stock userStock(userInput);
		userStock.symbol = userInput;
	
	else{
		cout<< "Bad choice " << endl;
		break;
		}
	cout<<"How many shares would you like to buy?\n";
	cin>> numberOfStock	
		balance=balance+(numberOfStock*currentPrice)
		cout <<"Your new balance is "<< balance<< endl;
		
	}
		return inputAccount;
}

Line Location main.cpp:211: error: expected declaration before '}' token
Line Location main.cpp:210: error: expected unqualified-id before 'return'
Line Location main.cpp:209: warning: control reaches end of non-void function
Line Location main.cpp:206: error: expected `;' before 'balance'
Line Location main.cpp:202: error: break statement not within loop or switch
Line Location main.cpp:200: error: expected `}' before 'else'
Line Location main.cpp:180: error: expected constructor, destructor, or type conversion before '>>' token
Line Location main.cpp:179: error: expected constructor, destructor, or type conversion before '>>' token
Line Location main.cpp:178: error: expected constructor, destructor, or type conversion before '>>' token
Line Location main.cpp:177: error: expected constructor, destructor, or type conversion before '.' token
Line Location main.cpp:159: error: expected ',' or ';' before '{' token
Line Location main.cpp:134: error: 'checkBalance' was not declared in this scope





Check your braces to make sure the all match up. In particular, line 159 looks suspicious.
Line Location main.cpp:134: error: 'checkBalance' was not declared in this scope

You don't have a function named checkBalance defined anywhere in the program or class yet. Therefore it can't find it.


Line Location main.cpp:159: error: expected ',' or ';' before '{' token

You are declaring an object of account type, named checkBalance, and passing it an object of account type named usersActualAccount - basically a call to a copy constructor here.


Line Location main.cpp:200: error: expected `}' before 'else'


You need to put a '}' before the else to end the scope of the if.

Line Location main.cpp:202: error: break statement not within loop or switch


You don't need the break; statement since you're not in a loop or switch statement, once the '}' is found, it's going to continue through the program anyways.

Line Location main.cpp:206: error: expected `;' before 'balance'


Line 205 doesn't have a semicolon:
cin>> numberOfStock


These are the ones I can see off the top of my head, but I have a feeling someone else has beat me to them. Get those fixed first, and I might be able to help you on the others.




Thanks folks; ok I did my best to understand this all and fix the errors. Of course new ones popped up so here's the rewrite:
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <string>
#include <fstream>

int sellStock();

using namespace std;

class stock{
	
public:
	stock();
	stock(string symbol);
	
	// The properties of the class, often known as the member variables
	std::string nameOfStock;
	double currentPrice;
	double previousClosingPrice;
	double share, balance, amount;
	string symbol;
	
	// The methods I want to be able to call on the class
	// For the difference in value, I should not need to provide any input when I make the 
	//   function call; the object should keep track of its own member variables.
	double getChangePercent();      
};

stock::stock()
{
};

stock::stock(string inputSymbol)
{
    // Lots of code to fetch values etc.
	ifstream in("stock.txt");
	if (in.is_open())
    {
		while(symbol!=inputSymbol)
        {
			in >> symbol;
			in >> previousClosingPrice;
			in >>currentPrice;
        }
    }
	else
    {
		cout << "Cannot open file" << endl;
    }

};


double stock::getChangePercent()
{
	cout << "You have called getChangePercent on the stock object " << endl;
	return 0.0;
};
struct stockAndAmount{
	string stockName;
	int amountOfStock;	
};
	class account{
	public:
	stockAndAmount stock1;
	stockAndAmount stock2;
	stockAndAmount stock3;
};
	account buyStock(account inputAccount);

int main()
{
	ifstream in;
	string userAccount;
	stock userStock;
	
	cout<< "Please enter your four digit account number.\n";

	cin>> userAccount;
	
	if (userAccount =="0000"){
		
		in.open("./Account_0000.txt");
		if (in.is_open()){
		cout <<"Opened account"<<endl;
			//test for file opening
		}
	}		
	else
		if (userAccount=="1234"){
			ifstream in("Account_1234.txt");
		}
		else{
		cout<< "Bad choice " << endl;
		}
		
		// User has chosen their account.
	account usersActualAccount;
	usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.amountOfStock;
	in>> usersActualAccount.stock2.stockName;
	in>> usersActualAccount.stock2.amountOfStock;
	in>> usersActualAccount.stock3.stockName;
	in>> usersActualAccount.stock3.amountOfStock;
	
	// Have just given the user some choices. Need to fetch their choice
		int choice;
		choice=0;
		account checkBalance(usersActualAccount);
		// We can make it loop until the user chooses to exit later 
		//   let's get it working once first
	while (choice!=7){
		cout<<"Main Menu\n";
		cout<<"1: Buy\n";
		cout<<"2: Sell\n";
		cout<<"3: Check balance\n";
		cout<<"4: Check closing price\n";
		cout<<"5: Check current price\n";
		cout<<"6: Check percent change\n";
		cout<<"7: Exit\n";
		
		cin >> choice;
		switch (choice){
			case 1:
				//go to buy option
				buyStock(usersActualAccount);
				break;
			case 2:
				//go to sell option
				cout << "You chose 2" << endl; // To help us see it works
				break;
			case 3:
				//go to check balance option
				checkBalance(usersActualAccount);

				break;
			case 4:
				//go to check closing price option
				cout << userStock.previousClosingPrice << endl; // To help us see it works
				break;
			case 5:
				//go to check current price option
				cout << userStock.currentPrice << endl; // To help us see it works
				break;
			case 6: 
				//go to check getchangePercent
				cout << userStock.getChangePercent() << endl;
				break;
			case 7:
				//Exit
				cout << "You chose to Exit." << endl;
				cout <<"Thank you for your business.\n";
				break;
			}
		}  
	return 0;
}
account usersActualAccount;
account checkBalance(usersActualAccount);
{
	
//brings in users account data from file	
	usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.stockName;
	in>> usersActualAccount.stock1.amountOfStock;
	in>> usersActualAccount.stock2.stockName;
	in>> usersActualAccount.stock2.amountOfStock;
	in>> usersActualAccount.stock3.stockName;
	in>> usersActualAccount.stock3.amountOfStock;
	
	//shows user their balance	
	cout<< usersActualAccount.stock1.stockName<<endl;
	cout<< usersActualAccount.stock1.amountOfStock<<endl;
	cout<< usersActualAccount.stock2.stockName<<endl;
	cout<< usersActualAccount.stock2.amountOfStock<<endl;
	cout<< usersActualAccount.stock3.stockNamee<<endl;
	cout<< usersActualAccount.stock3.amountOfStock<<endl;
}	
	
	in.open("stock.txt");
	in >> symbol;
	in >>currentPrice;
	int buyStock();
	double balance;
	double numberOfStock;
	double currentPrice;
	account buyStock(account inputAccount){
	string userInput;
	stock userStock;
	cout<< "Which stock would you like to buy AAPL, GOOG or INTC?\n";
	userStock.symbol = userInput;
	cin>> userInput;
	
	if ((userInput =="AAPL") ||
		(userInput =="aapl") ||
		(userInput =="GOOG") ||
		(userInput =="goog") ||
		(userInput =="INTC") ||
		(userInput =="intc")){
		
		stock userStock(userInput);
		userStock.symbol = userInput;
	}
	else{
		cout<< "Bad choice " << endl;
	};
	cout<<"How many shares would you like to buy?\n";
		cin>> numberOfStock;	
		balance=balance+(numberOfStock*currentPrice);
		cout <<"Your new balance is "<< balance << endl;
		
	}
		return inputAccount;
}

Line Location main.cpp:214: error: expected declaration before '}' token
Line Location main.cpp:213: error: expected unqualified-id before 'return'
Line Location main.cpp:212: warning: control reaches end of non-void function
Line Location main.cpp:182: error: expected constructor, destructor, or type conversion before '>>' token
Line Location main.cpp:181: error: expected constructor, destructor, or type conversion before '>>' token
Line Location main.cpp:180: error: expected constructor, destructor, or type conversion before '.' token
Line Location main.cpp:160: error: expected unqualified-id before '{' token
Line Location main.cpp:134: error: no match for call to '(account) (account&)'

I also checked the braces for line 159 but it still looked like it matched up. I'm not sure where it's mixed up at.
Okay...

Line Location main.cpp:134: error: no match for call to '(account) (account&)'

You still don't have a function/method called checkBalance, basically the program is interpreting it as a call to a copy constructor to create an account object named checkBalance that will be populated with the values of usersActualAmount. While debugging the rest of it and/or until you get that function/method written, you might want to comment that out.

Line Location main.cpp:160: error: expected unqualified-id before '{' token

I think this has a similar tie-in to the previous one. I would also comment this out for the moment.

Line Location main.cpp:180: error: expected constructor, destructor, or type conversion before '.' token
Line Location main.cpp:181: error: expected constructor, destructor, or type conversion before '>>' token
Line Location main.cpp:182: error: expected constructor, destructor, or type conversion before '>>' token

I may be wrong, but just eyeballing your {s and }s, I would say that your ifstream in object goes out of scope on Line 157, which means it's no longer available for use. If this part is supposed to be in the main function, you want to remove that } on line 157 if you're still wanting to be within main. That is why you're getting these three errors.

Line Location main.cpp:212: warning: control reaches end of non-void function
Line Location main.cpp:213: error: expected unqualified-id before 'return'
Line Location main.cpp:214: error: expected declaration before '}' token


These are also caused by that } on line 157.

With these tweaks...if you don't comment out the checkBalance things, I would guess those would be the only remaining errors.
Okay... I see what you're doing. On line 157, you are ending the main function. This is fine in that you're defining checkBalance right after. However, you want to remove the semicolon afterwards, and do it that way


For it to work, you would change:

account checkBalance(usersActualAccount); {

to

void/int/bool/etc account::checkBalance(account& userAccount) {

Also, you would need to define another ifstream object, for this function, and make it a member of the account class so that it has access to account's members.

There would still be some things you need to do, but that should get you on the right track.
Last edited on
Okay I am so beginner it's not funny. To write a function for checkBalance would it look like this?:
checkBalance <double> (usersActualAccount);

Also I tried to take out the } on line 157 but that brought up a host of all new errors also that portion of the code is not supposed to be connected, but a new function that I'm trying to define so I can call it in the Menu.
Maybe I'm misunderstanding the code??
Last edited on
ok so I changed the file coding now all I have to do is declare inputAccount; but I'm not sure what to declare it as as it's supposed to be altering the account file that it came from.
1
2
3
4
	
	}
		return inputAccount;
}
Last edited on
Topic archived. No new replies allowed.