c++ a program

You own shares of 4 different companies. You need to write a program to store the following information of the stocks and then to display it. The information to be stored for each company are:
Company Name
Number of shares
Unit price of the shares
Total Value of the shares of company
The' Total Value of the shares of company' should be calculated as result of Number of shares * Unit price of the shares. All these information except the The' Total Value of the shares of company' should be obtained from the user and should be stored in an array. Once the array has been populated (for all companies), print the complete information about each company and total value of stocks. Here is an example of the output.
Company Number of Shares Unit Price Total Value of the shares of company
Company A 100 10 1000
Company B 200 10.5 2100
Company C 81 20 1620
Company D 23.5 10 235
Total Value of Stocks : 4955
create an array of struct.
.
this is what l created but it is not building, from 25 errors l got 8 errors now, above is the question


struct mystruct;

#include <iostream>
#include <conio.h>

using namespace std;
//structure declaration;
struct shares

{

string CompanyName;
string Number_of_shares;
string Unit_price_of_shares;[code]



//function prototype;
void enterCompanyName (shares shares[]);
void printshares (shares shars[]);
//void sortbyCompanyName (shares shares []);
//void sortbyNumber of shares (shares shares[]);
//void sortbyUnitpriceofshares (shares shares[]);

}


{
shares shares[];

int count;

//input the shares
for (int i = 0; i < A; i++)
{
cout << entershares << (i + 1) << ": ";
}

//Display totalshares
cout << "The shares entered are: \n"
<< printshares;
_getch();
return 0;
}

//Functions
//Prints out the array of company
void printshares(shares shares[])
{
for(int i = 0; i < i; i++)
{
cout << i << ":" << /* Error no operator "<<" matches these operands*/ shares[i] << " " << shares[i] << shares[i]<< " ";
}
cout << endl;

}

void enterCompanyName (shares shares[]);
void printshares (shares shars[]);
//void sortbyCompanyName (shares shares []);
//void sortbyNumberofshares (shares shares[]);
//void sortbyUnitpriceofshares (shares shares[]);


return 0


}

can someone pls tell me what is wrong, my code is not bringing out my screen
Last edited on
What is your question?

If you are wondering how to start, begin with what you know, and work from there.

create an array of struct.


1
2
3
4
struct myStruct
{
  ...
};


and store them in a myStruct array.

Considering you included your code after my reply.
Last edited on
i could not understand that what you want to say??
You first need to include <string>

And these variable names will not work since there are spaces in the name.

1
2
string Number of shares;
string Unit price of shares


You need

1
2
string NumberOfShares;
string UnitPriceOfShares


or

1
2
string Number_of_shares;
string Unit_price_of_shares;


And there are many many other problems.
Last edited on
thanks all but l am still having problem building it, right now l have 8 errors from 25 errors showing.
really appreciate you guys. l would not mind if you can still help me succeed in this, this is my first shot in programming and l want to be successful
Post your updated code at this point.
c++ is a my best Programming Language.
struct mystruct;

#include <iostream>
#include <conio.h>

using namespace std;
//structure declaration;
struct shares

{

string CompanyName;
string Number_of_shares;
string Unit_price_of_shares;[code]



//function prototype;
void enterCompanyName (shares shares[]);
void printshares (shares shars[]);
//void sortbyCompanyName (shares shares []);
//void sortbyNumber of shares (shares shares[]);
//void sortbyUnitpriceofshares (shares shares[]);

}


{
shares shares[];

int count;

//input the shares
for (int i = 0; i < A; i++)
{
cout << entershares << (i + 1) << ": ";
}

//Display totalshares
cout << "The shares entered are: \n"
<< printshares;
_getch();
return 0;
}

//Functions
//Prints out the array of company
void printshares(shares shares[])
{
for(int i = 0; i < i; i++)
{
cout << i << ":" << /* Error no operator "<<" matches these operands*/ shares[i] << " " << shares[i] << shares[i]<< " ";
}
cout << endl;

}

void enterCompanyName (shares shares[]);
void printshares (shares shars[]);
//void sortbyCompanyName (shares shares []);
//void sortbyNumberofshares (shares shares[]);
//void sortbyUnitpriceofshares (shares shares[]);


return 0


}

this is what l have but still not building
Start by changing your declarations in your struct to this.

1
2
int Number_of_shares;
double Unit_price_of_shares;


And you haven't declared a size for your array.

And 'A' isn't declared.

And if your inputting data, use cin not cout.

1
2
3
4
5
//input the shares
for (int i = 0; i < A; i++) //A is undeclared
{
cout cin >> shares[i].Number_of_shares (or Unit_price_of_shares);
}


cout << i << ":" << /* Error no operator "<<" matches these operands*/ shares[i]


You need to cout shares[i].Number_of_shares or shares[i].Unit_price_of_shares (unless you overload the operator)
Last edited on
This code works. But is by no means accurate to your expectations. Modify it to what you want.

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
#include <iostream>
#include <conio.h>
using namespace std;
//structure declaration;
struct shares
{

string CompanyName;
int Number_of_shares;
double Unit_price_of_shares;
};

//function prototype;
void enterCompanyName (shares shares[]);
void printshares (shares shars[]);

const int sizeOfArray = 50;
int main()
{
shares shares[sizeOfArray];
int amount; //**You will need to ask the user how many they want to enter.
int count;


//input the shares
for (int i = 0; i < amount; i++)
{
cin >> shares[i].Number_of_shares; // number of shares or unit price
}

//Display totalshares
cout << "The shares entered are: \n"
<< printshares;
_getch();
return 0;
}

//Functions
//Prints out the array of company
void printshares(shares shares[])
{
for(int i = 0; i < i; i++)
{
cout << shares[i].Unit_price_of_shares;
}
cout << endl;

}
Thank you so much, l did modify it but it did build but can not debug saying lm using variable without initializing it
which variable? check where in the file it is telling you there is an error. It should be easy to see and fix.
Last edited on
c4700 error message saying variable amount is being used without being initialized. uninitialized local variable amount used
post code
l dont know how to say thank you to you out there, really appreciate it all.

code posted below

#include <iostream>
#include <conio.h>
using namespace std;
//structure declaration;
struct shares
{

string CompanyName;
int Number_of_shares;
double Unit_price_of_shares;
};

//function prototype;
void enterCompanyName (shares shares[]);
void printshares (shares shars[]);

const int sizeOfArray = 50;
int main()
{
shares shares[sizeOfArray];
int amount; //**You will need to ask the user how many they want to




//input the shares
for (int i = 0; i < amount; i++)

{
cin >> shares[i].Number_of_shares; // number of shares or unit price
}

//Display totalshares
cout << "The shares entered are: \n"
<< printshares;
_getch();
return 0;
}

//Functions
//Prints out the array of company
void printshares(shares shares[])
{
for(int i = 0; i < i; i++)
{
cout << shares[i].Unit_price_of_shares;
}
cout << endl;

}
Are you still getting an error? Because I copied your code and it compiled fine.

What you should do is ask the user how many they want to enter before the loop.

1
2
cout << "How many? ";
cin >> amount;


Your compiler might be preventing you from using it without assigning a value to it.
Last edited on
l need to debug to input some figures but the black screen pops out and when l enter any no it goes away.. it does compile successful but has 1 warning
hello JJJunk,
just want to say thank you for your help. l was able to build and debug it on my own.
appreciate it
Topic archived. No new replies allowed.