我需要帮助评分测试C ++项目 [英] I need help with grading tests C++ project

查看:56
本文介绍了我需要帮助评分测试C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我的代码有问题我需要



从一个文件中读取,该文件从文本文件中获得4个测试分数,然后对分数进行评分



以下是说明:

Okay I have an issue with my code I need

to read from a file that gets 4 test scores from a text file and then grade the score

Here is the instructions:

Each line represents the four tests taken by students in a class during the semester.  You need to calculate the average for each student in the class by reading in the file.  Display the average for each student, a space, and with their letter grade.





文件内容低于





The file content is below

44 55 77 88
79 88 100 99
77 99 98 99
100 88 89 100
55 56 40 77
100 100 99 95
88 84 87 88
96 97 99 100
30 44 77 55
79 77 88 0
54 52 60 77
88 77 88 77
44 77 10 95





输出应如下图所示

[ ^ ]



请有人帮助我我有3次尝试



什么我试过了:





the output should be like this in the image below
[^]

Please somebody help me I have 3 tries left

What I have tried:

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


double calculateAvg(int studentNum)
{
fstream infile("grades.txt",ios::in);
if(!infile){cout<<"file could not be found!";exit(1);}

double sum = 0; //sum of all test scores
double testScore; //individual student test score
int i = 0;

 while( i < 4 ) {
      infile >> testScore;
 sum = sum + testScore;
      i++;
   }

 infile.close();

return sum/4.0;
 

}

void printGrade(double average)
{
if (average >= 90)
cout << "A" << endl;
 else if (average >= 80)
cout << "B" << endl;
 else if (average >= 70)
cout << "C" << endl;
else if (average >= 60)
cout << "D" << endl;
else
cout << "F" << endl;
}

int main()
{
const int NUM_OF_STUDENTS = 0;
double averages[NUM_OF_STUDENTS];
//calculates and stores 10 students averages
int i = 0;

while(i<NUM_OF_STUDENTS) {
    averages[i] = calculateAvg(i);
      i++;
       
   }
  cout<<(averages[i])<<" ";
printGrade(averages[i]);
}

推荐答案

const int NUM_OF_STUDENTS = 0;
double averages[NUM_OF_STUDENTS];



您认为将分配给多少元素你的平均值数组?


Quote:

好吧我的代码有问题



是的,哪个问题?



你不知道是什么问题在你的代码中,使用调试器,它将逐步显示你的代码正在做什么。



有一个工具可以让你看到你的代码是什么正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Yes, which issue ?

You have no idea of what is the problem in your code, use the debugger, it will show you what your code is doing, step by step.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我需要帮助评分测试C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆