Hi, folks~
what about an enum with a for loop ?
I write code like this:
enum pet{dog,cat,bird,crocodile};
for(i=dog;i<crocodile;i++)
{
do something......;
}
but it didnt work, how could I get it correct ???
thanx in advance~
Hi, folks~
what about an enum with a for loop ?
I write code like this:
enum pet{dog,cat,bird,crocodile};
for(i=dog;i<crocodile;i++)
{
do something......;
}
but it didnt work, how could I get it correct ???
thanx in advance~
Never end on learning~
This works for me :
The output is :Code:#include <stdio.h> #include <stdlib.h> enum pet{dog,cat,bird,crocodile}; main() { for(int i=dog;i<=crocodile;i++) { printf("%04d\n\r", i); } system("Pause"); }
0000
0001
0002
0003
I am using Borland C++ 5.02.
If this is not working try :
enum pet{dog=0,cat,bird,crocodile};
Of course you can. Basically an enum is a special type of integer container. That is, you couldn't do: enum fraction { half = 0.5, ...etc}. But yes, they are especially useful for loops...
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
You might have forgot int i; in your code?
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
the entire code is right below:Originally posted by Magos
You might have forgot int i; in your code?
#include <iostream>
#include <string>
void main()
{
enum pet{dog,cat,bird,crocodile};
pet myPet;
for(i=dog;i<=crocodile;i++)
{
cout << myPet(i) << endl;
}
}
it seems nothing is of lack but couldnt work yet...............
Is there any special rules about enum ?
Never end on learning~
Like he said. int i; is missing. It reamins undefined, and you're getting into some trouble with your cout statement.
I fixed my code as follows, but no correct response yet.Originally posted by Sean
Like he said. int i; is missing. It reamins undefined, and you're getting into some trouble with your cout statement.
#include <iostream>
#include <string>
void main()
{
enum pet{dog,cat,bird,crocodile};
pet myPet;
int i;
for(i=dog;i<=crocodile;i++)
{
cout << myPet(i) << endl;
}
}
Never end on learning~
No no no. Start over! Think of an enum as an 'alias' for an int. Nothing more. And you will not be able to print the enumerations automatically by name! Look:
enum pet { dog, cat, crocodile };
Now, by default, the enum member 'dog' is an alias for the integer '0'. 'cat' is '1'. 'crocodile' is '2'. You can alter this default behavior, but you must experiment with it yourself. ie:
enum pet { dog = 2, cat = 4, crocodile = 8 };
So anyway, if you want to print the verbal representation of enumerated values, you must do it by hand.
Code:int main() { enum pet { dog, cat, crocodile }; int i; for(i = dog; i <= crocodile; i++) { switch(i) { case dog: cout << "dog" << endl; getch(); break; case cat: cout << "cat" << endl; getch(); break; case crocodile: cout << "crocodile" << endl; getch(); break; } } return 0; }
Of course, in the real world, you might use a corresponding string table...
Code:int main() { enum pet { dog, cat, crocodile }; char *look[] = {"dog", "cat", "crocodile"}; int i; for(i = dog; i <= crocodile; i++) { cout << look[i] << endl; getch(); } return 0; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
sorry, I tested both your codes, but they couldnt work either.Originally posted by Sebastiani
No no no...
Of course, in the real world, you might use a corresponding string table...Code:int main()...
Code:int main()...
my soft is DevC++. has my compiler any limitation when compiling ?
Never end on learning~
In C enum's are integers, but not in C++. Most compilers do the casting for you, but maybe DevC++ does not. Try casting the enum to int yourself :
It will also help if you can give the error messages you receive when trying to compile.Code:int main() { enum pet { dog = 0, cat, crocodile }; char *look[] = {"dog", "cat", "crocodile"}; int i; for(i = (int)dog; i <= (int)crocodile; i++) { cout << look[i] << endl; getch(); } return 0; }
Which compiler does the Dev-C++ IDE use?