如何访问和读取C++;? 我正在为我的C++类做一个最终的项目。这是一种选择题测验,需要通过将用户的答案与包含所有20个答案的answer.txt中的相应答案进行比较来检查正确答案。此外,学生的姓名和分数需要在另一个文本文件中,以便学生查看该文件以查看他们的分数。需要使用程序,answer.txt和scores.txt加上两个函数以及本节课所学的任何其他功能

如何访问和读取C++;? 我正在为我的C++类做一个最终的项目。这是一种选择题测验,需要通过将用户的答案与包含所有20个答案的answer.txt中的相应答案进行比较来检查正确答案。此外,学生的姓名和分数需要在另一个文本文件中,以便学生查看该文件以查看他们的分数。需要使用程序,answer.txt和scores.txt加上两个函数以及本节课所学的任何其他功能,c++,C++,从文件answer.txt中读取单个答案时遇到问题。我试图一次读取一个答案,并将其与用户在名为check\u answer的函数中输入的答案进行比较。我使用整数而不是字符来轻松检查大小写(通过添加32)。程序可以编译,但唯一的问题是,在我回答第一个问题后,所有其他问题都会弹出“回答正确!”,程序运行完毕 我正在使用DevC++。下面是我的程序代码和txt文件answers.txt的内容 #include<iostream.h> #include<fstream.h> #i

从文件
answer.txt
中读取单个答案时遇到问题。我试图一次读取一个答案,并将其与用户在名为
check\u answer
的函数中输入的答案进行比较。我使用整数而不是字符来轻松检查大小写(通过添加32)。程序可以编译,但唯一的问题是,在我回答第一个问题后,所有其他问题都会弹出
“回答正确!”
,程序运行完毕

我正在使用DevC++。下面是我的程序代码和txt文件
answers.txt
的内容

#include<iostream.h>
#include<fstream.h>
#include<string.h>

int total_correct = 0;
ifstream infile;

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

int main()
{
      name_prompt();

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

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

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

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

      cout << "\nQuestion 4\n" << "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 << "A. Duodenom antennae" << endl;    
      cout << "B. Ampullae of Lorenzini" << endl;        
      cout << "C. Spiracle gland" << endl; 
      cout << "D. Dermal denticles" << endl;     
      check_answer();

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

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

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

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

      cout << "\nQuestion 9\n" << "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 << "A. Skipping" << endl;    
      cout << "B. Jumping" << endl;        
      cout << "C. Breaching" << endl; 
      cout << "D. Charging" << endl;     
      check_answer();

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

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

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

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

      cout << "\nQuestion 14\n" << "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 << "A. 3-4 days" << endl;    
      cout << "B. 1 week" << endl;        
      cout << "C. 2 weeks" << endl; 
      cout << "D. 1-2 months" << endl;     
      check_answer();

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

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

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

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

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

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

      scores();

      system("pause");
      return 0;
}

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

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

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

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

      outfile << user_name << endl;
      outfile.close();
}

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

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

     int x, answer;

      cin >> x;

      infile >> answer;

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

void scores()
{
     ofstream outfile2;         
     int score;

     score = (total_correct * 5);

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

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

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

      cout << "Please see SCORES.TXT if you wish to view your results." << endl;
}

非常感谢您的帮助。非常感谢您抽出时间来看这个

我认为问题在于您试图将字符读入整数:

int x, answer;

cin >> x;
基本上,
cin>>x
表示“将一个整数从标准输入读入
x
”。问题是流中没有整数,只有一个字符。因此,读取操作失败,并且该字符留在流中。下次你试着读答案的时候,你会一次又一次地读同一个字符

首先,您应该重新考虑使用integer读取答案的选择。你真的需要一个整数来达到你想要达到的目的吗?您也可以对
char
使用算术运算


还有一个一般性建议:始终将数据与代码分开。。。在这种情况下,你应该把问题放在一个单独的文件中。这将使您的代码更具可读性,更易于管理

基本编程错误:未检查输入操作的结果。可能的设计错误:您将所有问题硬编码,而不是从数据文件中读取它们。非常感谢您花时间查看我的代码。有没有关于我应该如何修复错误的建议?是的。1) 学习如何编写正确的输入逻辑(数百个相关问题),2)将问题放入数据文件中,首先将数据文件读入程序的容器中,然后从容器中生成问题,并在进行过程中与答案匹配。如果您需要任何特定零件的帮助,请询问特定问题(但请确保首先搜索重复件!)。好的。非常感谢您花时间回复。我为我草率的代码道歉。我正在上计算机科学的入门课。这是我第一次(也是唯一一次)真正的编程经验。我遇到了麻烦,因为我不太懂这门语言,我们只能用课堂上学到的有限的知识来完成这个项目。老实说,我不知道什么是容器。我是否接近于使用我目前所拥有的创建一个功能性程序,或者你会建议采取另一种方法?你可能会让它工作,但我认为这并不是真正学习正确的东西。在编程或一般生活中,有一个普遍的“零-一-多”规则:你要么根本不做某件事,要么做一次或多次。后者涉及迭代所有内容的集合,并对每个元素应用相同的过程,而不管您有两个、三个还是一千个内容。这就是编程的意义:在问题中找到模式并一劳永逸地解决它。非常感谢!这很有帮助!我不认为我需要把这个项目的问题放在一个单独的文件中,但现在你提到了它,我觉得这将是非常有用的学习。您将如何阅读文件中问题的各个部分?是否可以使用任何东西将“$”和“#”等符号放在文件的行开头,并告诉程序从“$”读取到“#”?再次感谢您的帮助!你可以把你的问题和可能的答案放在不同的行中,然后用英语阅读。假设你所有的问题都有4个可能的答案。然后,您可以阅读一行包含问题的内容,然后阅读4行作为可能的答案,并向用户显示所有内容。用户回答后,阅读下一块5行,然后重复,直到到达文件末尾。
int x, answer;

cin >> x;