Additional C++ Help

Hi everyone,

I am currently attempting and trying to familiarize myself with c++. I am very new and trying my best with online research and videos.

I am currently trying to run a program to make it look like this:

Dept Average Above Below Performance
-------------------------------------------------------------------------------
1 37.6 7 5 unsatisfied
2 38.6 8 4 satisfied
3 44.9 7 5 unsatisfied
4 51.1 8 4 satisfied
5 43.0 7 5 unsatisfied
6 57.6 11 1 satisfied
7 48.2 9 3 satisfied

My program looks like this:

The following are all the possible stocks to buy:
Dept Average Above Below
-----------------------------------------------------------
.5 24.00 35.20 24
26 43.00 56.70 54
57.9 24.00 42.00 43
35 52.00 56.00 67
32 20.00 32.00 45
72 45.40 63.20 45
56 52.00 65.00 53
65 34.00 35.00 37
31 43.00 52.00 43
63.4 45.20 45.60 56
67.3 45.00 56.30 67
62 19.00 45.00 39
38 37.00 82.00 74

Here are the requirements:

Standard Sales Amounts:

Jan - 23.0
Feb - 33.1
Mar - 21.0
Apr - 23.5
May - 54.0
Jun - 34.3
Jul - 35.0
Aug - 45.0
Sept - 56.3
Oct - 45.6
Nov 34.0
Dec - 55.0

sales.dat
23 33.5 21 23 25 56 54 43 34.2 35.4 34 69.5
24 35.2 24 26 43 56.7 54 32 43 34 34 57.9
24 42 43 35 52 56 67 54 56 45.3 32 32
20 32 45 72 45.4 63.2 45 56 52 65 53 65
34 35 37.5 32 23 45 31 43 52 43 76 65
35 56 63.4 45.2 45.6 56 67.3 45 56.3 67 78 76
34.2 45 62 19 45 39 38 37 82 74 45 58.4

This is my code, anything helps! Thanks again:

#include <stdio.h>

#include <stdlib.h>

#include <fstream>

#include <iomanip>

#include <iostream>

using namespace std;

void readInformation(double currentprice[], string stocksymbol[], int numbershare[], doubl\
e \
buyingprice[], int & n)

{

ifstream in;

string fname;

cout << "Enter the file name: ";

cin >> fname;

in.open(fname.c_str());

if(in.fail())

{

cout << "Incorrect file" << endl;
exit(1);

}

for(n=0; ; n++)

{

in >> stocksymbol[n];

if(in.eof()) break;

in >> currentprice[n] >> buyingprice[n] >> numbershare[n];

}

in.close();

}
int main()

{

string stocksymbol[100];
double buyingprice[100];

double currentprice[100];

int n;

int numbershare[100];

readInformation(currentprice, stocksymbol, numbershare, buyingprice, n);

cout << "Store Statistics:" <<endl;

cout << "Dept \tAverage \tAbove \tBelow \tPerformance"

<<endl;

cout << "-----------------------------------------------------------" <<endl;

cout << fixed << setprecision(2);

for(int i=0; i<n; i++)

{

if(numbershare[i]<100 && currentprice[i] <= buyingprice[i])

{

cout << left << setw(16) << stocksymbol[i];

cout << left << setw(16) << currentprice[i];

cout << left << setw(21) << buyingprice[i];

cout << left << setw(16) << numbershare[i] << endl;

}

}

return 0;

}
Last edited on
Reformatting the code so that it is readable and fixing readInformation() gives:

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
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

const size_t maxread {100};

void readInformation(double currentprice[], string stocksymbol[], int numbershare[], double buyingprice[], int& n)
{
	string fname;

	cout << "Enter the file name: ";
	cin >> fname;

	ifstream in(fname);
	if (!in) {
		cout << "Incorrect file\n";
		exit(1);
	}

	for (n = 0; (n < maxread) && (in >> stocksymbol[n] >> currentprice[n] >> buyingprice[n] >> numbershare[n]); ++n);
}

int main()
{
	string stocksymbol[maxread] {};
	double buyingprice[maxread] {};
	double currentprice[maxread] {};
	int numbershare[maxread] {};
	int n {};

	readInformation(currentprice, stocksymbol, numbershare, buyingprice, n);

	cout << "Store Statistics:\n";
	cout << "Dept \tAverage \tAbove \tBelow \tPerformance\n";
	cout << "---------------------------------------------------\n";
	cout << fixed << setprecision(2);

	for (int i = 0; i < n; ++i)
		if (numbershare[i] < 100 && currentprice[i] <= buyingprice[i]) {
			cout << left << setw(24) << stocksymbol[i];
			cout << left << setw(8) << currentprice[i];
			cout << left << setw(8) << buyingprice[i];
			cout << left << numbershare[i] << '\n';
		}
}


With the provided data, the output is:


Store Statistics:
Dept    Average         Above   Below   Performance
---------------------------------------------------
.5                      24.00   35.20   24
26                      43.00   56.70   54
57.9                    24.00   42.00   43
35                      52.00   56.00   67
32                      20.00   32.00   45
72                      45.40   63.20   45
56                      52.00   65.00   53
65                      34.00   35.00   37
31                      43.00   52.00   43
63.4                    45.20   45.60   56
67.3                    45.00   56.30   67
62                      19.00   45.00   39
38                      37.00   82.00   74

Thanks but is there any advice or additional help with assisting me in making it look like this:

My code is above if you need to refer back to it. Thanks again!

Store Statistics
Dept Average Above Below Performance
---------------------------------------------------------------------------------------------
1 37.6 7 5 unsatisfied
2 38.6 8 4 satisfied
3 44.9 7 5 unsatisfied
4 51.1 8 4 satisfied
5 43.0 7 5 unsatisfied
6 57.6 11 1 satisfied
7 48.2 9 3 satisfied
if you have the data you need for the output, you need to write a function to generate the output the way you want it, or rewrite the one that currently generates the output.
satisfied and unsatisfied are booleans. its probably something like if (something > minrequred) satisfied = true else satisfied = false. then a string array: const string satisfaction[]{"unsatisfied","satisfied"};
and a cout statement: cout << things... << satisfaction[satisfied] << endl;
//uglier, but concise, you can put the boolean expression in the [] instead of having a variable for it
that is satisfaction[x > y] will do the job
the rest of it is just numbers that you already have. A classic for loop with an i++ type counter lets you print the line numbers, just watch the off by 1 (if you start i at 0 to do a container's first location, cout << i+1 instead of cout << i ).
you can use '\t' to put tabs between columns to make it line up.
Can you do it with those hints?
Last edited on
First, work out how to produce the required output from the given data manually using pen/paper. Once you know this, then specify the steps needed to do this. This is the algorithm design. Then this can be made into a program design and then coded from this. Coding is the last thing you do when developing a program!

If you describe the algorithm/design then we'll be able to advise re the coding.
Thank you guys! I have an idea on what to do, thanks for the hints. I really appreciate it.
Topic archived. No new replies allowed.