C++ (C+;+;)在将数组内容写入文件时遇到问题

C++ (C+;+;)在将数组内容写入文件时遇到问题,c++,arrays,ofstream,C++,Arrays,Ofstream,学校作业。基本上必须为驾驶考试创建一个测试密钥,然后验证学生是否通过了考试。正确答案将存储在一个数组中,学生答案和全名将存储在用户输入的另一个数组中,并写入文本文件 有一段时间我认为我所做的是正确的,直到我去运行程序,注意到无论数组的长度有多长,它都会写一个奇怪的i字符,上面带有重音。它还将数组的正确答案写入我不想执行的文件。我所需要写的文件是:(全名,学生答案(20),全名,学生答案(20),等等) 文件的第一个字母也因某种原因被截断。(例如,“John Doe”变为“ohn Doe”)此错误

学校作业。基本上必须为驾驶考试创建一个测试密钥,然后验证学生是否通过了考试。正确答案将存储在一个数组中,学生答案和全名将存储在用户输入的另一个数组中,并写入文本文件

有一段时间我认为我所做的是正确的,直到我去运行程序,注意到无论数组的长度有多长,它都会写一个奇怪的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);
}
#包括
#包括
#包括
使用名称空间std;
无效正确答案(常量整数大小,字符正确答案[]);
无效提交的答案(常量整数大小,提交的字符数[]);
void Get_Name(字符串和全名);
int main()
{
常数int SIZE=4;
字符[大小];
提交字符的回答者[大小];
字符串文件名;
字符串全名;
char go='Y';
流输出;
正确答案(尺寸、正确答案);

cout写入文件时收到垃圾邮件的原因是,当您对文件(ofstream对象)使用>>运算符时,实际上是在向其传递指针,而它只是不断读取字符,直到找到行尾或空白。例如:

int main()
{
    char submittedAnswers[4] = { 'a', 'b', 'c', 'd' };
    char * memptr = submittedAnswers; // Points to "abcdÌÌÌ̤ònÉÈø+="
    ofstream outputObj ("d:/filetest.txt");

    outputObj << submittedAnswers << endl;
}
那么为什么要执行cin.ignore()?因为您认为必须在阅读一行之前添加cin.ignore(),才能使其工作:

cout << "\nEnter the file name: ";
cin.ignore(); // You've added this because otherwise getline(cin, fileName)
              // ends up empty
getline(cin, fileName);
outputObj.open(fileName);

在循环中。无论何时该行执行它,默认情况下都会跳过空白和新行。当您输入答案时,您会键入一些内容,然后按enter键。当您按enter键时,它会向缓冲区添加一个“\n”。因此,命令行输入会插入到correctAnswers[],但在最后一个cin>>中,在它的末尾有一个“\n”字符,这就是为什么在调用getline()之前必须调用cin.ignore,以跳过缓冲区中的“\n”字符。

写入文件时收到垃圾邮件的原因是,当您对文件(ofstream对象)使用>>运算符时,实际上是在向其传递指针,它只会一直读取字符,直到找到行尾或空白。例如:

int main()
{
    char submittedAnswers[4] = { 'a', 'b', 'c', 'd' };
    char * memptr = submittedAnswers; // Points to "abcdÌÌÌ̤ònÉÈø+="
    ofstream outputObj ("d:/filetest.txt");

    outputObj << submittedAnswers << endl;
}
那么为什么要执行cin.ignore()?因为您认为必须在阅读一行之前添加cin.ignore(),才能使其工作:

cout << "\nEnter the file name: ";
cin.ignore(); // You've added this because otherwise getline(cin, fileName)
              // ends up empty
getline(cin, fileName);
outputObj.open(fileName);

在循环中。无论何时该行执行它,默认情况下都会跳过空白和新行。当您输入答案时,您会键入一些内容,然后按enter键。当您按enter键时,它会向缓冲区添加一个“\n”。因此,命令行输入会插入到correctAnswers[],但在最后一个cin>>中,在它的末尾有一个“\n”字符,这就是为什么在调用getline()之前必须调用cin.ignore,跳过缓冲区中的“\n”字符。

您是否尝试使用调试器单步执行代码?我没有。我会尝试,我真的不知道如何使用调试器,但我想只有一种学习方法。我只是使用编译器错误消息来修复大多数问题。只有当您编写的代码无法执行时才会发出编译器错误转换为可执行文件。并非所有编译的程序都能正常工作,甚至不能以定义的方式工作。你可以使用调试器,逐行逐行地检查它是否正常工作。是的,我知道每个编译的程序并不一定意味着它能正常运行。我只是从来没有使用过调试器逐行地检查。即使是ju现在,当我尝试它时,它似乎没有告诉我任何事情。它打开控制台,运行程序,然后关闭。不过,我将查看如何使用它。感谢您的建议如果您对调试器过敏,您可以始终放置“打印”语句,打印变量名及其内容。这是一种古老的技术,在调试器不可行时使用(如发布模式问题)。您是否尝试过使用调试器单步执行代码?我没有。我会尝试,我真的不知道如何使用调试器,但我想只有一种学习方法。我只是使用编译器错误消息来修复大多数问题。编译器错误仅在您编写的代码无法转换为可执行文件时才会出现。并非所有程序都是如此编译-可以正常工作,甚至可以按定义的方式工作。你可以使用调试器逐行检查它是否正常工作。是的,我知道每一个编译的程序并不一定意味着它运行正常。我只是从来没有使用过调试器逐行检查。即使是在我刚刚尝试它时,似乎也没有告诉我一个错误什么都可以。它打开控制台运行程序,然后关闭。不过我会看看如何使用它。谢谢你的建议如果你对调试器过敏,你可以随时放置“print”语句,打印变量名及其内容。这是一种古老的技术,在调试器不可行时使用(例如发布模式问题)。