typedef enum

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;
typedef enum {
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.
i don't know how to print a number from an enum. please help me!!! i don't want to convert them!!
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).
sorry but my program doesn't work. it says "expected primary expression before 'n'.

#include<stdio.h>
int n;
typedef enum {
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
} giorni;
int main(){
printf("Inserisci un numero da 1 a 7: ");
scanf("%d",&n);
printf("\nGiorno corrispondente: %d",giorni n);
}
What do you want giorni n to do? Convert (cast) n to a giorni? Well then you can do (giorni) n.

 
printf("\nGiorno corrispondente: %d", (giorni) n);

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);
Last edited on
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.
First have you tried seeing if changing the printf( ); to cout<< variable;

Also, not that Im a big fan of ways around true solutions, but your always given the option of a switch statement. If all you want is:
if the user writes 1, the program answers "lunedi"
. A switch statement would easily solve this problem.
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.
Last edited on
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>

#include<iostream>
using namespace std;

int n;
typedef enum {
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
} giorni;

int main(){
	cout << "Enter number between 1  - 7: " ;
	cin >> n; 
	switch(n)
               {
               case 1:
                    cout<<"Lunedi";
                    break;
               case 2:
                    cout<<"Martedi";
                    break;
               case 3:
                    cout<<"Mercoledi";
                    break;
               default:
                    cout<<"What to say in default?";
}
return 0;
}
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.....

There's a lot of weird information here.

Whether 'typedef'ed or not, a C-style enum is just a named int. You're playing with ints.

Your code has identifiers for each value, but there is no way to look up the identifier from its value. You have to create a table to do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <ciso646>
#include <iostream>
#include <limits>

const char* weekday_names[] =
{
  nullptr, 
  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};

enum weekday_values
{
  Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};

int main()
{
  int n;
  std::cout << "Input the day of week as a number in 1..7: ";
  std::cin >> n;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  
  weekday_values weekday = (weekday_values)n;
  std::cout << "That's " << weekday_names[ weekday ] << "!\n";
  
  std::cout << "Wednesday is day number " << Wednesday << ".\n";
}

The whole enum↔string/struct/whatever thing is a common problem, one which a certain macro pattern exists to make life easier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <ciso646>
#include <iostream>
#include <limits>

#define FOREACH_OP(F) \
  F(DayZero)   \
  F(Sunday)    \
  F(Monday)    \
  F(Tuesday)   \
  F(Wednesday) \
  F(Thursday)  \
  F(Friday)    \
  F(Saturday)
  
#define ENUMF(x)    x ,
#define STRINGF(x) #x ,

const char* weekday_names[] = { FOREACH_OP(STRINGF) };
enum        weekday_values    { FOREACH_OP(ENUMF)   };

int main()
{
  int n;
  std::cout << "Input the day of week as a number in 1..7: ";
  std::cin >> n;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  
  weekday_values weekday = (weekday_values)n;
  std::cout << "That's " << weekday_names[ weekday ] << "!\n";
  
  std::cout << "Wednesday is day number " << Wednesday << ".\n";
}

Hope this helps.
All these codes are too difficult for me!! Please try to explain easier.
Lets see where you stand with some of this. What is an enum?
Also, why are you determined to,
please help me!!! i don't want to convert them!!
, not convert them?
Last edited on
closed account (E0p9LyTq)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

std::string giorni[] =
{
   "Lunedi",
   "Martedi",
   "Mercoledi",
   "Giovedi",
   "Venerdi",
   "Sabato",
   "Domenica"
};

int main()
{
   int n;
   std:: cout << "Inserisci un numero da 1 a 7: ";
   std::cin >> n;

   std::cout << "\nGiorno corrispondente: "  << giorni[n - 1] << "\n";

   return 0;
}

Inserisci un numero da 1 a 7: 3

Giorno corrispondente: Mercoledi
Topic archived. No new replies allowed.