gpt4 book ai didi

c++ - C++ 中的数据类型

转载 作者:行者123 更新时间:2023-11-27 23:54:09 24 4
gpt4 key购买 nike

我正在做一项填空数据类型的作业。《Learn C++ for Game Development》里面的小章节我重读了无数遍并尝试了各种类型,但是,我的问题是从枚举。

原代码如下:

// DataTypes.cpp : The data types to declare each of the variables is missing.
// Based on the value being stored in the variable and the comments beside it,
// fill in the data type at the beginning of each line. Then compile and run
// program to make sure you selected the correct types.
//
// After you submit your answers, try changing the values stored in the
// variables. What can you learn about the different data types?
//

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int atgc, const char * arg[])
{
classAverage = 90.7f; //Decimal number
letterScore = 'A'; //Single letter
testScore = 95; //Whole number value
classTestAverage = 88.4f; //Decimal number, notice the 'f' at the end
colorCode{
Green = 1,
Yellow = 5,
Red = 10
} gradebookColor; //Stores list of values
gradebookColor = Green; //This line does not need a declaration, it was declared in the line above
isStudentPassing = true; //Could be true or false

cout << "The class average is currently "
<< classAverage
<< endl;
cout << "The class test average was "
<< classTestAverage
<< endl;
cout << "Your test score was "
<< testScore
<< endl;
cout << "Your current letter score is "
<< letterScore
<< endl;
cout << "The color of your gradebook entry is "
<< gradebookColor
<< endl;
cout << "Are you passing? "
<< boolalpha //This line allows the word 'true' or 'false' to be printed instead of '0' or '1'
<< isStudentPassing
<< endl;
return 0;
}

这是我到目前为止完成的:

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int atgc, const char * arg[])
{
float classAverage = 90.7f; //Decimal number
char letterScore = 'A'; //Single letter
int testScore = 95; //Whole number value
float classTestAverage = 88.4f; //Decimal number, notice the 'f' at the end
enum class colorCode {
Green = 1,
Yellow = 5,
Red = 10
};

unsigned int gradebookColor; //Stores list of values
colorCode gradebookColor = colorCode::Green; //This line errors out bool isStudentPassing = true; //Could be true or false
cout << "The class average is currently "
<< classAverage
<< endl;
cout << "The class test average was "
<< classTestAverage
<< endl;
cout << "Your test score was "
<< testScore
<< endl;
cout << "Your current letter score is "
<< letterScore
<< endl;
cout << "The color of your gradebook entry is "
<< gradebookColor
<< endl;
cout << "Are you passing? "
<< boolalpha //This line allows the word 'true' or 'false' to be printed instead of '0' or '1'
<< isStudentPassing
<< endl;
return 0;
}

我知道我不明白如何称呼绿色。我尝试了书中建议的各种组合,但似乎没有任何效果。

请帮助我解决这个问题并了解原因。

提前谢谢你。

最佳答案

就像 Ceros 在评论中所说的那样,您需要附加枚举类型:

colorCode gradebookColor = colorCode::Green;

关于c++ - C++ 中的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com