Archived

This topic is now archived and is closed to further replies.

need clarification on 2 simple things

This topic is 5044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can you not use a switch statement unless your evaluating one variable? IE it has to be switch (variable) case whatever: blah and NOT switch (variable - x) case whatever: blah??? and do all of an arrays elements automatically initialize to 0 when the array is declared? IE if i do int array[5]; will array[0] through array[4] be automatically initialized to 0?? the reson im asking is, because my professer (first semester CIS) is telling me that you can only use a switch if your evaluating a single variable. and he also told me that an arrays elements initialize to 0 automatically, but im not sure if hes right about either of these. he also didnt know what a recursive function was, he uses iostream.h instead of using namespace std; (i tried telling him that there is a difference but he doesnt believe me) , and he told my class that main should return void because main will never have to return anything. basically, hes an idiot and i dont trust anything he teaches me. ive honestly learned MUCH more from reading this board then i learned the whole semester with this jerk. thanks for any help! [edited by - graveyard filla on February 20, 2004 4:24:20 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
can you not use a switch statement unless your evaluating one variable?


No, it may be any expression. Try it.

quote:

and do all of an arrays elements automatically initialize to 0 when the array is declared?



No, they won''t.

Share this post


Link to post
Share on other sites
1) The syntax for switch is
switch(exspression) { case constant: break; }
So yes, variable-x would be perfectly valid.

2) Iffy. An array of a class will be default constructed.
int array[5] = { 0 }; supposably initializes them all to 0.

3) int main() {}. Main must always return an int. It doesn''t actually need a return statement though.

Share this post


Link to post
Share on other sites
I would write up a very detailed and commented code that proves both of those points wrong in a clearly understandable way, and present it to the class.

Of course, proving your teacher wrong like this can have repurcussions...


I''m not kidding when I say this...

I corrected a teacher reading from a science book in 5th grade who pronounced Socrates as "SO CRATES"

Share this post


Link to post
Share on other sites
thanks for the clarification. so, i could actually write ANY expression inside an case statement? such as

switch (a < b && c < d)


case ??: //<-----------what goes here?

how does this work???

also, it angers me very much to even think about how stupid my teacher is. it sickens me, actually. to think im paying thousands of dollars for school, just to learn bad programming practises. this isnt the first teacher ive had, either, whos taught bad programming. im coming up to the third semester now, and i guess im gunna stick with it. but if my new programming class has another idiotic teacher, im going to seriously consider switching schools. i just dont know if my credits will be transferrable or not, and that would suck to lose a years worth of school for nothing. i wish i did good in high school, so i could go to a good college. sigh..........


Share this post


Link to post
Share on other sites
I'm assuming this is all C++?

quote:
Original post by graveyard filla
so, i could actually write ANY expression inside an case statement? such as

switch (a < b && c < d)


case ??: //<-----------what goes here?

how does this work???



Yes, you can do that. But you should try to answer your own question about "what goes here?"...

What type does the expression (a < b && c < d) evaluate to? Once you understand that, it should be obvious what Case statements you can write (hint, there is only two!).

Your teacher probably meant that a switch can only be used to evaluate a single expression, but it does sound like he doesn't know what he's talking about (especially on the array thing). I would try to foster the impression that you want to help correct for the benefit of the entire class rather than make him look bad...

quote:

also, it angers me very much to even think about how stupid my teacher is. it sickens me, actually. to think im paying thousands of dollars for school, just to learn bad programming practises. this isnt the first teacher ive had, either, whos taught bad programming. im coming up to the third semester now, and i guess im gunna stick with it. but if my new programming class has another idiotic teacher, im going to seriously consider switching schools. i just dont know if my credits will be transferrable or not, and that would suck to lose a years worth of school for nothing. i wish i did good in high school, so i could go to a good college. sigh..........



Teachers are just people. Some people are brilliant, some people are smart, some people are average and some people are dumb. It's unfortunate that you don't have brilliant teachers, but that's the case for a majority of schools. Take consolation that you can learn from other sources like the web, library and playing with things on your own.

If you didn't do good in high school, was that the teacher's fault?


[ Little Devils Inc. ]

[edited by - rypyr on February 20, 2004 6:14:25 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Deyja
3) int main() {}. Main must always return an int. It doesn't actually need a return statement though.


This last one is compiler - specific. Try building it on a compiler for an ARM device and youll drive it crazy (without a return stmt).... hehe

[edited by - psamty10 on February 20, 2004 6:16:07 PM]

Share this post


Link to post
Share on other sites
so if i did

switch (a > b && c < d)

case true: blah blah
break;

case false: blah blah blah
break;


this would work? so using a case statement when evaluating an expression as opposed to evaluating a single variable, there could only be 2 different results???





[edited by - graveyard filla on February 20, 2004 6:56:22 PM]

Share this post


Link to post
Share on other sites
It''s because of the type of the expression - and thus the possible values the expression could have.

switch (some_integer * 3) {
case 3:
case 6:
case 9:
do_something();
case 10: /* can''t happen, silly! */
find_WMDs(Iraq);
}

Share this post


Link to post
Share on other sites
I'm certainly not giving an answer, but my understanding was that declared variables that are not initiated contain the value held by the memory space holding them.

That said, I'm not a C/C++ programmer, and have only played briefly with the two languages.

[edited by - botman2 on February 20, 2004 10:20:10 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by psamty10
quote:
Original post by Deyja
3) int main() {}. Main must always return an int. It doesn''t actually need a return statement though.


This last one is compiler - specific. Try building it on a compiler for an ARM device and youll drive it crazy (without a return stmt).... hehe

According to Stroustrup (TC++PL, 3.2), main() does not need to explicitly return anything (it returns 0 for you if you do not). If your compilers do not accept this, they are not proper C++ compilers.

Share this post


Link to post
Share on other sites
i think deyja was just saying that int main(){} neeeds to return an int, and it does.

what i was reffering to was the fact that my teacher said that we should always make main a function that returns void, when in fact its good practise to have main return an int.

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
so if i did

switch (a > b && c < d)

case true: blah blah
break;

case false: blah blah blah
break;


this would work?


We''ve all told you "yes", but why don''t you just test it out instead of wasting time asking us?

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
i think deyja was just saying that int main(){} neeeds to return an int, and it does.

what i was reffering to was the fact that my teacher said that we should always make main a function that returns void, when in fact its good practise to have main return an int.

It''s not "good practice" to have main() return an int. main() has to return an int. If main() doesn''t return an int, it''s not proper C++. Note, however, that as I pointed out, it does not need a return statement: If the control reaches the end of main(), the effect is the same as if there had been a ''return 0;'' at the end.

Share this post


Link to post
Share on other sites
Just a few more remarks about the switch. You can basically use any expression you want for the switch as long as it evaluates to an integral type. You can't use a float variable by itself as the expression since it's obviously not an integral type. Bool can be treated as an integral type so using that as the condition works too.





--{You fight like a dairy farmer!}

[edited by - Greatwolf on February 21, 2004 1:42:32 PM]

Share this post


Link to post
Share on other sites