473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to do with this case?

Hello I define a sequence of const
typedef struct
{
int year;
int month;
}birth
const birth a={1990,1};
const birth b={1990,2};
......
Now I'd like to use switch in my main function
I have

switch(test)
{
case a.month: ...
case b.month: ...
...
}

but the compiler says :
case label does not reduce to an integer constant

What should I do?
I don't want to use if.

Thanks a lot!

Nov 15 '05 #1
3 1628

<qi*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello I define a sequence of const
typedef struct
{
int year;
int month;
}birth
} birth;
const birth a={1990,1};
const birth b={1990,2};
.....
Now I'd like to use switch in my main function
I have

switch(test)
{
case a.month: ...
case b.month: ...
...
}

but the compiler says :
case label does not reduce to an integer constant
Right. They must be integer constants.

What should I do?
Use integer constants, or some other mechanism
which doesn't require them, e.g. 'if'.
I don't want to use if.


Why not? Are you under the misconception that 'switch'
is somehow inherently 'better' than 'if'?

Perhaps if you give more context about exactly what
you want your program to do, we could offer specific
advice on structuring your code.

-Mike
Nov 15 '05 #2
qi*****@gmail.com wrote:
Hello I define a sequence of const
typedef struct
{
int year;
int month;
}birth
const birth a={1990,1};
const birth b={1990,2};
.....
const in C specifies that an object is not modifiable, it does not
create a true compile time constant.
Now I'd like to use switch in my main function
I have

switch(test)
{
case a.month: ...
Your cases must be compile time constants.
case b.month: ...
...
}

but the compiler says :
case label does not reduce to an integer constant

What should I do?
I don't want to use if.


So use a constant, not a const qualified object.

#define A_MONTH (1)
const birth a={1990,A_MONTH};
....
switch (test)
case A_MONTH:
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #3
qi*****@gmail.com writes:
Hello I define a sequence of const
typedef struct
{
int year;
int month;
}birth
const birth a={1990,1};
const birth b={1990,2};
.....
Now I'd like to use switch in my main function
I have

switch(test)
{
case a.month: ...
case b.month: ...
...
}

but the compiler says :
case label does not reduce to an integer constant

What should I do?
I don't want to use if.


Declaring something as "const" doesn't make it a constant. It's
something best thought of as a read-only variable. Yes, it would be
nice if you could do what you're trying to do; unfortunately, the
language doesn't allow it.

You probably need to use a chain of if/else statements -- which isn't
such a bad thing, really.

If you really want to use case, you'll have to use constant
expressions. For example:

#define A_YEAR 1990
#define A_MONTH 1
#define B_YEAR 1991
#define B_MONTH 2
const birth a = {A_YEAR, A_MONTH};
const birth b = {B_YEAR, B_MONTH};

There's another trick that avoids the use of macros:

enum { A_YEAR = 1990, A_MONTH = 1,
B_YEAR = 1991, B_MONTH = 2 };
const birth a = { A_YEAR, A_MONTH };
const birth b = { B_YEAR, B_MONTH };

This only works for values of type int.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
9
by: Kevin | last post by:
Hi, I am getting a syntax error Microsoft VBScript compilation error '800a03ea' Syntax error On the code below. The error references the "End Select" line Can anyone help me with what I am...
5
by: Ryan | last post by:
I'm struggling with a Case statement. The problem I has is with doing >= I can use any value in there, but need to check if it's greater or equal to 1. I'm sure I'm missing something but can't...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
4
by: Samuels90 | last post by:
Hey everyone, My program is supposed to go to Column R Row 2. If Column M contain "01" it should run the "sum(if array" for that Case,(no pun intended), in Column R. Can someone tell me what is...
0
by: ANGanley | last post by:
Any ideas why i get an error in my script. set dateformat YMD set datefirst 7 CREATE TABLE #ChildSessions ( siteid integer null ,childid integer null ,sessionid integer null ,sun...
4
by: clairelee0322 | last post by:
My program has an error C2360: initialization of 'i' is skipped by 'case' label. I don't know why. Please help me out! thks!! the error occurs in Division case 3. //Claire's Calculator...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.