Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're doing some dodgey things. As mentioned already, don't test <code>eof</code>. You actually need to test whether your input was successful and/or whether the stream is good. You can have a catch-all when you're reading streams:</p> <pre><code>while( infile ) { //... } </code></pre> <p>I mentioned also that you need to check that the value was read. It could be that an integer cannot be read because the input stream does not contain something that looks like an integer, or perhaps some other error occurred. Since you're only reading one integer at a time, you can test that operation by itself. If the stream was bad to begin with (<em>eg</em> the file could not be opened) it will fail straight away:</p> <pre><code>while( infile &gt;&gt; input[x] ) { //... } </code></pre> <p>I wouldn't generally use the above approach if reading more than one value each time, because the loop condition gets cluttered. Here's an alternative:</p> <pre><code>while( infile ) { if( !(infile &gt;&gt; input[x]) ) { cout &lt;&lt; "Failed to read value " &lt;&lt; x+1 &lt;&lt; endl; break; } //... } </code></pre> <p>Let's roll with that, because it's more useful when you wanna find out what's going on. The next thing is that you have a fixed-size array with 100 values. If you read too many values, you are going to overflow. So you need to make sure this won't happen:</p> <pre><code>while( infile &amp;&amp; x &lt; 100 ) { if( !(infile &gt;&gt; input[x]) ) { cout &lt;&lt; "Failed to read value " &lt;&lt; x+1 &lt;&lt; endl; break; } cout &lt;&lt; input[x] &lt;&lt; endl; if( input[x] &lt; 50 ) { fail++; } else { pass++; } x++; } </code></pre> <p>Now, it seems to me that you're not even using the array, so why keep it at all? You could just read into a temporary value:</p> <pre><code> int grade; if( !(infile &gt;&gt; grade) ) break; // ... </code></pre> <p>But if you wanted to keep the array and didn't know how many grades you might end up reading, use a <code>vector&lt;int&gt;</code> instead of an <code>int</code> array. I'll show this with the simpler loop style:</p> <pre><code>std::vector&lt;int&gt; grades; int grade; while( infile &gt;&gt; grade ) { grades.push_back(grade); //... } </code></pre> <p>Notice how I've used more descriptive variable names. Something like <code>input</code> doesn't tell you much about what it contains. That is the kind of variable name you'd get from an inexperienced programmer whose YouTube videos you should immediately boycott.</p> <p>The last thing I'll suggest is using <code>ifstream</code> instead of <code>fstream</code>, because by default you don't need to open the file in read/write mode. You could supply flags to <code>fstream::open</code>, but why do that when you can just construct <code>ifstream</code> like this:</p> <pre><code>ifstream infile("exam.txt"); </code></pre> <p>You also don't need to explicitly close it, because that will happen when <code>infile</code>'s destructor is called.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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