How do I stop the program from crashing after a call to the 2nd function?
Anonymous in /c/coding_help
746
report
the program crashes after the call to the second function from the first one. why?<br>```cpp<br>#include <iostream><br>#include <cmath><br>#include <algorithm><br>#include <fstream><br><br>using namespace std;<br><br>int main() {<br><br> string str;<br> ifstream file("input.txt");<br> if(!file.is_open()) {<br> cout << "File was not open.";<br> return 1;<br> }<br><br> string word;<br> getline(file, word);<br><br> double k1 = 0, k2 = 0;<br><br> for (int i = 0; i < word.length(); i++) {<br> k1 += word[i];<br> k2 += pow(word[i], 2);<br> }<br><br> double Ck = pow(k1, 2) / word.length();<br><br> k2 -= Ck;<br><br> double sigma = sqrt(k2 / word.length());<br><br> double childSigma = 0, childK1 = 0, childK2 = 0;<br><br> for (int i = 0; i < 1000; i++) {<br> childK1 = 0, childK2 = 0;<br> for (int j = 0; j < 100; j++) {<br> getline(file, word);<br> for (int k = 0; k < word.length(); k++) {<br> childK1 += word[k];<br> childK2 += pow(word[k], 2);<br> }<br> }<br><br> childK1 /= 100; childK2 /= 100;<br><br> childSigma = sqrt((childK2 - pow(childK1, 2)));<br><br> cout << sigma - childSigma << endl;<br> }<br><br> cout << sigma << endl;<br> cin >> str;<br> return 0;<br>}<br>```<br>dear programmers, help me not to crash the program after calling the 2nd function. I'm still in the process of learning to code, any help would be appreciated.
Comments (16) 29420 👁️