C ++中的数据类型

时间:2017-05-10 15:09:14

标签: c++ .net

我正在进行分配以填写空白数据类型。 我在“学习C ++进行游戏开发”中多次重读小章节 并试过各种类型,但是, 我的问题是从颜色调用绿色 枚举。

以下是原始代码:

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

我知道我不明白如何调用绿色。我尝试了书中提出的各种组合,但似乎没有任何效果。

请帮忙,以便我可以解决这个问题并理解原因。

提前谢谢。

3 个答案:

答案 0 :(得分:1)

就像Ceros在评论中所说,你需要附加枚举类型:

colorCode gradebookColor = colorCode::Green;

答案 1 :(得分:1)

更改变量的类型。

enum colorCode
{
  Green = 1
};

colorCode gradebookColor = colorCode::Green;

std::cout << gradebookColor << std::endl;

将输出&#34; 1&#34;

答案 2 :(得分:0)

数据类型: - 数据类型是识别数据类型和处理它的相关操作的手段。 基本上有两种类型的数据类型 1.基本数据类型(int,char,bool,float,double,void) 2.derived数据类型(数组,指针,结构,类等) 有关简单语言的更多详细信息,请单击下面给出的链接 https://simplifiedtutorial4u.blogspot.in/2017/08/data-types-in-c.html