Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't this code output to a file?
    primarykey
    data
    text
    <p>I've debugged my program and the arrays seem to be allocated well. However for some strange and stupid reason, the code doesn't output the arrays into the file.</p> <p>Please help me spot my bug or such!</p> <pre><code>#include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; #include &lt;fstream&gt; using namespace std; void sRecSort(string *n, int *s, string *e, int len){ for (int i = 0; i &lt; len; i++){ for (int j = i + 1; j &lt; len; j++){ if (s[j] &lt; s[i]){ swap(s[i],s[j]); swap(e[i],e[j]); swap(n[i],n[j]); } } } } void printLowestRecord(char inFileName[]){ string tempSubString = " "; string names[12] = {" "}; int grades[12] = {0}; string emails[12] = {""}; int firstSpace = -1; int secondSpace = -1; ifstream inputMe(inFileName); while (!inputMe.eof()){ for (int i = 0; i &lt; 12; i++){ getline(inputMe, tempSubString); for (int w = 0; w &lt; strlen(tempSubString.c_str()); w++){ if (tempSubString[w] != ' '){ continue; } else{ if (firstSpace == -1){ firstSpace = w; } else if (firstSpace != -1 &amp;&amp; secondSpace == -1){ secondSpace = w; names[i] = tempSubString.substr(0, firstSpace); grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str()); emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1)); break; } } } firstSpace = -1; secondSpace = -1; } } sRecSort(names, grades, emails, 12); inputMe.close(); } void sortFileRecords(char inFileName[], char outFileName[]){ ifstream inputFile(inFileName); ofstream outputFile(outFileName); string tempSubString = " "; string names[12] = {" "}; int grades[12] = {0}; string emails[12] = {" "}; int firstSpace = -1; int secondSpace = -1; while (!inputFile.eof()){ for (int i = 0; i &lt; 12; i++){ getline(inputFile, tempSubString); for (int w = 0; w &lt; strlen(tempSubString.c_str()); w++){ if (tempSubString[w] != ' '){ continue; } else{ if (firstSpace == -1){ firstSpace = w; } else if (firstSpace != -1 &amp;&amp; secondSpace == -1){ secondSpace = w; names[i] = tempSubString.substr(0, firstSpace); grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str()); emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1)); break; } } } firstSpace = -1; secondSpace = -1; } } sRecSort(names, grades, emails, 12); for (int q = 0; q &lt; 12; q++){ outputFile &lt;&lt; names[q] &lt;&lt; " " &lt;&lt; grades[q] &lt;&lt; " " &lt;&lt; emails[q] &lt;&lt; endl; } inputFile.close(); outputFile.close(); } int main (int argc, char * const argv[]) { printLowestRecord("gradebook.txt"); sortFileRecords("gradebook.txt", "sortedGradebook.txt"); return 0; } </code></pre> <p>Here's my data: <br>Sean 80 sean@csi.edu <br>James 100 james@yahoo.com <br>Issac 99 issac@mail.csi.edu <br>Thomas 88 tom@cix.csi.edu <br>Alice 78 alice@myclass.com <br>Jone 75 jone@hotmail.com <br>Zach 89 zach@yahoo.com <br>Mark 86 mark@gmail.com <br>Nick 79 nick@bmail.com <br>Amy 95 amy@hotmail.com <br>Claire 89 claire@yahoo.com <br>Eve 97 eve@nytimes.com</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload