Program for finding the largest product of a file

Hello, I'm a C++ student at a community college and I'm completely stumped on this program we're tasked with making where there is a file called numbers.txt with a 1000 numbers inside of it and breaks up the program into segments where the numbers are all multiplied together. The length of the segments is to be determined by the user and program is supposed to find the biggest product out of one of those sets and I believe I know which part is messing up but I don't know how to fix it. It seems like the program is not calculating the product like it's supposed to since it's not wanting to move on to the next group of numbers down the file, for some reason it calculates it correctly for a parameter of 4 per set but not 15 (largest product of 4 per set being 6561 and for 15 being 5399780682240).

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
long result;
long large=0;
long position;
int length;
string variable;
int ray[1000];
cout<<"What certain amount of digits do you want to find the maximum product for?\n";
cin>>length;
ifstream see;
see.open("numbers.txt");
ofstream fout;
fout.open("output.txt");
while(see.eof()==0)
{
for(int i=0;i<1000;i++)
{
see>>variable;
ray[i]=stoi(variable);
}
for(int i=0;i<1000;i++)
{
position=0;
result=1;
//The part that keeps messing up
while(position<length)
{
result*=ray[i];
position++;
}

if(result>large)
{
large=result;
}
}
}

cout<<"The greatest product is "<<large;

}

Here are also the contents of the file numbers.txt:

2 3 5 2 3 4 1 8 9 9 4 5 5 4 4 8 3 3 5 7
5 1 1 3 6 6 8 3 4 3 2 3 2 2 3 5 9 3 3 6
1 8 9 8 3 6 3 7 8 9 1 2 3 5 1 4 8 4 5 9
5 5 3 3 6 3 9 8 4 6 4 8 9 5 2 2 6 3 9 4
9 6 6 6 3 1 3 7 7 8 2 2 5 5 2 7 5 3 5 9
2 4 9 6 1 6 7 5 2 9 3 6 3 8 6 9 9 2 7 4
4 8 5 2 7 2 7 5 8 7 2 6 4 2 9 6 5 3 2 8
1 7 1 1 7 6 3 3 4 3 9 7 7 2 9 2 6 2 5 9
5 8 7 6 4 4 4 1 2 7 5 8 1 4 9 6 5 9 7 5
1 8 2 1 8 5 9 3 1 9 7 7 1 7 1 5 8 5 7 3
6 8 4 8 3 5 4 5 3 2 4 2 9 2 2 1 3 8 3 7
8 1 5 7 6 3 5 8 1 3 3 3 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3 1 3 9 2 2 9 3 1 9 3 7 5 3 5 4
8 1 4 7 4 9 7 9 2 3 2 5 2 5 6 3 4 3 2 1
3 4 8 5 1 2 2 1 5 9 9 6 3 8 2 7 8 4 3 3
1 9 2 3 8 1 3 7 9 3 6 8 4 1 1 5 6 3 4 2
8 1 5 1 8 8 3 3 7 7 1 3 7 4 8 3 7 6 4 7
5 5 2 9 7 6 2 2 3 7 3 5 3 8 8 4 8 9 5 5
8 4 3 5 4 3 8 6 9 9 4 2 3 8 6 6 6 3 1 1
6 8 1 7 7 3 2 3 4 8 8 6 2 9 2 5 8 3 6 6
1 8 5 8 4 1 5 3 1 7 1 4 8 2 4 2 4 2 7 8
4 7 3 7 7 7 6 9 7 1 9 8 5 9 2 8 8 7 8 2
9 9 3 7 9 8 8 4 7 9 1 1 7 9 2 5 2 9 2 3
9 9 4 1 7 4 1 6 1 8 8 2 2 7 9 1 4 5 5 7
2 7 8 2 7 3 9 7 1 7 4 9 7 6 6 1 8 7 9 8


I wish I knew what to do, but I sadly haven't come up with anything to fix it. If someone where to respond, I would like to know not just the fix but how it can improve the program so I can learn from what I was doing wrong.
Last edited on
Your problem statement is not clear.
Yeah sorry I kinda messed it up. There's a file with a 1000 numbers inside of it and we have to a program that will find by a limited number consecutive digits and find the largest product out of them, and the limit that the numbers can keep consecutively multiplying by is determined by the user.

Say I had the line in the file:
1 2 3 4 5 6 7 8 9 10

If the user selected 2 as the limit, then the program should take the numbers out of the file and do something like this:

(1*2)=2, then (3*4)=12, then (5*6)=30, then (7*8)=56, and (9*10)=90
Since 90 is the largest product then this it what it should output.

In a similar example with the same line in the file and instead the user selects 5 as the limit then it would be:

(1*2*3*4*5)=120 and (6*7*8*9*10)=30,240 and the larger product would be outputted.

I can't get it to spit out the right largest product with a limit of 15 consecutive numbers using what I've gathered from the file using "see>>variable" then storing into an array named 'ray' as the result it a much higher number than what it should be. It does it correctly with a limit of 4 but I'm thinking it only works for it since it's multiplying just one of the digits in the array by itself 4 times with that particular limit being 9. It seems like it's not going consecutively like it's supposed to because the part that's getting calculated (result) is being prevented from updating it's position and multiplying consecutively to a certain point is due to it being confined to a while loop. I tried some different configurations for that part and none of them worked right as the largest product ended up being either too big or too small. I'm hoping this explained it a little bit better than the last post.
@babyboi44,

A number of the size that you cite for a product of 15 consecutive numbers will not fit in an int. To hold that you would need something like unsigned long long.

However, multiplying numbers together rapidly produces large products. So, are there limits on the size of numbers in your file (single-digit, for example)? Are there limits on the number of them you must multiply together (15, say)? YOU MUST STATE THESE.

Are there exactly 1000 numbers in your file? If so, why do you need to loop on batches of 1000? Your method wouldn't work if some of the maximal product come from one batch of 1000 and some from the next. The only array worth storing is one of (at least) size 'length'. Unless you want to do something else with the data there is nothing to be gained by having an array of size 1000 and if your file contains more numbers than that (you are not clear about this, but this particular file does indeed have 1000 numbers in it) then your method will fail.

Don't use .eof() to detect end of file - this won't flag anything until you have tried to read from non-existent data, by which time your loop will be committed to going on with whatever was previously inside that variable.

No idea why you read into a string, then convert. Why don't you read straight into an integer?

No idea why you have opened an output file; you aren't using it.

I think you need to state, verbatim, your original assignment, not your paraphrasing of it, which remains unclear, particularly as to constraints.

PLEASE USE CODE TAGS.
Last edited on
Topic archived. No new replies allowed.