首页 > 解决方案 > C++结构练习

问题描述

我正在尝试完成结构练习;

  1. 一个。在本课程中,您将使用以下 3 个假设的学生信息来定义和声明结构数据。假设 Lab 分数是 70%,Test 分数是总成绩的 30%。
First name (string)   :   John            Alisa           Mike    
Last name (string)    :   White           Brown           Green
Course Grade (char)   :   0 (to be calculated)    “           “
Test score (int)      :   88          90          75
Lab score (int)       :   70          64          97

湾。执行以下任务:

  • 以结构数据类型初始化/声明每个学生。请注意 MVS 的 IntelliSense 功能。
  • 使用函数调用(即 getGrade)计算课程成绩。这可以是 void 函数传递引用或适当的 char 函数来返回课程成绩。输入将是测试分数和实验室分数。百分比(30% 和 70%)可以定义为全局双常数。使用逐步增量的方法来开发您的代码。
  • 将学生信息显示给用户。尝试使用函数调用来打印此输出(参见第 620 页)。并询问我有关如何进行的一些想法。示例输出可能是:
John    White   Grade is: C     Test Score is: 88       Lab Score is: 70
Alisa   Brown   Grade is: C     Test Score is: 90       Lab Score is: 64
Mike    Green   Grade is: A     Test Score is: 75       Lab Score is: 97
Press any key to continue . . .

这是我到目前为止汇总的内容并且被卡住了 - 不知道为什么我没有得到想要的输出。(任何帮助将不胜感激!):

//
#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);
}

标签: c++visual-c++struct

解决方案


这个..

void printstudent(studentType student);

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

将该行替换为

printStudent(student3);
// ^^ name of the function to call
//           ^^ parameter(s) passed to the function

我得到以下输出:

Mike Green7597A

您可能想添加一些空白并打印其他学生。我建议您学习std::vector和循环以使您的代码更容易。


推荐阅读