将字符逐行分别读入数组,并比较答案 C++

huangapple go评论66阅读模式
英文:

Reading lines of characters separately into arrays and comparing answers C++

问题

I have done a project where I read only 1 line of 20 characters and compare those characters to an answer key and outputs how many are right and how many are wrong based on what they inputted in the file. My code below shows how it looks with just one line of 20 characters in the .dat file. I would like to do the same thing like this, but with 7 lines of 20 characters each. I am not sure what to write to separate the 7 lines and compare them separately to the right answers.

    char letter;
    int index = 0;

    while (inData >> letter) // takes the character from each array from the file
    {
        studentAns[index] = letter;
        index++; // takes the 20 characters from the file
    }

I have tried to add more variables outside, and inside the loop, but the output displays nothing. Such as this.

# Correct | # Incorrect
-----------------------
    0     |    20
-----------------------

You failed!

char letter;
char letter2;
int index = 0;
int index2 = 0;

while (inData >> letter) // takes the character from each array from the file
{
    studentAns[index] = letter;
    index++; // takes the 20 characters from the file
    
    while (inData >> letter2) 
    {
        studentAns2[index2] = letter2;
        index2++; 
    }
}
英文:

I have done a project where I read only 1 line of 20 characters and compare those characters to an answer key and outputs how many are right and how many are wrong based on what they inputted in the file. My code below shows how it looks with just one line of 20 characters in the .dat file. I would like to do the same thing like this, but with 7 lines of 20 characters each. I am not sure what to write to separate the 7 lines and compare them separately to the right answers.

    char letter;
    int index = 0;

    while (inData >> letter) // takes the character from each array from the file
    {
        studentAns[index] = letter;
        index++; // takes the 20 characters from the file
    }

I have tried to add more variables outside, and inside the loop, but the output displays nothing. Such as this.

# Correct | # Incorrect
-----------------------
    0     |    20
-----------------------

You failed!

char letter;
char letter2;
int index = 0;
int index2 = 0;

while (inData >> letter) // takes the character from each array from the file
{
    studentAns[index] = letter;
    index++; // takes the 20 characters from the file
    
    while (inData >> letter2) 
    {
        studentAns2[index2] = letter2;
        index2++; 
    }
}

    

答案1

得分: 1

如果你能确保有7个学生,每人有20个问题,那么你可以使用二维数组char来进行输入:

char student[7][20];

while (int studentIndex = 0; studentIndex < 7; ++studentIndex)
{
    for (int letterIndex = 0; letterIndex < 20; ++letterIndex)
        std::cin >> student[studentIndex][letterIndex];
}

然后,student[0][0]将是第一个学生的第一个问题,student[0][1]将是第一个学生的第二个问题,student[1][3]将是第二个学生的第四个问题,依此类推。

英文:

If you can guarantee 7 students with 20 questions each, then you can use a two-dimensional array of char to perform the input:

char student[7][20];

while (int studentIndex = 0; studentIndex &lt; 7; ++studentIndex)
{
    for ( int letterIndex = 0; letterIndex &lt; 20; ++letterIndex)
        std::cin &gt;&gt; studentAns[studentIndex][letterIndex];
}

Then student[0][0] would be the first question of the first student, student[0][1] would be the second question of the first student, student[1][3] would be the fourth question of the second student, etc.

答案2

得分: 0

我会给你提供翻译好的部分:

我将为您提供对您所提出的问题的更一般性回答。假设您有一个包含“n”行且每行长度可变的文件。
然后,我们将利用向量和getline()函数来获取答案。

#include <iostream>
#include <fstream>
using namespace std;

int number_of_lines = 0;

int main(){
    string line;
    vector<string> str;
    ifstream myfile("example.dat");
    if(myfile.is_open()){
        while(myfile.peek() != EOF){
            getline(myfile,line);
            str.push_back(line);
            number_of_lines++;
        }
        myfile.close();
    }
    for(int i=0; i<number_of_lines; i++)
    {
        cout<<str[i]<<endl;
    }
    return 0;
}

如果您需要进一步的帮助,请随时提出。

英文:

I will give you a more general answer to the question you have asked. Suppose you have a file which contains "n" number of lines and each line is of variable length.
Then we will take the help of vector and getline() function to get the answer.

#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

int number_of_lines = 0;

int main(){
    string line;
    vector&lt;string&gt; str;
    ifstream myfile(&quot;example.dat&quot;);
    if(myfile.is_open()){
        while(myfile.peek() != EOF){
            getline(myfile,line);
            str.push_back(line);
            number_of_lines++;
        }
        myfile.close();
    }
    for(int i=0; i&lt;number_of_lines; i++)
    {
        cout&lt;&lt;str[i]&lt;&lt;endl;
    }
    return 0;
}

huangapple
  • 本文由 发表于 2023年5月10日 11:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定