Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    487

    [C++] Switch con operatori condizionali

    Ciao ragazzi!

    int numero;

    switch (numero)
    {
    case (numero<100) :/*bla bla bla*/;break;
    case (numero>=100&&numero<200):/*bla bla bla*/;break;
    case (numero>=200&&numero<300):/*bla bla bla*/;break;
    default: MessageBox(NULL,"The number is bigger than consented input","",0);break;

    }


    HELP PLEASE!!!

  2. #2
    Non si può fare con uno switch, devi ricorrere ad una serie di if ... else if.
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    487

    Grazie MItaly

    Grazie

  4. #4
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Puoi anche fare a meno degli "if":
    codice:
    /*
    
    Isidoro Ghezzi, 2 feb 2010
    
    $ g++ --version; g++ -ansi -pedantic -Wextra -Wconversion main.cpp; ./a.out 
    i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
    Copyright (C) 2005 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    The number -5 is less than 1;
    The number 0 is less than 1;
    The number 1 is between 1 and 10;
    The number 2 is between 1 and 10;
    The number 10 is between 1 and 10;
    The number 2010 is greater than 10;
    
    */
    
    #include <iostream>
    #include <algorithm>
    
    namespace ig{
    	void TestNoSwitchAndIf (const int theSelector){
    		const char * kCStringContainer []={
    			"less than 1",
    			"between 1 and 10",
    			"greater than 10"
    		};
    		
    		const unsigned short aIndex = (theSelector >= 1) + (theSelector > 10);
    		std::cout << "The number " << theSelector <<
    		" is " << kCStringContainer [aIndex] << ";" << std::endl;
    	}
    }
    
    int main (void){
    	const int kTestContainer []={
    		-5, 0, 1, 2, 10, 2010
    	};
    	
    	std::for_each (kTestContainer,
    		kTestContainer + sizeof (kTestContainer)/sizeof (kTestContainer [0]),
    		ig::TestNoSwitchAndIf
    	);
    	return 0;
    }
    ;-)

  6. #6
    "Overkill" mi pare la parola più adatta.
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.