switch statement and functions
Sep 7, 2018 at 3:24am UTC
i am trying to do functions with swtichstatement iam having a hard time. please 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 80 81 82 83 84 85 86 87 88 89
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct StockInfo
{
string coname;
float numShares;
float PurPrice;
float CurrPrice;
};
void addRecord (struct StockInfo, int size);
void displayResult ( struct StockInfo, int size);
int main()
{
const int size =2;
StockInfo portfolio[size];
int count=0;
int choice;
do
{
cout << endl
<< " 1 - Add a stock.\n"
<< " 2 - Display Profit/loss.\n"
<< " 3 - Exit Program.\n"
<< " Enter your choice and press return: " ;
cin >> choice;
switch (choice)
{
case 1:
void addRecord (struct StockInfo portfolio,int size)
{
cout << "enter company's name please:" <<endl;
cin>>portfolio[i].coname;
cout << "enter the number of shares bought:" <<endl;
cin >> portfolio[i].numShares;
cout <<"What was the purchase price?" <<endl;
cin >> portfolio[i].PurPrice;
cout << "what is the current price of the share?" <<endl;
cin >> portfolio[i].CurrPrice;
cin.ignore();
count++
}
break ;
case 2:
void displayResult (struct StockInfo portfolio,int size)
{
cout<<"Portfolio Report\n\n================" <<endl;
cout<<"Company\t\tProfit(Loss)" <<endl;
for (int i=0; i< size; i++)
{
double Profitloss= portfolio[i].numShares * (portfolio[i].CurrPrice-portfolio[i].PurPrice);
cout<<portfolio[i].coname<<setw(15)<<"$" <<setw(5)<<Profitloss<<endl;//cout<<"Your total Profitloss is "<<Profitloss<<endl;
}
//Number of shares * (current price – purchase price)
break ;
case 3:
//Exit the program
break ;
default :
cout << "Not a Valid Choice. \n" << "Choose again.\n" ;
break ;
}
}
while (choice !=3);
return 0;
}
Sep 7, 2018 at 7:07am UTC
You are not allowed to define functions inside other functions.
Move the function inside the switch statements outside of main and just call the functions from main.
Sep 7, 2018 at 8:15am UTC
"hard time" is an inaccurate description of the problem.
The compiler lists errors and warnings. It is useful to read and understand them.
For example, I did pull the addRecord() outside, as Thomas suggested.
The compiler sees these issues in it:
warning: unused parameter 'size' [-Wunused-parameter]
error: 'i' was not declared in this scope
error: 'count' was not declared in this scope
1 2 3 4 5 6 7
void addRecord (struct StockInfo portfolio,int size ) // warn
{
cout << "enter company's name please:" <<endl;
cin>>portfolio[i ].coname; // error
// more of those
count ++ // error
}
The compiler got a bit confused though; it does not mention that there is no
;
after the
count++
Two notes:
1. Your indentation is not systematic. That affects readability.
2. C++ does not require
struct
when you do use typename:
1 2
void addRecord( struct StockInfo, int );
void addRecord( StockInfo, int ); // sufficient
Topic archived. No new replies allowed.