学校任务。必须基本上为驾驶员考试创建一个测试钥匙,然后验证学生是否通过了考试。正确的答案将被存储在一个数组中,学生的答案和全名将被存储在用户输入的另一个数组中,并写入一个文本文件。(C++)将数组内容写入文件时出现问题
有一段时间我以为我在做什么是正确的,直到我去运行该程序,并注意到无论数组长度多长时间,它都会写出一个奇怪的I字符,并且上面有一个口音。它还将数组的正确答案写入我不想做的文件中。所有我需要写入的文件是:(全名,学生答案(20),全名,学生答案(20)等)。
该文件的第一个字母也由于某种原因被切断。 (例如“John Doe”变成“ohn Doe”)这个错误的原因是什么?没有cin.ignore()语句我不知道如何获得我需要的全名到文件中。
我已经将数组大小设置为常数,允许我将其更改为4个空格而不是20个,以便进行快速测试。
任何帮助表示赞赏。编程极其新颖。有时享受它有时很难。
我的代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void Correct_Answers(const int SIZE, char correctAnswers[]);
void Submitted_Answers(const int SIZE, char submittedAnswers[]);
void Get_Name(string &fullName);
int main()
{
const int SIZE = 4;
char correctAnswers[SIZE];
char submittedAnswers[SIZE];
string fileName;
string fullName;
char go = 'Y';
ofstream outputObj;
Correct_Answers(SIZE, correctAnswers);
cout << "\nEnter the file name: ";
cin.ignore();
getline(cin, fileName);
outputObj.open(fileName);
while (go == 'Y' || go == 'y')
{
Get_Name(fullName);
outputObj << fullName << endl;
Submitted_Answers(SIZE, submittedAnswers);
outputObj << submittedAnswers << endl;
cout << "\nTo process another user enter Y. To quit enter N: ";
cin >> go;
}
cout << "\n\n\n";
system("Pause");
return(0);
}
void Correct_Answers(const int SIZE, char correctAnswers[])
{
int questionCounter = 0;
cout << "\nEnter the correct answers for the drivers exam.\n";
for (int x = 0; x < SIZE; x++)
{
cout << "\tQuestion #" << ++questionCounter << ": ";
cin >> correctAnswers[x];
while (correctAnswers[x] != 'A' && correctAnswers[x] != 'a' && correctAnswers[x] != 'B' && correctAnswers[x] != 'b' && correctAnswers[x] != 'C' && correctAnswers[x] != 'c' && correctAnswers[x] != 'D' && correctAnswers[x] != 'd')
{
cout << "\tInvalid entry. Re-enter answer: ";
cin >> correctAnswers[x];
}
}
}
void Submitted_Answers(const int SIZE, char submittedAnswers[])
{
int questionCounter = 0;
cout << "\nWelcome to the written portion of the drivers exam!";
cout << "\nDo your best to answer the questions to the best of your knowledge.";
cout << "\n15 out of 20 are needed to pass the exam. Best of luck!\n\n";
for (int x = 0; x < SIZE; x++)
{
cout << "\tQuestion #" << ++questionCounter << ": ";
cin >> submittedAnswers[x];
while (submittedAnswers[x] != 'A' && submittedAnswers[x] != 'a' && submittedAnswers[x] != 'B' && submittedAnswers[x] != 'b' && submittedAnswers[x] != 'C' && submittedAnswers[x] != 'c' && submittedAnswers[x] != 'D' && submittedAnswers[x] != 'd')
{
cout << "\tInvalid. Re-enter answer: ";
cin >> submittedAnswers[x];
}
}
}
void Get_Name(string &fullName)
{
cout << "\nEnter the users name: ";
cin.ignore();
getline(cin, fullName);
}
你是否尝试用调试器逐句通过你的代码? –
我没有。我会尝试,我老实说,只是不知道如何使用调试器,但我想只有一种方法来学习。我只是使用编译器错误消息来解决大部分问题。 –
仅当您编写的代码无法转换为可执行文件时才会发出编译器错误。并非所有编译的程序都能正常工作,甚至可以定义的方式工作。您可以使用调试器逐行检查它是否正常工作。 –