wu :: forums
« wu :: forums - C++ problem »

Welcome, Guest. Please Login or Register.
May 18th, 2024, 6:55am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: SMQ, Grimbal, towr, ThudnBlunder, Icarus, Eigenray, william wu)
   C++ problem
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: C++ problem  (Read 569 times)
curt_cobain
Newbie
*





   


Posts: 33
C++ problem  
« on: Sep 28th, 2008, 1:06pm »
Quote Quote Modify Modify

Following code will give error
switch(i)
    {
   int x=10;
   case 1:cout<<"A";
    }
 
Following code will node give error
switch(i)
    {
   int x;
   case 1:cout<<"A";
    }
WhyHuh??//
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: C++ problem  
« Reply #1 on: Sep 28th, 2008, 1:16pm »
Quote Quote Modify Modify

If I had to guess, I would say that declaring variables outside of cases is allowed (but probably a bad habit), whereas changing them isn't.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
0.999...
Full Member
***





   


Gender: male
Posts: 156
Re: C++ problem  
« Reply #2 on: Sep 28th, 2008, 2:13pm »
Quote Quote Modify Modify

Also odd is the fact that I can easily undermine the system employed by simply doing this (which compiles in my GCC compiler):
#include <iostream>
 
using namespace std;
 
int main()
{
    int i = 6;
    switch(i) {
        int x;
        case 6:
            cout << "Hello world!" << endl;
        default: x = 1;
    }
    return 0;
}
« Last Edit: Sep 28th, 2008, 2:13pm by 0.999... » IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: C++ problem  
« Reply #3 on: Sep 29th, 2008, 12:36am »
Quote Quote Modify Modify

How is that undermining the system exactly? I can't see any reason why that shouldn't compile; and it's basically an example of Curt's second example which didn't give an error (although it'd be clearer if he had used "not" instead of "node" Wink).
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
0.999...
Full Member
***





   


Gender: male
Posts: 156
Re: C++ problem  
« Reply #4 on: Sep 29th, 2008, 4:32pm »
Quote Quote Modify Modify

Disregard that. This works:
switch(i) {
  int x;
  x = 2;
  case 6:
    x = 1;
    cout << "Hello world!" << endl;
}
So, it appears that you can't declare and initialize a new variable in the same statement, but you can do anything else with it.
 
Also interesting is that the following prints "Hello, world":
switch(i) {
  i = 2;
  case 6:
    cout << "Hello world!" << endl;
}
Which means that the variable given to the switch is evaluated only once. I declared "i" as volatile to receive the same result.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: C++ problem  
« Reply #5 on: Sep 30th, 2008, 1:13am »
Quote Quote Modify Modify

on Sep 29th, 2008, 4:32pm, 0.999... wrote:
Also interesting is that the following prints "Hello, world":
switch(i) {
  i = 2;
  case 6:
    cout << "Hello world!" << endl;
}
Which means that the variable given to the switch is evaluated only once. I declared "i" as volatile to receive the same result.
When the program encounters the switch, it immediately jump to the relevant case label, so "i = 2;" is never executed.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: C++ problem  
« Reply #6 on: Sep 30th, 2008, 5:07am »
Quote Quote Modify Modify

on Sep 30th, 2008, 1:13am, towr wrote:
When the program encounters the switch, it immediately jump to the relevant case label, so "i = 2;" is never executed.

Which is also why switch(foo) { int bar = 1; ... } is illegal: by the rules of the language the variable bar must be initialized, but it's in a location where the initialization code will never be executed -- a contradiction.
 
--SMQ
IP Logged

--SMQ

Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board