My assignment is to create a simple stock broker program that ask the user how much they are willing to invest and ask what company they would like to invest in. Finally it outputs how many shares the user will have based on their investment amount. My code is below. My professor said to declare symbolic constants and factor out the if else statements. Ive been struggling trying to understand constant variables. How do I use const variables to factor out the if else statements?
// Give user choices of stocks to choose from and ask how much they are willing to invest
cout << "Welcome to my Stock Broker program." << endl;
cout << "You can buy shares of the following companies" << endl;
cout << " Bank of America at $ 16.70 per share" << endl;
cout << " Citigroup at $ 49.52 per share" << endl;
cout << " Facebook at $ 67.09 per share" << endl;
cout << " GM at $ 35.95 per share" << endl;
cout << " Microsoft at $ 37.64 per share" << endl;
cout << " Tesla at $ 198.23 per share" << endl;
cout << " Samsung at $ 1250 per share" << endl;
cout << " Visa at $ 226 per share \n" << endl;
cout << "Please enter the amount you want to invest" << endl;
cin >> investment;
// Ask user what company they would like to invest in and ask for one character input
cout << "Please select the company whose shares you want to purchase." << endl;
cout << "Enter b/B for Bank of America" << endl;
cout << " c/C for Citigroup" << endl;
cout << " f/F for Facebook" << endl;
cout << " g/G for General Motors" << endl;
cout << " m/M for Microsoft" << endl;
cout << " t/T for Tesla" << endl;
cout << " s/S for Samsung" << endl;
cout << " v/V for Visa" << endl;
cin >> stock;
// characters correspond to a company and divide investment by company's share price
if ((stock == 'c') || (stock == 'C')) {
result = investment / Citigroup;
cout << "With $ " << investment << " you can purchase " << result << " shares of Citigroup at $ 49.52 per share" << endl;
}
else if ((stock == 'f') || (stock == 'F')) {
result = investment/ facebook;
cout << "With $ " << investment << " you can purchase " << result << " shares of Facebook at $ 67.09 per share" << endl;
}
else if ((stock == 'g') || (stock == 'G')) {
result = investment / GM;
cout << "With $ " << investment << " you can purchase " << result << " shares of GM at $ 35.95 per share" << endl;
}
else if ((stock == 'm') || (stock == 'M')) {
result = investment / microsoft;
cout << "With $ " << investment << " you can purchase " << result << " shares of Microsoft at $37.64 per share" << endl;
}
else if ((stock == 't') || (stock == 'T')) {
result = investment / Tesla;
cout << "With $ " << investment << " you can purchase " << result << " shares of Tesla at $ 198.23 per share" << endl;
}
else if ((stock == 's') || (stock == 'S')) {
result = investment / Samsung;
cout << "With $ " << investment << " you can purchase " << result << " shares of Samsung at $ 1250 per share" << endl;
}
else if ((stock == 'b') || (stock == 'B')) {
result = investment / BAC;
cout << "With $ " << investment << " you can purchase " << result << " shares of BAC at $ 16.7 per share" << endl;
}
else if ((stock == 'v') || (stock == 'V')) {
result = investment / Visa;
cout << "With $ " << investment << " you can purchase " << result << " shares of Visa at $ 226 per share" << endl;
}
else if ((stock == 'v') || (stock == 'V')) {
result = investment / Visa;
cout << "With $ " << investment << " you can purchase " << result << " shares of Visa at $ 226 per share" << endl;
}
else if ((stock == 'v') || (stock == 'V')) {
result = investment / Visa;
cout << "With $ " << investment << " you can purchase " << result << " shares of Visa at $ 226 per share" << endl;
}
else {
cout << "Invalid input, please restart program" << endl;
}
cout << "Thank you for using my stock broker program!" << endl;
With constant variables for the stock prices, I'd probably try to use the variable name in output statements further down the program instead of hard-coding the prices. That way if the prices change later, you only have to update it in the constant variable declaration - not a lot of different places throughout the code.