簡體   English   中英

C++ 中的數據類型

[英]Data Types in C++

我正在完成一項填寫空白數據類型的作業。 我已經多次重讀了“學習 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;
}

我知道我不明白如何稱呼綠色。 我嘗試了書中建議的各種組合,但似乎沒有任何效果。

請幫助我解決這個問題並了解原因。

先感謝您。

就像 Ceros 在評論中所說的那樣,您需要附加枚舉類型:

colorCode gradebookColor = colorCode::Green;

更改變量的類型。

enum colorCode
{
  Green = 1
};

colorCode gradebookColor = colorCode::Green;

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

將輸出“1”

當你定義一個enum class你必須在enum的名稱前面加上它的值: colorCode::Green 您也可以在沒有class說明符的情況下定義您的enum ,然后名稱是可選的。 但是,請記住, enum隨后可以隱式地從int轉換為int

數據類型:--數據類型是標識數據類型和處理數據的相關操作的手段。 基本上有兩種數據類型 1. 基本數據類型(int、char、bool、float、double、void) 2. 派生數據類型(數組、指針、結構、類等)有關簡單語言的更多詳細信息,請單擊給定的鏈接下面https://simplifiedtutorial4u.blogspot.in/2017/08/data-types-in-c.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM