Clueless and freaking out!!

I'm trying to write a program to check stock exchange prices from a file with a menu similar to a bank's atm. So far all I can figure out is below. First off my boolean for the symbol check looks like crap. Secondly if the file is in a word document how can I get the program to read "current prices" and "yesterday's prices" separately or do I have to have 2 separate files; I don't understand how it reads the char and double like say GOOG 221 230; with the prices respectively.
Actually there's a lot here I don't understand and my C++ for dummies doesn't help.

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
#include <iostream>
using namespace std;
	
class Stock
{
	private:
	
	public:
		void getChangePercent();
		//gets the percent that the current price is different from the previous closing price
	
}


int main()
{
	double previousClosingPrice;
	double currentPrice;
	double share, balance, amount;
	char symbol
	int choice;
	
	cout<< "Please enter the stock symbol.\n"
	cin>> symbol
	while{ 
		if (symbol=="t"||"T"||"AAPL"||"aapl"||"GMCR"||"gmcr"||"goog"||"GOOG"||"HHC"||"hhc"
||"intc"||"INTC"||"JNJ"||"jnj"||"KFT"||"kft"||"MCD"||"mcd"||"sd"||"SD"||"SHS"||
"shs"||"VRUS"||"vrus"||"vz"||"VZ")
			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";
			
			while{
				if (0<choice<8)
					switch (choice){
					case 1:
					//go to buy option
					case 2:
					//go to sell option
					case 3:
					//go to check balance option
					case 4:
					//go to check closing price option
					case 5:
					//go to check current price option
					case 6: 
					getChangePercent();
					//go to check getchangePercent
					case 7:
					//Exit
					cout <<"Thank you for your business.\n";
					}				
				else
				//make return to previous menu
				cout <<"Invalid option.\n";
			}
		else
		cout <<"Invalid option.\n";		
		//return to "Please enter stock symbol"
		
	return 0;
}
 double getChangePercent(){
 getChangePercent()= previousClosingPrice-currentPrice)/previousClosingPrice* 100;
 }
//sell option after figureing out share balance		
{
Cout<<"Please enter in whole numbers the amount of dollars of stock you wish to sell.\n";
cin>> share
if(share<500 || share<balance)
cout<<"Sale invalid.\n";
else cout<<"Congratulations, you have just sold $"<<share<<" worth of stock.\n";
balance=balance-share	
}
A few things for you to consider.

char symbol (missing semi-colon? Lots of your code seems to be missing semi-colons at the end of statements)

symbol is a single char. You then go on to attempt to test it for equality with, for example, AAPL. A single character will never be the same as four characters. You should get to grips with strings, both the C-style array of char with an associated char pointer, and the C++ std::string.

while{

while what? I think you've misunderstood what the while loop is for, and how it works.

You have an if statement, and the only thin that happens if it is true is cout<<"Main Menu\n":

That colon is presumably meant to be a semi-colon. If you want the if statement to trigger more than one statement if it is true, you must use braces, like this:

1
2
3
4
5
6
if (something that might be true)
{
  //do this
  // and this
 // and this as well
}


Best read up on how the if statement works.

if (0<choice<8)

That's just a nonsense. I think you meant

if ((0 < choice) && (choice < 8))
which is if choice is greater than zero, and choice is less than 8.

Your switchyard contains no break; statements. If the choice made is case 1, when the case 1 code is finished, it will go on to do the case 2 code, and the case three code, and on and on until it reaches a break statement or gets to the end.

I advise you to look up how switch and case work, and read some examples.

You've defined a Stock class, but never use it. If you're not going to use it, why have it? I think you've misunderstood how classes work, as you then go on to define the getChangePercent as a function at the end.

All this lot:

1
2
3
4
5
6
7
8
9
//sell option after figureing out share balance		
{
Cout<<"Please enter in whole numbers the amount of dollars of stock you wish to sell.\n";
cin>> share
if(share<500 || share<balance)
cout<<"Sale invalid.\n";
else cout<<"Congratulations, you have just sold $"<<share<<" worth of stock.\n";
balance=balance-share	
}


will never be used and is just hanging around at the end.

I think you've started at too high a level for your experience and knowledge. You might find it easier to start with something simpler and work up, or break this chosen exercise into sub-components and solving each in turn (the divide-and-conquer method of coding, which is often very effective).

Last edited on
Thank you for the help; I'm trying to break it down but it does seem to be overwhelming me. The code you've read above is actually from after my tutor looked at it. So if you have any other recommendations on how to work this including the I/O file I would appreciate it.
Your trying to run the function getChangePercent(); when you dont have one properly defined. You are also missing lots of colons and have bad if and else statements. If you have MSN or teamviewer, I can add you and rewrite this to fix.
I'm afraid I'm on a mac, but I do have team viewer. Do you have yahoo IM?
Last edited on
Okey dokey, let's take a look here. It's easy to get overwhelmed with code when you're learning the syntax at the same time.

Here is a version of your code with a lot of the functionality removed in the interests of making it clear and simple to follow. Please read it, and if there are any parts you don't understand ask again; once you understand what it does, you should be able to then add a little bit at a time to add all the functionality you want. It is so much easier to add little bits to a working programme than to make it all at once and then struggle to even get it to compile :)
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
#include <iostream>
#include <string>  // Let's use C++ strings. They're awfully helpful
using namespace std;

double getChangePercent(); // Have to declare a function before using it
	
int main()
{
  double previousClosingPrice;
  double currentPrice;
  double share, balance, amount;
  string symbol;
  int choice;
	
  cout<< "Please enter the stock symbol.\n";
  cin>> symbol;
	
        // We're going to compare the symbol to a long list of things. We could compare it to 
        //   upper and lower case versions of the same words, or we could ensure that the user's
        //   entered symbol is lower case and save ourselves some code; the less code we write,
       //     the easier it is for us to write, debug and maintain. :)  For now, we're going for ease
        //    of writing, so let's not bring in a function to make it lower case just yet.

      

	if ( (symbol=="t") ||
             (symbol=="T") ||
             (symbol =="AAPL") ||
             (symbol =="aapl") ||
             (symbol =="GMCR") ||
             (symbol =="gmcr") ||
             (symbol =="GOOG") ||
             (symbol =="goog") )  // I've left out the rest, but you get the idea. :)
             { 
                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";

                // Have just given the user some choices. Need to fetch their choice
                cin >> choice;
	
                // We can make it loop until the user chooses to exit later 
                //   let's get it working once first		
	        switch (choice){
				case 1:
				  //go to buy option
                                  cout << "You chose 1" << endl; // To help us see it works
                                  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
                                  cout << "You chose 3" << endl; // To help us see it works
                                  break;
				case 4:
				  //go to check closing price option
                                  cout << "You chose 4" << endl; // To help us see it works
                                  break;
				case 5:
				  //go to check current price option
                                  cout << "You chose 5" << endl; // To help us see it works
                                  break;
				case 6: 
				  getChangePercent();
				  //go to check getchangePercent
                                  cout << "You chose 6" << endl; // To help us see it works
                                  break;
				case 7:
				  //Exit
                                  cout << "You chose 7" << endl; // To help us see it works
				  cout <<"Thank you for your business.\n";
                                  break;
                                default:
                                  // It's good practice to include something in case none of the cases
                                  //     match
                                  cout << "Bad choice " << endl;
		                }				
	        }
		else
                {
		cout <<"Invalid  symbol.\n";
                }		
		
	return 0;
}

// Here, we'll define the function
 double getChangePercent(){
// For now, just make it clear that we are calling the function
cout << "Function has been called" << endl;
return 0.0;
}
Last edited on
Well that did make the code a bit easier to read but I've still got a LOT of errors and warnings. And I have to use classes. Unfortunately it's one of the stipulations. How do I repost with the warnings/errors?
Lots of errors and warnings? Yikes. I didn't think my code was that bad :)

I get lots of warnings about unused variables (double previousClosingPrice; and friends) but no errors.

Customarily, when compiling, an error is something that means it won't compile (or won't link) and a warning is something you might like to look at but doesn't actually break it.

Please check your warnings/errors and tell me if they're all just warnings, or if it actually doesn't compile. It compiles and runs on my system, so if it doesn't compile on yours, I'll be a bit surprised :)
OH yeah big honking red errors.
Can you copy some here for us to examine?

Line Location main.cpp:100: error: 'currentPrice' was not declared in this scope

Line Location main.cpp:100: error: expected `;' before ')' token

Line Location main.cpp:100: error: 'previousClosingPrice' was not declared in this scope

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

Line Location main.cpp:112: error: expected unqualified-id before 'return'

Line Location main.cpp:113: error: expected declaration before '}' token

Yes also got those warnings you mentioned. Do you want me to re-supply the full code incase I missed something?
Last edited on
Given that both your code and my code are less than 100 lines long, and your compiler is finding errors on line 100 and later, I can only surmise that what you're compiling isn't actually the code above. I suggest deleting it all, making sure it's blank, and then copying the above code again.

Last edited on
ok, that works. thank you.
Oh good. If you get stumped for how to add your needed functionality, do come back again.

In the meantime, you can read up a little on classes here:

http://www.cplusplus.com/doc/tutorial/

The syntax of classes is relatively easy; what people sometimes struggle with is the conceptual understanding - that is, having a good way to add classes to their mental problem-solving model.
Last edited on
Topic archived. No new replies allowed.