Skip to content

Commit 03b0012

Browse files
committed
Streamlined the entry function and ...
removed system pause since it's Windows specific.
1 parent de39fe6 commit 03b0012

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

PiCalc.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,37 @@ int main()
3737
}
3838
else
3939
duration = duration_MS.count();
40-
//std::cout << "Computed " << digits <<" (10^"<<log10l(digits) << ") digits (excluding the first digit ('3')) in " << duration << suffix << "." << std::endl << std::endl;
41-
std::cout << "Computed " << digits <<" (10^"<<log10l(digits) << ") digits (excluding the first digit ('3')) in " << execution_time << "seconds" << "." << std::endl << std::endl;
40+
41+
std::cout << "Computed " << digits <<" (10^"<<log10l(digits) << ") digits (excluding the first digit ('3')) in " << execution_time << "seconds" << "." << std::endl << std::endl;
42+
4243

43-
///Running simple tests
44-
std::string correctLastDigits = "61168313937514970581120187751592";
45-
std::string calculatedPiStr = calculatedPi.get_str();
46-
size_t offset = calculatedPiStr.length() - correctLastDigits.length();
47-
bool doesWork = ~(calculatedPiStr.compare(offset, correctLastDigits.length(), correctLastDigits));
44+
///Writing to file:
45+
std::cout << "Writing computed value to output file." << std::endl;
46+
FILE* pOutFile = fopen(OUTPUT_TXT_FILEPATH,"w");
47+
if(pOutFile==NULL)
48+
{
49+
perror("Error opening/creating output file. ");
50+
exit(EXIT_FAILURE);
51+
}
52+
size_t writtenChars = mpz_out_str(pOutFile, 10, calculatedPi.get_mpz_t());
53+
if(fclose(pOutFile)!=0)
54+
{
55+
perror("Error closing output file. ");
56+
exit(EXIT_FAILURE);
57+
}
58+
std::cout << "Wrote computed values to :" << OUTPUT_TXT_FILEPATH << "!" << std::endl << std::endl;
59+
60+
//Running simple tests
61+
bool doesWork = (writtenChars == 1+digits) ;// 1 char takes up 1 byte
4862
std::string testStatus = "FAILED";
4963
if (doesWork)
5064
{
5165
testStatus = "passed (remember, tests are not exhaustive)";
5266
}
5367
std::cout << "Initial tests have " + testStatus + "." << std::endl << std::endl;
54-
55-
56-
///Writing to file:
57-
std::cout << "Writing computed value to output file." << std::endl;
58-
std::ofstream out(OUTPUT_TXT_FILEPATH);
59-
out << calculatedPiStr;
60-
out.close();
61-
std::cout << "Wrote computed values to :" << OUTPUT_TXT_FILEPATH << "!" << std::endl << std::endl;
62-
6368

6469
///Finished.
65-
std::cout << "Program finished. Want to exit?" << std::endl;
66-
system("PAUSE");
70+
std::cout << "Program finished." << std::endl;
71+
6772
return EXIT_SUCCESS;
6873
}

0 commit comments

Comments
 (0)