No Output

Finally got my program to run without errors but it will not print anything.

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int size=10;



void computeTotal(int quantity[], double price[], double total[], int size)  // notice void return type



{
// write code and place here to correctly populate the total array

		for(int i=0; i<size; i++)
			total[i]=quantity[i]*price[i];
return;   // notice no value is returned
}
double maxSales(double total[],int& maxValIndex, int size)  // notice the call by reference parameter
{
// write code and place here to correctly determine the max total value and the index where it occurs


	for(int i=1;i<size;i++)
	{
		if(total[i]>total[maxValIndex])
			maxValIndex=i;
	}
return maxValIndex;
}
int main()
{
string productID[25]; 
int quantity[25], index; 
double price[25],total[25];
ifstream indata; // this is the way we declare input streams
indata.open("sales.txt");
ofstream outdata; // this is the way we declare output streams
outdata.open("report.txt");


int i=0;
indata >> productID[i] >> quantity[i] >> price[i];


while(!indata.eof()) // reading in the data to parallel arrays


{
i++;
indata >> productID[i] >> quantity[i] >> price[i];
}
computeTotal(quantity, price, total, i); // function call
double biggestSale = maxSales(total,index,i);// function call
int maxValIndex= 0;

// place appropriate code here to produce correct output

for(int i=1;i<size;i++)
cout<<"The biggest sales occurs for "<< productID[maxValIndex] <<" for the amount of $ "<<total[maxValIndex]<<endl;


indata.close();
outdata.close();
return 0;
}
You should be verifying the files were opened successfully. What will your program do if the sales.txt file is not where you expect it.

"25" seems like a magic number. Make use of existing container types...std::list may be a good option.

Even if you do with the array approach, why are productID[0], etc always left empty? Why not make use of them?

In the last for loop, you go from 1-10 over arrays that have size '25'. What if you only read in 2 lines from sales.txt? This should not be 1-10, it should be 0 - num elements in totals.
Are you sure it is successfully reading the "sales.txt" file?

Other than that, I found the program slightly confusing. There's a global constant "size". There's also in both computeTotal() and maxSales() a reference to "size". But it's a completely different animal, it's the parameter passed from the calling function. There, the passed value is "i" which is actually the count of records read from the input file.

In fact the only place the global "size" is used is in the for loop at the end of main(). At that point, I rather think the limit of the loop should be the same "i" previously mentioned.

But that doesn't solve the problem.

I'd put in some temporary cout statements to show that the input is being read successfully. If that's ok, then examine the rest of the code.
In addition to the issues already mentioned, there are several logic errors:
1
2
3
4
5
6
    double biggestSale = maxSales(total,index,i);// function call
    int maxValIndex= 0;
    // place appropriate code here to produce correct output
    for (int i=1; i<size; i++)
        cout<<"The biggest sales occurs for "<< productID[maxValIndex] 
          <<" for the amount of $ "<<total[maxValIndex]<<endl;

The least important issue is the needless for loop here, which simply repeats the same identical output message size-1 times. Having removed the "for", we also remove the last reference to that constant.

Next, maxValIndex is set to zero, so that is the index of the row which will be reported.

And the function maxSales is supposed to return a double, but actually returns an int (converted to a double). It also returns the same value via the reference parameter, index. But neither of these returned values are made use of.

Of course, it's possible that two or more different products might result in an identical sales total. Thus, the maximum sales figure might apply to more than one product, and perhaps there could be a reason for a loop, in order to print all products with the maximum total.
Last edited on
I am beyond confused. Below was the initial problem. My professor wrote the code below and we are just supposed to complete the function bodies which I thought would be simple but is apparently not. May be this could help you all better understand what I was attempting to do.


This program reads from a file, sales.txt, containing multiple records of the format:
string int double
where the string is a productID, the int is a quantity, and the double is a price.
It stores this data in three parallel arrays. The program then uses a function, computeTotal, to compute
the total sales for each record. Next, it uses a second function, maxSales, to find the largest total and
the corresponding record index.
Finally, the message "The biggest sale occurs for product ID **** in the amount of $****" is written to a
file, report.txt, where of course the **** are replaced by the correct values.

Obviously, a great deal of the code is already provided for you. Your task is to write code to complete the
function bodies and to produce the correct output to the file.
*/
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void computeTotal(int quantity[], double price[], double total[], int size)  // notice void return type
{
// write code and place here to correctly populate the total array
return;   // notice no value is returned
}
double maxSales(double total[],int& maxValIndex, int size)  // notice the call by reference parameter
{
// write code and place here to correctly determine the max total value and the index where it occurs
return maxVal;
}
int main()
{
string productID[25]; 
int quantity[25], index; 
double price[25],total[25];
ifstream indata; // this is the way we declare input streams
indata.open("sales.txt");
ofstream outdata; // this is the way we declare output streams
outdata.open("report.txt");
int i=0;
indata >> productID[i] >> quantity[i] >> price[i];
while(!indata.eof()) // reading in the data to parallel arrays


{
i++;
indata >> productID[i] >> quantity[i] >> price[i];
}
computeTotal(quantity, price, total, i); // function call
double biggestSale = maxSales(total,index,i); // function call
// place appropriate code here to produce correct output
indata.close();
outdata.close();
return 0;
}



sales.txt

IF47 3 13.95
FF32 1 4.95
GB19 4 12.00
YU82 32 .89
W4R5 6 12.49
SS44 3 129.55
QW41 5 6.75
HG65 4 3.00
K8Y6 23 99.99
WQ22 11 3.43
Well, that makes the problem clearer.
I just tried compiling the supplied initial code.
The first thing I had to do was to define inside the function maxSales(), a variable, double maxVal;
Now it compiles.

The second thing I've done is to make sure that the executable program output from the compiler/linker is in the same path (directory) as the file sales.txt which I've created using the supplied data.

That's what I would regard as a blank canvas or clean slate on which to begin work.
-----------------------------------------

Your code is actually correct in parts, it just needs a little fine-tuning. Function computeTotal I think is ok. Function maxSales is very nearly ok. It just needs to return the appropriate value for maxVal.

The final output - partly right, partly wrong. Again a little fine-tuning required.

I'm trying to give some guidance without actually doing the coding for you (well, I already did my own working version of the code).
I understand completely. I am confused about the maxSales I thought I was returning the right value. Can you explain or show me an example of the correct way please?
Look at the skeleton code above (the code you were given).
1
2
3
4
5
6
7
8
double maxSales(double total[],int& maxValIndex, int size)
{
    // write code and place here to correctly determine
    // the max total value 
    // and the index where it occurs

    return maxVal;
}

Two things to notice here. Look at the comment. It says you are to return two things, a value and the corresponding index.

It does that by using maxVal for the value, and reference parameter maxValIndex for the index.

Now refer to the code posted at the top of this thread.
1
2
3
4
5
6
7
8
9
double maxSales(double total[],int& maxValIndex, int size)  
{
    for(int i=1;i<size;i++)
    {
        if (total[i] > total[maxValIndex])
            maxValIndex=i;
    }
    return maxValIndex;
}

What I see here is the function returns two items, but instead of the required value and its index, actually the index value is returned in both cases.

Also I see that this line: return maxVal; has been replaced by return maxValIndex;.
Look at the return type of the function: double maxSales()
It is supposed to return a type double. (maxValIndex is an integer).

As I suggested earlier, I believe you need to define the variable maxVal (as supplied, your professor's code does not compile successfully).

So, you need something like this:
1
2
3
    double maxVal;
    maxval = /* here add the appropriate code */
    return maxVal;

Actually, the above three lines of code could probably be simplified and done in just one line. But I wrote it that way for the purposes of explanation.

I hope this gets you a step closer to the solution.
Last edited on
It did! I finally got everything straightened out. Thanks!!!
Topic archived. No new replies allowed.