Here is my code for Project Euler #8. It reads the 1000-digit number from a file by reading each character, and then converting that to an integer before storing it into an array. I then intended on using loops to read groupings of 13 digits, 0-12, 1-13, 2-14, etc. And multiplying them. If the result is larger than the previous largest one, it is stored into num2.
#include <iostream>
#include <fstream>
using namespace std;
int num1=1, num2;
int main(){
int digitArray[1000];
char ctemp;
int itemp;
ifstream myfile;
myfile.open("1000 digit number.txt");
if(myfile.is_open()){
for(int i=0; i < 1000; i++){
myfile >> ctemp;
itemp = ctemp - '0';
digitArray[i] = itemp;
}
}
myfile.close();
for(int i = 0; i < 1000; i++){
int j = i;
while(j != (i+13)){
num1 *= digitArray[j];
j++;
if(j == 1000){
break;
}
}
if(num1 > num2){
num2 = num1;
} else {}
}
cout << num2 << endl;
return 0;
}
This code outputs the value 5000940, which is not the correct answer. Help?