首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++结构练习

C++结构练习
EN

Stack Overflow用户
提问于 2020-02-04 20:59:33
回答 2查看 164关注 0票数 1

我试着完成一个结构练习;

  1. a.在本课程中,您将使用以下3种假设的学生信息定义和声明结构化数据。假设实验室成绩为70%,考试成绩为30%。

姓名(字符串):John Alisa Mike姓氏(string):白棕色绿色课程等级(char):0(待计算)“测试分数(int):8890 75 Lab得分(int):70 64 97

b.执行下列任务:

  • Initialize/declare每个学生的结构化数据类型。注意MVS的IntelliSense特性。
  • 用函数调用(即getGrade)计算课程成绩。这可能是一个空函数,通过引用传递或适当的char函数返回课程成绩.输入将是测试分数和实验室评分。百分比(30%和70%)可以定义为全局双常数。使用一步一步的增量方法来开发您的code.
  • Display,将学生信息返回给用户.尝试使用函数调用打印此输出(见第620页)。问我一些关于如何进行的想法。一个示例输出可能是:

约翰·怀特分数是:C级考试成绩是: 88分是:70分是:C级考试成绩是:90分是:64个迈克·格林成绩是:一个考试成绩是:75分是: 97按任意键继续。。。

以下是我到目前为止所做的工作--不确定为什么我没有得到预期的输出。(如有任何帮助,将不胜感激!)

代码语言:javascript
复制
//
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const double testweight = 0.30;
const double labweight = 0.70;
char getGrade(int testScore, int labScore) {
    if ((testweight * testScore) + (labweight * labScore) >= 90)
        return 'A';
    else if ((testweight * testScore) + (labweight * labScore) >= 80)
        return 'B';
    else if ((testweight * testScore) + (labweight * labScore) >= 70)
        return 'C';
    else if ((testweight * testScore) + (labweight * labScore) >= 60)
        return 'D';
    else return 'F';
}


struct studentType
{
    string studentFName;
    string studentLName;
    int testScore;
    int labScore;
    char grade;

};


void printstudent(studentType student)
{
    cout << student.studentFName << " " << student.studentLName
        << "" << student.testScore
        << "" << student.labScore
        << "" << student.grade << endl;
}
int main()
{

    studentType student1;
    studentType student2;
    studentType student3;

    student1.studentFName = "John";
    student1.studentLName = "White";
    student1.testScore = 88;
    student1.labScore = 70;
    student1.grade = getGrade(student1.testScore, student1.labScore);

    student2.studentFName = "Alisa";
    student2.studentLName = "Brown";
    student2.testScore = 90;
    student2.labScore = 64;
    student2.grade = getGrade(student2.testScore, student2.labScore);

    student3.studentFName = "Mike";
    student3.studentLName = "Green";
    student3.testScore = 75;
    student3.labScore = 97;
    student3.grade = getGrade(student3.testScore, student3.labScore);

    void printstudent(studentType student);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-04 21:11:45

这个..。

代码语言:javascript
复制
void printstudent(studentType student);

不是如何调用函数(它是函数声明)。

将这一行替换为

代码语言:javascript
复制
printStudent(student3);
// ^^ name of the function to call
//           ^^ parameter(s) passed to the function

我得到以下输出:

代码语言:javascript
复制
Mike Green7597A

您可能希望添加一些空白,并打印其他学生。我建议您学习std::vector和循环以简化代码。

票数 2
EN

Stack Overflow用户

发布于 2020-02-04 21:11:36

代码的最后一行是函数声明。这应该是函数调用。用下面代码中的一行或全部行替换它:

代码语言:javascript
复制
printstudent(student1);
printstudent(student2);
printstudent(student3);

对于函数调用,您需要function_name,括号中将参数和分号放在末尾。

代码语言:javascript
复制
function_name(argument);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60065456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档