C++使用 for 循环的枚举来填充浮点数组
C++ using enumerations with for loops to fill a float Array
我的练习是:
编写一个程序,获取用户对颜色强度和透明度的选择。对颜色和透明度使用单个枚举 -
RED
、GREEN
、BLUE
、ALPHA
。使用利用枚举集从RED
循环到ALPHA
(含(循环for
循环。在for
循环中,让用户为每个枚举常量输入一个值(即红色值,蓝色值等(,这些值应该在0.0
和1.0
之间,并存储在数组中。
#include <string>
#include <iostream>
enum Difficulty
{
RED,
GREEN,
BLUE,
ALPHA
};
int main(int argc, char* argv[])
{
char cColours[10][14] = { "RED", "GREEN", "BLUE", "ALPHA" };
float fArray[4];
int icounter = 0;
while (icounter != 5)
{
std::cout << "For colour " << cColours[icounter] << " please enter a number ranging from 0.0 - 1.0 " << std::endl;
std::cout << "press 10 to exit " << std::endl;
for (int i = RED; i = ALPHA; i++)
std::cin >> fArray[i];
++icounter;
}
system("pause");
return 0;
}
根据评论,你想要得到一种颜色混合(r,g,b,a(。你白白使用嵌套数组。
所以一个可能的代码:
#include <iostream>
#include <string>
#include <stdio.h>
enum ColorChannels {RED = 0, GREEN, BLUE, ALPHA, ALL_CHANNELS};
/*Every enumeration gives values to its elements. In default the first element is zero,
I illustrated this here. So ALL_CHANNELS (or difficulty if you want is equal with four,
which is the number of all channels - this will be useful in loop*/
int main(int argc, char* argv[])
{
std::string names[ALL_CHANNELS];
names[RED] = "RED"; //this is same to names[0]
names[1] = "GREEN";
names[BLUE] = "BLUE";
names[3] = "ALPHA";
/*I'm not sure if char sequences work. If work, use it. I prefer more the strings*/
float colorArray[ALL_CHANNELS];
for (int i = 0; i < ALL_CHANNELS; i++)
{
std::cout << "Enter " << names[i] << " value between 0.0 and 1.0: ";
//For instance "Enter RED value between 0.0 and 1.0: "
//and NOW your read one parameter. You made a nested loop for nothing
std::cin >> colorArray[i];
std::cout << "n"; //for a new line
}
// all elements read
system("pause");
return 0;
}
尝试对锻炼做一些改进,比如写回颜色;写出它是否更有可能是红色,蓝色或绿色;阅读更多颜色;等等。
相关文章:
- C++ 填充数组时遇到问题
- 如何使用同时设置计数和填充数组的函数
- 如何使填充数组的函数发送到函数?而不是其参数中的数组
- 如何使用文件填充数组并将其与用户输入C 进行比较
- 使用 memset 填充数组会显示垃圾
- Visual Studio从文件中填充数组时中断
- 分段 填充数组时出错
- 用不同的值填充数组/矢量的边缘和中心
- 使用相同的数据填充数组对象或使用指针
- 是否可以使用三元运算符"?"在 C/C++ 中填充数组列表?
- 填充数组(写入位置0x00000000的访问冲突)
- 从文件中读取输入并填充数组以存储数据供以后使用
- c++当循环在填充数组时没有正确终止时,希望能帮助退出此循环
- 填充数组会导致数组周围的堆栈损坏
- Cuda有效地将数据插入到未排序的填充数组中
- 填充数组的 C++ 函数
- 访问部分填充数组上的第一个打开元素
- 将一个字节数组从C#传递到C++填充数组的C++com对象中
- 如何使用向量中的信息填充数组并从数组中选择随机项
- C,填充数组使用dec2bin转换器