How do I access and read individual lines from files in C++?
<p>I'm working on a final project for my C++ class.  It is a multiple choice quiz in which the correct answers need to be checked by comparing the user's answer with the corresponding answer that is in an <code>answer.txt</code> that contains all 20 answers.  Also the student's name and score needs to be in another textfile that will allow students to look at it to view their scores.  The program, <code>answer.txt</code> and <code>scores.txt</code>  PLUS two functions and whatever else you can use from what you have learned this class period need to be used.</p>

<p>I'm having trouble reading individual answers from the file <code>answer.txt</code>.  I am trying to read one answer at a time and compare it to the answer the user inputted all within a function called <code>check_answer</code>.  I am using integers instead of characters to allow me to easily check for upper and lowercase (by adding 32).  The program compiles, but the only problem is that after I answer the first question, all of the other questions pop up with <code>"You answered correctly!"</code> and the program is finished running.</p>

<p>I am using DevC++.  Below is the code for my program and the contents of the txt file <code>answers.txt</code>.    </p>

<pre><code>#include&lt;iostream.h&gt;
#include&lt;fstream.h&gt;
#include&lt;string.h&gt;

int total_correct = 0;
ifstream infile;

void name_prompt();
void check_answer();
void scores();

int main()
{
      name_prompt();

      cout &lt;&lt; "\nGREAT WHITE SHARK TRIVIA\n";
      cout &lt;&lt; "Enter the letter corresponding to the correct answer choice, then press enter.\n";

      cout &lt;&lt; "\nQuestion 1\n" &lt;&lt; "What is the Great White Shark's scientific name?\n";
      cout &lt;&lt; "A. Hemipristis elongatus" &lt;&lt; endl;    // Snaggletooth shark
      cout &lt;&lt; "B. Odontaspis ferox" &lt;&lt; endl;         // Smalltooth sand tiger
      cout &lt;&lt; "C. Carcharodon carcharias" &lt;&lt; endl;   // Great White Shark
      cout &lt;&lt; "D. Oxynotus caribbaeus" &lt;&lt; endl;      // Caribbean roughshark
      check_answer();

      cout &lt;&lt; "\nQuestion 2\n" &lt;&lt; "Carcharodon is a mix of two Greek words that translate in English to:\n";
      cout &lt;&lt; "A. Ragged tooth" &lt;&lt; endl;    
      cout &lt;&lt; "B. Swift killer" &lt;&lt; endl;        
      cout &lt;&lt; "C. White fish" &lt;&lt; endl; 
      cout &lt;&lt; "D. Sea warrior" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 3\n" &lt;&lt; "How big do Great White Sharks grow?\n";
      cout &lt;&lt; "A. Approximately 10 feet long" &lt;&lt; endl;    
      cout &lt;&lt; "B. Approximately 17 feet long" &lt;&lt; endl;        
      cout &lt;&lt; "C. Approximately 21 feet long" &lt;&lt; endl; 
      cout &lt;&lt; "D. Approximately 30 feet long" &lt;&lt; endl;  
      check_answer();

      cout &lt;&lt; "\nQuestion 4\n" &lt;&lt; "What is the name of the organ on the Great White Shark that allows it to\nsense electromagnetic fields radiating from its prey?\n";
      cout &lt;&lt; "A. Duodenom antennae" &lt;&lt; endl;    
      cout &lt;&lt; "B. Ampullae of Lorenzini" &lt;&lt; endl;        
      cout &lt;&lt; "C. Spiracle gland" &lt;&lt; endl; 
      cout &lt;&lt; "D. Dermal denticles" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 5\n" &lt;&lt; "How long do Great White Shark mothers stay with their young?\n";
      cout &lt;&lt; "A. No time whatsoever" &lt;&lt; endl;    
      cout &lt;&lt; "B. 3 months" &lt;&lt; endl;        
      cout &lt;&lt; "C. 6 months" &lt;&lt; endl; 
      cout &lt;&lt; "D. 1 year" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 6\n" &lt;&lt; "How old are female Great Whites when they usually bear their first young?\n";
      cout &lt;&lt; "A. 2-4 years" &lt;&lt; endl;    
      cout &lt;&lt; "B. 6-8 years" &lt;&lt; endl;        
      cout &lt;&lt; "C. 8-10 years" &lt;&lt; endl; 
      cout &lt;&lt; "D. 12-14 years" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 7\n" &lt;&lt; "What is the favorite prey of an adult Great White Shark?\n";
      cout &lt;&lt; "A. Crustaceans" &lt;&lt; endl;    
      cout &lt;&lt; "B. Fish" &lt;&lt; endl;        
      cout &lt;&lt; "C. Plankton" &lt;&lt; endl; 
      cout &lt;&lt; "D. Sea mammals" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 8\n" &lt;&lt; "What is the tactic used by Great Whites to attack prey on the surface\nof the water?\n";
      cout &lt;&lt; "A. Attack from the side" &lt;&lt; endl;    
      cout &lt;&lt; "B. Attack from below" &lt;&lt; endl;        
      cout &lt;&lt; "C. Jump out of the water and land on the desired target" &lt;&lt; endl; 
      cout &lt;&lt; "D. Wait until the surface prey dives and then attack" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 9\n" &lt;&lt; "After spotting surface prey from below, Great Whites swim upward and launch\nthemselves out of the water grabbing their prey. This is called: \n";
      cout &lt;&lt; "A. Skipping" &lt;&lt; endl;    
      cout &lt;&lt; "B. Jumping" &lt;&lt; endl;        
      cout &lt;&lt; "C. Breaching" &lt;&lt; endl; 
      cout &lt;&lt; "D. Charging" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 10\n" &lt;&lt; "How do Great White Sharks consume their food?\n";
      cout &lt;&lt; "A. Rip off chunks of meat and swallow them whole" &lt;&lt; endl;    
      cout &lt;&lt; "B. Chew thoroughly and swallow" &lt;&lt; endl;        
      cout &lt;&lt; "C. Hold in mouth, let saliva break it down, and then swallow" &lt;&lt; endl; 
      cout &lt;&lt; "D. Suck out bodily fluids and leave carcass" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 11\n" &lt;&lt; "Of the following, which is the best location to view Great White\nbreaching behavior?\n";
      cout &lt;&lt; "A. Myrtle Beach, South Carolina" &lt;&lt; endl;    
      cout &lt;&lt; "B. Cape Cod, Massachusetts" &lt;&lt; endl;        
      cout &lt;&lt; "C. Oban, Scotland" &lt;&lt; endl; 
      cout &lt;&lt; "D. False Bay, South Africa" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 12\n" &lt;&lt; "On average, how many non-fatal Great White Shark attacks occur each year?\n";
      cout &lt;&lt; "A. 2-3" &lt;&lt; endl;    
      cout &lt;&lt; "B. 3-4" &lt;&lt; endl;        
      cout &lt;&lt; "C. 8-10" &lt;&lt; endl; 
      cout &lt;&lt; "D. 15-20" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 13\n" &lt;&lt; "What is one of the main predators juvenille Great Whites have to watch out for?\n";
      cout &lt;&lt; "A. Humboldt Squid" &lt;&lt; endl;    
      cout &lt;&lt; "B. Various Whale Species" &lt;&lt; endl;        
      cout &lt;&lt; "C. Lion's Mane Jellyfish" &lt;&lt; endl; 
      cout &lt;&lt; "D. Other Great White Sharks" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 14\n" &lt;&lt; "After eating a marine mammal (such as a seal or sea lion), a Great White\nShark can go how long without another large meal?\n";
      cout &lt;&lt; "A. 3-4 days" &lt;&lt; endl;    
      cout &lt;&lt; "B. 1 week" &lt;&lt; endl;        
      cout &lt;&lt; "C. 2 weeks" &lt;&lt; endl; 
      cout &lt;&lt; "D. 1-2 months" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 15\n" &lt;&lt; "How fast can adult Great White Sharks swim?\n";
      cout &lt;&lt; "A. Up to 64 mph" &lt;&lt; endl;    
      cout &lt;&lt; "B. Up to 43 mph" &lt;&lt; endl;        
      cout &lt;&lt; "C. Up to 20 mph" &lt;&lt; endl; 
      cout &lt;&lt; "D. Up to 14 mph" &lt;&lt; endl; 
      check_answer();

      cout &lt;&lt; "\nQuestion 16\n" &lt;&lt; "The teeth of a Great White Shark can measure up to?\n";
      cout &lt;&lt; "A. 0.5 in" &lt;&lt; endl;    
      cout &lt;&lt; "B. 1.0 in" &lt;&lt; endl;        
      cout &lt;&lt; "C. 2 in" &lt;&lt; endl; 
      cout &lt;&lt; "D. 2.5 in" &lt;&lt; endl;         
      check_answer();

      cout &lt;&lt; "\nQuestion 17\n" &lt;&lt; "Great White Sharks have which of the following?\n";
      cout &lt;&lt; "A. Acute hearing" &lt;&lt; endl;    
      cout &lt;&lt; "B. Good eyesight" &lt;&lt; endl;        
      cout &lt;&lt; "C. Keen sense of smell" &lt;&lt; endl; 
      cout &lt;&lt; "D. All of the above" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 18\n" &lt;&lt; "In addition to its normal function, a Great White Shark's liver helps it to:\n";
      cout &lt;&lt; "A. Float" &lt;&lt; endl;    
      cout &lt;&lt; "B. Swim aerodynamically" &lt;&lt; endl;        
      cout &lt;&lt; "C. Sense prey" &lt;&lt; endl; 
      cout &lt;&lt; "D. All of the above" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 19\n" &lt;&lt; "Great White Sharks can detect tiny amounts of blood up to how far away?\n";
      cout &lt;&lt; "A. 500 feet" &lt;&lt; endl;    
      cout &lt;&lt; "B. 1 mile" &lt;&lt; endl;        
      cout &lt;&lt; "C. 3 miles" &lt;&lt; endl; 
      cout &lt;&lt; "D. 5 miles" &lt;&lt; endl;     
      check_answer();

      cout &lt;&lt; "\nQuestion 20\n" &lt;&lt; "Great White Shark's mouths contain up to how many teeth?\n";
      cout &lt;&lt; "A. 50" &lt;&lt; endl;    
      cout &lt;&lt; "B. 100" &lt;&lt; endl;        
      cout &lt;&lt; "C. 200" &lt;&lt; endl; 
      cout &lt;&lt; "D. 300" &lt;&lt; endl;     
      check_answer();

      scores();

      system("pause");
      return 0;
}

void name_prompt()
{
     char user_name[30];
     ofstream outfile;

     cout &lt;&lt; "Please enter your name: ";
     cin.get(user_name, 30);

     outfile.open("SCORES.TXT", ios::out);

     if(!outfile)
      {
                 cout &lt;&lt; "Error opening file.\n";
      }

      outfile &lt;&lt; user_name &lt;&lt; endl;
      outfile.close();
}

**void check_answer()
{
      ifstream infile;
      infile.open("ANSWERS.TXT", ios::in);

      if(!infile)
      {
                 cout &lt;&lt; "Error opening file.\n";
      }

     int x, answer;

      cin &gt;&gt; x;

      infile &gt;&gt; answer;

      if(x == answer || x == (answer + 32))
      {
           cout &lt;&lt; "You answered correctly!\n" &lt;&lt; endl;
           total_correct++;
      } 
      else
      {
          cout &lt;&lt; "You answered incorrectly.\n" &lt;&lt; endl;
      }
      infile.close();
}**

void scores()
{
     ofstream outfile2;         
     int score;

     score = (total_correct * 5);

     outfile2.open("SCORES.TXT", ios::app);

     if(!outfile2)
      {
                 cout &lt;&lt; "Error opening file.\n";
      }

      outfile2 &lt;&lt; "You answered " &lt;&lt; total_correct &lt;&lt; " out of 20 questions correctly." &lt;&lt; endl;
      outfile2 &lt;&lt; "You earned a score of " &lt;&lt; score &lt;&lt; "%.\n";
      outfile2.close();

      cout &lt;&lt; "Please see SCORES.TXT if you wish to view your results." &lt;&lt; endl;
}
</code></pre>

<p>contents of "answers.txt":</p>

<pre><code>C
A
C
B
A
D
D
B
C
A
D
A
D
D
B
D
D
A
C
D
</code></pre>

<p>Help would be very much appreciated. Thank you so much for taking the time to look at this!</p>
