reading from file!

closed account (ETAkoG1T)
I am making a program that reads from a file and should find the five consecutive numbers with the biggest product. I thought I this would work but it doesn't, it doesn't generate but it just waits for input. Please don't ask what the problem is because the problem is that the program I thought should work doesn't! Thanks for the time:

Code:
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 "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <fstream>
using namespace std;
long product(char ar[]);

int main()
{
	long biggest = 0;
	ifstream in;
	char ar[5];
	int a = 0;
	int o = 0;
	in.open("digits.txt");
	for(int i = 0; !(in.eof());  i++  )
	{
		for(;o < 5; o++)
		in.get(ar[o]);

		if(biggest < product(ar))
			biggest = product(ar);

		in.get(ar[a]);
		if(ar[a] == '\n')
			in.get(ar[a]);
		a++;
		if(a == 5)
			a = 0;
	}
	cout << "The largest product is: " << biggest;
	cin.get();
	cin.get();
	return 0;
}

long product(char ar[])
{
	return ar[0] * ar[1] * ar[2] * ar[3] * ar[4];
}
Last edited on
closed account (ETAkoG1T)
This is what the file contains:
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450


Its not homework its a project euler problem.
closed account (ETAkoG1T)
I fixed the program but it generates wrong answer
++[variable] for performance gain.
1
2
for(;o < 5; o++)
     in.get(ar[o]);


By the way, you never reset o back to 0.
So I'm going to assume that your code just keeps comparing the first 5 digits.
closed account (ETAkoG1T)
No it is meant to not reset back to 0, its only suppose to do that once, then it will only change one value every time from then on! Study the code a bit more and you will understand but the answer I get is still wrong.
char ar[5];
ar has only 5 slots... I don't understand.. if o keeps increasing.. then where is the data going?
closed account (ETAkoG1T)
This is the first line the program is suppose to check:
73167176531330624919225119674426574742355349194934
This is how the array checks the numbers
7 * 3 * 1 * 6 * 7
[0] [1] [2] [3] [4]
then it changes the first in the array:
3 * 1 * 6 * 7 * 1
[1] [2] [3] [4] [0]
Notice how 0 now is the new number
1 * 6 * 7 * 1 * 7
[2] [3] [4] [0] [1]
Do you notice how I use the array? I am not very good with c++ but I thought this was a cool way.
6 * 7 * 1 * 7 * 6
[3] [4] [0] [1] [2]

every time the array changes the program evaluates the product and compares it to the biggest so far.

Last edited on
Oh nevermind. Sorry. I wasn't giving it all my attention.
Not sure what the problem is, though.
closed account (ETAkoG1T)
I solved the problem, the problem was that because char values isn't the same as the integer values the calculations was with the ascii codes making the answers really big numbers. I made a convertor though so now it works :)
Well gratz. i'll have to take a look later
Topic archived. No new replies allowed.