How i can print an element from the enum giorni at the line 15?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<stdio.h>
int n;
typedefenum {
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
} giorni;
int main(){
printf("Inserisci un numero da 1 a 7: ");
scanf("%d",&n);
printf("\nGiorno corrispondente: ",giorni->n-1);
}
Enums are just a list of named numerical constants (in C++ they also provide some additional type safety, but not sure about C). There is no automatic way to convert an enum or integer value to a string.
What you have done is that you have created a type called "giorni". You can now create variables of that type if you want. The possible values that the variable can have are Lunedi, Martedi, Mercoledi, Giovedi, Venerdi, Sabato and Domenica.
1 2
// Creates a variable named g with the value Giovedi.
giorni g = Giovedi;
All the possible enum values are just numbers (but the type is different). If you convert a giorni to an int Lunedi will give you the value 0, Martedi the value 1, Mercoledi the value 2, and so on. The conversion from enum type to int is implicit so you can print the value the same way you do with normal integers.
1 2 3
// Prints the underlying numerical value of g.
// If g == Giovedi this will print "3".
printf("%d\n",g);
Looking at the program you have written I'm not sure enums are very useful at all. If you want the user to input a number and then the program should output some text depending on what the number is, then I think you should probably use an array or a switch instead to do the lookup (int -> string).
But this is a bit pointless because when you pass it to the function the value will be converted back to an int so what gets printed is the value of n.
1 2
// This will do the same as above.
printf("\nGiorno corrispondente: %d", n);
this is what i want to do...
if the user writes 1, the program answers "lunedi" (the first element or f*cking thing of the enum giorni).
i want the program writes the day that correspond at the int n.
An enum is not an array or container so it doesn't have elements. It's just a way to create a new type with a defined list of possible values.
Names of types, functions, variables, etc. are not part of the final compiled program (except maybe as debug information). It's there to help you as a programmer write the code. The final executable file doesn't need them. So values Lunedi, Martedi, etc. are just for you to use in your code, but if you want to print these names you will have to write the code for doing so.
You could have an array of strings and then use the variable n as an index in the array to lookup the string that you want (don't forget check that the index is in the correct range), or you could use a switch statement (or a chain of if-else) to handle each of the possible integer values and print the corresponding names.
Here is a backwards way of doing what you want. I did not complete it but I did a few to test it and it worked fine. Unfortunately its not going to be necessarily using the data type you created but will give the result. I don't really agree with this approach but just showing a possible solution. Peter87's idea of using an array to represent the info you have is a more brilliant way of going about it. It just comes down to: are you looking for a simple fix to a problem or are you really interested in figuring out how to work with and manipulate the data to work for you. Note that if you try to print an enum, say Lunedi, alone and it does print anything. It will be printing its value, which will be viewed as 0,1,2... All of which is relying on what order it is in. The first enum is 0 and so on. That is partly why I think the array idea is better.
Sorry for the multi reply in advance, but I see your determined to keep the enum. Is that because you want to keep the user defined data type or because you want the enum? I ask because if your simply determined to keep the user defined data type you could always use a struct. Structs are a great way to get into user defined data, in my opinion, also helps with prepping for classes, easy to use, also my opinion.....
if the user writes 1, the program answers "lunedi" (the first element or f*cking thing of the enum giorni).
i want the program writes the day that correspond at the int n.
Enumerated constants won't work for these requirements. Strings is what you want.