I have written code in C++ 11 and check output with Project Euler site, and it is correct. I am not showing output, just to keep it secret, at least from my end.
Please review my C++ 11 code.
#include <fstream>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream file("E:\\names.txt");
if (!file.is_open())
{
cout << "Unable to open file";
return -1;
}
/* Get all file data with ',' delimiter*/
vector<string> words;
string token;
while (getline(file, token, ','))
{
words.push_back(token);
}
//sort all the words
sort(words.begin(), words.end());
int counter = 0;
unsigned int grand_total = 0;
//get total of each word and multiply with it's position
for (auto it = words.begin(); it != words.end(); it++)
{
counter++;
//copy word from token excluding " "
string word((*it).begin() + 1, (*it).end() - 1);
int sub_total = 0;
for (string::iterator it = word.begin(); it != word.end(); it++)
{
//ascii value of A is 65
//A is 1, B is 2, so *it - 64
sub_total += *it - 64;
}
//grand total will be final answer
grand_total += sub_total*counter;
}
return 0;
}