Thread: [Dev-C++] Make switch condition an enum
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2002-10-18 11:45:18
|
Hi,
I have the following chunk of code, and I've been asked to make the switch
(SomeError) statement use an enum. Well I was told to "make that an enum".
How would I create the enum and use it in the switch statement?
// Get error strings
switch (SomeError)
{
case ERROR_ONE:
DebugOut = _T("Error One!");
break;
case ERROR_TWO:
DebugOut = _T("Error Two!");
break;
default:
DebugOut = _T("Unspecified error!");
break;
}
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
|
|
From: Zodiaq <zo...@ac...> - 2002-10-18 12:21:41
|
Hello Daniel and all Dev-C++ users,
Friday, October 18, 2002, 1:45:12 PM, Daniel wrote as follows:
DG> Hi,
DG> I have the following chunk of code, and I've been asked to make the switch
DG> (SomeError) statement use an enum. Well I was told to "make that an enum".
DG> How would I create the enum and use it in the switch statement?
enum ERROR_TYPES { ERROR_ONE = 1, ERROR_TWO, OTHER_ERROR,
EVEN_MORE_OTHER_ERROR };
ERROR_TYPES SomeError = ERROR_TWO;
DG> // Get error strings
DG> switch (SomeError)
DG> {
DG> case ERROR_ONE:
DG> DebugOut = _T("Error One!");
DG> break;
DG> case ERROR_TWO:
DG> DebugOut = _T("Error Two!");
DG> break;
DG> default:
DG> DebugOut = _T("Unspecified error!");
DG> break;
DG> }
does it works??
--
regards, Zodiaq
|
|
From: Abhijit S. <mu...@gm...> - 2002-10-18 14:42:19
|
> I have the following chunk of code, and I've been asked to make the switch
> (SomeError) statement use an enum. Well I was told to "make that an enum".
>
> How would I create the enum and use it in the switch statement?
>
>
> // Get error strings
> switch (SomeError)
> {
> case ERROR_ONE:
> DebugOut = _T("Error One!");
> break;
>
> case ERROR_TWO:
> DebugOut = _T("Error Two!");
> break;
>
> default:
> DebugOut = _T("Unspecified error!");
> break;
> }
Something like this:
enum ErrorType { ERROR_ONE = 1, ERROR_TWO }
and then the same code with SomeError being of type ErrorType rather than
int (as I assume it is here).
For those of you who are interested, a helmet saved my life yesterday, after
a crash at 90 kph.
___________________________________________________________
Abhijit Shylanath
E-mail: mu...@gm... || ibr...@bi...
Web-site: http://mudeth.tripod.com/
|
|
From: Ioannis V. <no...@ho...> - 2002-10-18 23:37:34
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Daniel Glenfield
> Sent: Friday, October 18, 2002 2:45 PM
> To: dev...@li...
> Subject: [Dev-C++] Make switch condition an enum
>
>
>
> Hi,
> I have the following chunk of code, and I've been asked to
> make the switch
> (SomeError) statement use an enum. Well I was told to "make
> that an enum".
>
> How would I create the enum and use it in the switch statement?
Enumerators are user defined integral types taking discrete, user
defined values. For example:
// Mon=0, Tue=1, Wed=2, Thu=8, Fri=9 Sat=20 Sun=21
enum Days { Mon, Tue, Wed, Thu=8, Fri, Sat=20, Sun};
The above is the type. It does not occupy space the same as int, or a
class does not occupy space by itself. Its objects do:
Days oneday=Wed;
switch(oneday)
{
case Mon:
// ...
}
> // Get error strings
> switch (SomeError)
> {
> case ERROR_ONE:
> DebugOut = _T("Error One!");
> break;
>
> case ERROR_TWO:
> DebugOut = _T("Error Two!");
> break;
>
> default:
> DebugOut = _T("Unspecified error!");
> break;
> }
What is that _T you are using all the time anyway? It seems to me
something like "boxing" (as it happens in .NET for example).
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2002-10-19 11:25:54
|
The _T(x) is a macro used by a Microsoft Developer Studio, the wxWindows
framework etc, to handle wide/non-wide, characters. All you have to do is
to #define _UNICODE (or sometimes #define UNICODE, or in wxWindows
#define wxUSE_UNICODE 1) to switch all "tagged"
strings to wide character strings.
/Per W
On Sat, 19 Oct 2002, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Daniel Glenfield
> > Sent: Friday, October 18, 2002 2:45 PM
> > To: dev...@li...
> > Subject: [Dev-C++] Make switch condition an enum
> >
> >
> >
> > Hi,
> > I have the following chunk of code, and I've been asked to
> > make the switch
> > (SomeError) statement use an enum. Well I was told to "make
> > that an enum".
> >
> > How would I create the enum and use it in the switch statement?
>
> Enumerators are user defined integral types taking discrete, user
> defined values. For example:
>
> // Mon=0, Tue=1, Wed=2, Thu=8, Fri=9 Sat=20 Sun=21
> enum Days { Mon, Tue, Wed, Thu=8, Fri, Sat=20, Sun};
>
>
> The above is the type. It does not occupy space the same as int, or a
> class does not occupy space by itself. Its objects do:
>
>
> Days oneday=Wed;
>
>
> switch(oneday)
> {
> case Mon:
> // ...
> }
>
>
> > // Get error strings
> > switch (SomeError)
> > {
> > case ERROR_ONE:
> > DebugOut = _T("Error One!");
> > break;
> >
> > case ERROR_TWO:
> > DebugOut = _T("Error Two!");
> > break;
> >
> > default:
> > DebugOut = _T("Unspecified error!");
> > break;
> > }
>
>
> What is that _T you are using all the time anyway? It seems to me
> something like "boxing" (as it happens in .NET for example).
>
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
>
>
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:
> Access Your PC Securely with GoToMyPC. Try Free Now
> https://www.gotomypc.com/s/OSND/DD
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-19 18:04:36
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Saturday, October 19, 2002 2:26 PM
> To: Ioannis Vranos
> Cc: dev...@li...
> Subject: RE: [Dev-C++] Make switch condition an enum
>
>
> The _T(x) is a macro used by a Microsoft Developer Studio,
> the wxWindows
> framework etc, to handle wide/non-wide, characters. All you
> have to do is
> to #define _UNICODE (or sometimes #define UNICODE, or in wxWindows
> #define wxUSE_UNICODE 1) to switch all "tagged"
> strings to wide character strings.
I do not know Win32 API-specific stuff but concerning ANSI C and ANSI
C++ you can define a string literal as a wide character one by placing
the capital letter L in front of it. For example consider the C90
program:
#include <stdio.h>
#include <stddef.h>
int main()
{
const wchar_t *ws=L"Wide character string";
wprintf(L"%s\n", ws);
return 0;
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2002-10-19 19:03:37
|
Guess the definition of _T("x")...
#if defined(_UNICODE) || defined(UNICODE)
#define _T(x) L##x
#else
#define _T(x) x
#endif
This is the OFFICIAL way of writing portable MFC or wxWindows programs.
The L"Hello world" concept requires you to change a huge number of strings
whenever you want to activate/deactivate unicode support.
As you can probably guess, all functions taking a string as a parameter
must also be specially handled.
// Microsoft
#if defined(_UNICODE) || defined(UNICODE)
#define _tcslen wcslen
#else
#define _tcslen strlen
#endif
// wxWindows
size_t wxStrlen(const wxChar *s);
/Per W
On Sat, 19 Oct 2002, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Saturday, October 19, 2002 2:26 PM
> > To: Ioannis Vranos
> > Cc: dev...@li...
> > Subject: RE: [Dev-C++] Make switch condition an enum
> >
> >
> > The _T(x) is a macro used by a Microsoft Developer Studio,
> > the wxWindows
> > framework etc, to handle wide/non-wide, characters. All you
> > have to do is
> > to #define _UNICODE (or sometimes #define UNICODE, or in wxWindows
> > #define wxUSE_UNICODE 1) to switch all "tagged"
> > strings to wide character strings.
>
>
> I do not know Win32 API-specific stuff but concerning ANSI C and ANSI
> C++ you can define a string literal as a wide character one by placing
> the capital letter L in front of it. For example consider the C90
> program:
>
>
> #include <stdio.h>
> #include <stddef.h>
>
> int main()
> {
> const wchar_t *ws=L"Wide character string";
>
> wprintf(L"%s\n", ws);
>
> return 0;
> }
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
>
>
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:
> Access Your PC Securely with GoToMyPC. Try Free Now
> https://www.gotomypc.com/s/OSND/DD
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-20 03:23:37
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Saturday, October 19, 2002 10:03 PM
> To: Ioannis Vranos
> Cc: dev...@li...
> Subject: RE: [Dev-C++] Make switch condition an enum
>
>
> Guess the definition of _T("x")...
>
> #if defined(_UNICODE) || defined(UNICODE)
> #define _T(x) L##x
> #else
> #define _T(x) x
> #endif
>
> This is the OFFICIAL way of writing portable MFC or wxWindows
> programs.
> The L"Hello world" concept requires you to change a huge
> number of strings
> whenever you want to activate/deactivate unicode support.
>
> As you can probably guess, all functions taking a string as a
> parameter
> must also be specially handled.
>
> // Microsoft
> #if defined(_UNICODE) || defined(UNICODE)
> #define _tcslen wcslen
> #else
> #define _tcslen strlen
> #endif
>
> // wxWindows
> size_t wxStrlen(const wxChar *s);
>
> /Per W
Well let UNICODE aside. wchar_t is a wide character type existing in
*all* machines. It belongs to the C/C++ standards. In C90 wchar_t is a
typedef defined in stddef.h. In C++98 it is a *built in* type (and in
all machines using UNICODE, wchar_t is usually UNICODE).
I see no reason why "activate" or "deactivate" wide character support
using "compilation under condition". If your code needs wide character
support so as to work in some platforms, use wchar_t in the first place
(something bad, from all the compilers i have seen, wcout doesn't exist
in MINGW's <iostream> and probably in GCC itself).
Forget all the Win32 bad programming styles. :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2002-10-20 16:43:04
|
Bad programming styles aside.
While WinNT/Win2k/WinXP uses Unicode-only in the kernel, older Win95,
Win98 don't have unicode, and have to try and convert any unicode calls
back to 8-bit characters. Guess what. That takes time. What use do I have
of a wchar_t type, when it doesn't exist on the OS?
By #undef _UNICODE, a _T("x") program would then compile to a multi-byte
character set program, i.e. a lot of strange characters without a 16-bit
character type.
Life isn't only about C90, C98, C++98 etc. To write programs the customer
is happy with, you have to make due with the limitations of the OS,
available compilers, ...
/Per W
On Sun, 20 Oct 2002, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Saturday, October 19, 2002 10:03 PM
> > To: Ioannis Vranos
> > Cc: dev...@li...
> > Subject: RE: [Dev-C++] Make switch condition an enum
> >
> >
> > Guess the definition of _T("x")...
> >
> > #if defined(_UNICODE) || defined(UNICODE)
> > #define _T(x) L##x
> > #else
> > #define _T(x) x
> > #endif
> >
> > This is the OFFICIAL way of writing portable MFC or wxWindows
> > programs.
> > The L"Hello world" concept requires you to change a huge
> > number of strings
> > whenever you want to activate/deactivate unicode support.
> >
> > As you can probably guess, all functions taking a string as a
> > parameter
> > must also be specially handled.
> >
> > // Microsoft
> > #if defined(_UNICODE) || defined(UNICODE)
> > #define _tcslen wcslen
> > #else
> > #define _tcslen strlen
> > #endif
> >
> > // wxWindows
> > size_t wxStrlen(const wxChar *s);
> >
> > /Per W
>
>
> Well let UNICODE aside. wchar_t is a wide character type existing in
> *all* machines. It belongs to the C/C++ standards. In C90 wchar_t is a
> typedef defined in stddef.h. In C++98 it is a *built in* type (and in
> all machines using UNICODE, wchar_t is usually UNICODE).
>
> I see no reason why "activate" or "deactivate" wide character support
> using "compilation under condition". If your code needs wide character
> support so as to work in some platforms, use wchar_t in the first place
> (something bad, from all the compilers i have seen, wcout doesn't exist
> in MINGW's <iostream> and probably in GCC itself).
>
>
> Forget all the Win32 bad programming styles. :)
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:
> Access Your PC Securely with GoToMyPC. Try Free Now
> https://www.gotomypc.com/s/OSND/DD
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-21 02:21:27
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Per Westermark
> Sent: Sunday, October 20, 2002 7:43 PM
> To: Ioannis Vranos
> Cc: dev...@li...
> Subject: RE: [Dev-C++] Make switch condition an enum
>=20
>=20
> Bad programming styles aside.
>=20
> While WinNT/Win2k/WinXP uses Unicode-only in the kernel, older Win95,
> Win98 don't have unicode, and have to try and convert any=20
> unicode calls
> back to 8-bit characters. Guess what. That takes time. What=20
> use do I have
> of a wchar_t type, when it doesn't exist on the OS?=20
>=20
> By #undef _UNICODE, a _T("x") program would then compile to a=20
> multi-byte
> character set program, i.e. a lot of strange characters=20
> without a 16-bit
> character type.
>=20
> Life isn't only about C90, C98, C++98 etc. To write programs=20
> the customer
> is happy with, you have to make due with the limitations of the OS,
> available compilers, ...
Well i do not know Win32 API programming. What i know is that wchar_t is
supported in every C++98 compliant compiler even if the OS doesn't
support UNICODE.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2002-10-21 10:46:40
|
Most compilers support float and double too, but if the underlying
processor doesn't support it and the c libraries have to supply a software
emulation, the performance of the program will really suck.
The trick is to know the target environment before starting to code...
When the target OS doesn't have wide character support, the use of wchar_t
isn't recommended.
/Per W
On Mon, 21 Oct 2002, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Per Westermark
> > Sent: Sunday, October 20, 2002 7:43 PM
> > To: Ioannis Vranos
> > Cc: dev...@li...
> > Subject: RE: [Dev-C++] Make switch condition an enum
> >
> >
> > Bad programming styles aside.
> >
> > While WinNT/Win2k/WinXP uses Unicode-only in the kernel, older Win95,
> > Win98 don't have unicode, and have to try and convert any
> > unicode calls
> > back to 8-bit characters. Guess what. That takes time. What
> > use do I have
> > of a wchar_t type, when it doesn't exist on the OS?
> >
> > By #undef _UNICODE, a _T("x") program would then compile to a
> > multi-byte
> > character set program, i.e. a lot of strange characters
> > without a 16-bit
> > character type.
> >
> > Life isn't only about C90, C98, C++98 etc. To write programs
> > the customer
> > is happy with, you have to make due with the limitations of the OS,
> > available compilers, ...
>
>
> Well i do not know Win32 API programming. What i know is that wchar_t is
> supported in every C++98 compliant compiler even if the OS doesn't
> support UNICODE.
>
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
>
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:
> Access Your PC Securely with GoToMyPC. Try Free Now
> https://www.gotomypc.com/s/OSND/DD
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-22 04:20:54
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Monday, October 21, 2002 1:46 PM > To: Ioannis Vranos > Cc: dev...@li... > Subject: RE: [Dev-C++] Make switch condition an enum > > > Most compilers support float and double too, but if the underlying > processor doesn't support it and the c libraries have to > supply a software > emulation, the performance of the program will really suck. Well it will support some floating point type, willn't it? float can be the same with double. > > The trick is to know the target environment before starting to code... > When the target OS doesn't have wide character support, the > use of wchar_t > isn't recommended. Why? wchar_t can be the same as an int for example. While everything will work, like wprintf() etc why not use it? Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |