简体   繁体   English

有人可以帮我完成这个计划任务吗? (C++)

[英]Can someone give me a bit of assistance finishing up this program assignment? (C++)

So, the purpose of this program is to input data from a.txt file that has a long list of student IDs and three grades, and output this data into another.txt file, but with the IDs and the averaged grades.因此,该程序的目的是将数据从一个包含一长串学生 ID 和三个成绩的 .txt 文件以及 output 这个数据输入到另一个 .txt 文件中,但带有 ID 和平均成绩。 I am a bit stumped about how to make my program read the next line after it reads one line.我对如何让我的程序在读取一行后读取下一行感到有些困惑。 Right now how the code is it just repeatedly prints the first line.现在代码是如何重复打印第一行的。

int main()

{

int id;
int score1;
int score2;
int score3;
ofstream output;
ifstream input;

int grade = 0.3 * score1 + 0.3 * score2 + 0.4 * score3;

  char letgrade;

if (grade >= 90.0)
char letgrade = 65;
else if (grade >= 80.0)
char letgrade = 66;
else if (grade >= 70.0)
char letgrade = 67;
else if (grade >= 60.0)
char letgrade = 68;
else char letgrade = 69;

output.open("studentGrade.txt");

do 
{

input.open("StudentInfo.txt");

input >> id >> score1 >> score2 >> score3;

output << id << " " << letgrade << endl;

input.close();

}

while (!input.eof());

output.close();

return 0;

 }

From what I understand, the getline function comes in handy here.据我了解,getline function 在这里派上用场。 It's just I need help regarding how to use it.只是我需要有关如何使用它的帮助。

You are repeatedly reopening the file anew in input.open("StudentInfo.txt");您在input.open("StudentInfo.txt");

Your do {} while loop should be something more like the following ( although you need to be careful with while (.stream.eof) {} ):您的do {} while循环应该类似于以下内容( 尽管您需要小心使用while (.stream.eof) {} ):

input.open("StudentInfo.txt");
while (!input.eof()) {
  input >> ...;
  output << ... << endl;
}

The code posted has other issues a teacher would likely characterize as deficiencies: unorganized procedures, bloated predicate ( if expression), failure to adhere to a recognizable variable and coding style - but nothing that a quick review of material presented in lecture or section notes on files, loops and a quick read of a well-regarded C++ style guide won't fix.发布的代码还有其他问题,教师可能会将其描述为缺陷:无组织的程序、臃肿的谓词( if表达式)、未能遵守可识别的变量和编码风格——但没有什么比快速回顾讲座或章节注释中提供的材料更重要文件、循环和快速阅读广受好评的 C++ 样式指南将无法修复。

C++ is a tough language and countless veterans have "blown their feet off" in spite of an excellent command of the language. C++ 是一门难学的语言,尽管对这门语言有出色的掌握,但无数退伍军人“大吃一惊”。 As a novice, you should expect that answers to questions (such as this one) to require a 30-45 minutes of additional research to define terms, place it in a wider context or develop intuition for the machinery.作为新手,您应该期望问题的答案(例如这个问题)需要 30-45 分钟的额外研究来定义术语、将其置于更广泛的上下文中或培养对机器的直觉。

minimal working version, lots of comments.最小的工作版本,很多评论。

#include <fstream>
#include <iostream>

// this here's a function. it's a bit of code that accepts 3 parameters,
// does stuff with them, prints a message, then returns a value.
int computeGradeAverage(int a, int b, int c)
    {
    int avg =  ( a + b + c ) / 3;
    std::cout << "computed average of " << a << ", " << b << ", " << c << ", is " << avg << std::endl;
    return avg;
    }

// the main function. typically programs have many functions, and the C/C++
// languages (and java etc etc) require a single 'main' function to start the program at
// (it can be up top, or at bottom of file, or in the middle; doesn't matter).
int main()
    {

    // a) allocate named variable of given types to hold values.
    int id;
    int score1;
    int score2;
    int score3;
    int gradeaverage;
    std::ofstream output;
    std::ifstream input;

    // b) open input and output files
    input.open("StudentInfo.txt");
    output.open("studentGrade.txt");

    // c) loop over each line of input file
    do
        {
        // c.i) read data (one line) in
        input >> id >> score1 >> score2 >> score3;

        // c.ii) do calculation on input data
        gradeaverage = computeGradeAverage(score1, score2, score3);

        // c.iii) write data out
        output << id << " " << gradeaverage << std::endl;
        }

    // d) stop looping if we have reached the end of the input file (eof = end of file)
    // ... 'input' is the file object; input.eof() is call of one of its member function s
    // ... the 'eof()' function will return true (if at end) or false (if there's more data).
    // ... "while(!thing)" means "while NOT thing" .. so the 'while ( ! input.eof()  )' means:
    //  ..  do { "the things" } while " input file not finished "
    while (!input.eof());
    // There are several ways of doing this, like while(!eof) { .. } and others.

    // e) close the files. while it'll work without doing this, it's a very important
    // habit to put away things after use; deallocate memory, close files, commit transactions,
    // free (as in liberate, not beer) any acquired resources, etc etc.
    input.close();
    output.close();

    // f) return a value to the caller. In this case, the caller is the operating system,
    // and at least with unixy programs, '0' means 'okay', and other numbers would mean
    // some error occurred. For example, the 'g++' compiler returns 1, on any error.
    return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM