- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的 C++ 类(class)做期末项目。这是一个多项选择测验,需要通过将用户的答案与包含所有 20 个答案的 answer.txt
中的相应答案进行比较来检查正确答案。此外,学生的姓名和分数需要放在另一个文本文件中,以便学生查看它以查看他们的分数。该程序 answer.txt
和 scores.txt
加上两个函数,以及您在本节课期间学到的任何可以使用的东西。
我无法从文件 answer.txt
中读取个别答案。我尝试一次读取一个答案,并将其与用户在名为 check_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;
}
“answers.txt”的内容:
C
A
C
B
A
D
D
B
C
A
D
A
D
D
B
D
D
A
C
D
非常感谢您的帮助。非常感谢您抽出宝贵时间查看此内容!
最佳答案
我认为问题在于您正试图将一个字符读入一个整数:
int x, answer;
cin >> x;
基本上 cin >> x
表示“从标准输入读取一个 整数 到 x
”。问题是流中没有整数,只有一个字符。因此读取操作失败并且该字符留在流中。下次您尝试阅读答案时,您最终会一遍又一遍地阅读同一个字符。
首先你应该重新考虑你选择使用整数来阅读答案。你真的需要一个整数来达到你想要达到的目的吗?您也可以对 char
使用算术运算...
一般性建议:始终将数据与代码分开...在这种情况下,您应该将问题放在单独的文件中。这将使您的代码更具可读性和更易于管理。
关于c++ - 如何访问和读取 C++ 文件中的各行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10669035/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!