View Full Version: do/while structure, modulous, switch structure

C++ Learning Community > C++ Tips > do/while structure, modulous, switch structure


Title: do/while structure, modulous, switch structure
Description: a little program i made


ih8censorship - June 26, 2003 02:07 AM (GMT)
well here it is it checks to see if a number is odd or even:
CODE

#include <iostream>  /*you can have it iostream.h as normal but it will give you a non fatal "antiquated" header error*/
using namespace std; /*you need to use this if you dont include the h on the end of the header*/
long value;   /*makes a long variable which can store larger numbers than interger or int*/
int main()
{


cout<<"enter a number and i will tell you if it is odd or even"<<"\n"; /*prints instructions to the screen and makes a newline with the escape charachter \n*/
do{  /*starts DOing something (farily ovious isnt it?;) */
 cin>>value;  /*reads some keyboard input into the long variable value*/
 switch (value % 2){ /*takes the value read in and divides it by 2 then returns the "left overs"-- either a 1 or a 0  another name for % is modulous*/
  case 0:  /*checks for a condition to be true */
  cout<<"its a even number!"<<"\n"; /*outputs to the screen the results and makes a new line*/
  break;  /*breaks the switch and goes to while*/
  case 1: /*checks for a condition to be true */
  cout<<"its an odd number!"<<"\n"; /*outputs to the screen the results and makes a new line*/
  break;  /*breaks the switch and goes to while*/
}
}while (1 != 2); /*does something while 1 is NOT EQUAL to 2 (this way it goes on forever) you
can use a "sentinal value" to make the user be able to break the chain but i dident feel like including that*/

return 0; /*returns 0 to int main() i guess i dunno for sure :-/ */
}



hope this helps someone!

Dragon - June 26, 2003 05:28 PM (GMT)
using namespace std; specifies to use the standard namespace. I don't think it has anything to do with leaving the ".h" on or not, as headers with ".h" are usually C headers. Correct me if I'm wrong.

Shadow of the Moon - June 26, 2003 06:00 PM (GMT)
the return 0; returns 0 to windows or whatever called it.

myork - January 21, 2004 05:24 PM (GMT)


History of <iostream> and <iostream.h>

A correct C++ program should use <iostream>. The file <iostream.h> is provided for backwards compatability with programs written before the C++ standard was completely defined.

Originally namespace was not part of C++ and so the streams were defined in (what we now call) the global namespace. The C++ standard says that <iostream.h> must be provided for backward compatability with these old programs.


When you include <iostream> you place cin,cout etc into the standard namespace. As a consequence to access these values you need to let the compiler know where to look. This can be done in several ways:

1) Add the follwoing to the top of your source.
CODE
using namespace std;

2) Prefix all objects in the standard namespace with std::.
CODE
std::cout << "H World\n";

3) Selectively use the using directive.
CODE
using std::cout


PS. It is extremly bad manners to add using directives into a header file. It is known as polluting the namespace.



FrozenKnight - January 22, 2004 11:32 AM (GMT)
why not use?
CODE
switch (value & 1)

it works identicaly to
CODE
switch (value % 2)

just runs a bit faster, i admit it's a bit harder to read but it is a great performance enhancer.

myork - January 22, 2004 03:16 PM (GMT)
Tidied up your code and added comments. I hope you find them usefull.

A quick tip:
To make code look good on a web page remove all <tabs>.

Whitespace is your friend. Use it to help make your program more readable.

CODE

include <iostream>
         /*
          * you can have it iostream.h as normal but
          * it will give you a non fatal "antiquated"
          * header error.
          *
          * MIY Comment:
          *  That's because iostream.h is there for historical
          *  reasons to allow old programs to compile. It has
          *  officially de deprecated and therefore may be
          *  dropped with the next version of the compiler.
          *  If this happens and you upgrade your code will fail
          *  to compile.
          */

/using namespace std;
sing std::cout;
sing std::cin;
          /*
          * you need to use this if you dont include the h
          * on the end of the header
          *
          * MIY Comment.
          *  This is because iostream.h will in-affect add the
          *  stuff from the streams into the global namespace
          *  (to be compatable with older code) while the correct
          *  iostream will add the stuff into the standard (std)
          *  namespace.
          *
          * There are alternatives.
          */
ong value;
         /*
          * makes a long variable which can store larger
          * numbers than interger or int
          *
          * MIY Comment.
          *  This declares a global variable of type long.
          *  long is an integer and the standard defines the
          *  relative size of short, int and long as:
          *
          *    short <= int <= long
          *
          * The actual size is up to the compiler and values
          * can actually very.
          *
          *    To get the actual size try:
          *    cout <<"sizeof(short) = "<<sizeof(short)<<"\n"
          *    cout <<"sizeof(int)   = "<<sizeof(int)<<  "\n"
          *    cout <<"sizeof(long)  = "<<sizeof(long)<< "\n"
          *
          * On Windows you will get:
          *    sizeof(short) = 2
          *    sizeof(int)   = 4
          *    sizeof(long)  = 4
          * Thus on windows long is the same size as int.
          * PS. I will bet their are some compiler flags to
          * make long 8 bytes but I am not an MSVC expert.
          *
          * N.B. There is no type 'integer'
          */

int main()
{
   /*
    * prints instructions to the screen and makes a newline with
    * the escape charachter \n
    */
   cout<<"enter a number and i will tell you if it is odd or even"<<"\n";

   do
   {
       /*starts DOing something (farily ovious isnt it?;) */
       /*
        * MIY Comment:
        *   Actually marks the start of a do loop.
        */


       /*
        * reads some keyboard input into the long variable value
        *
        * MIY Comment:
        *   Actually it reads from the standard input.
        *   By default this is a keyboard but dont rely on this
        *   always being true, sombody could have tied a file
        *   to this stream, in that case you are reading from a file.
        *
        */
       cin >> value;

       /*
        * takes the value read in and divides it by 2 then returns
        * the "left overs"-- either a 1 or a 0  another name for %
        * is modulous
        */
       switch(value % 2)
       {
           case 0:  /*checks for a condition to be true
               /*
                * outputs to the screen the results and makes a new line
                */
               cout<<"its a even number!"<<"\n";
               break;        //breaks the switch and goes to while

           case 1: //checks for a condition to be true
               /*
                * outputs to the screen the results and makes a new line
                */
               cout<<"its an odd number!"<<"\n";
               break;        //breaks the switch and goes to while
       }
   }
   while (1 != 2);
   /*
    * does something while 1 is NOT EQUAL to 2
    * (this way it goes on forever)
    * you can use a "sentinal value" to make the user be able to break the
    * chain but i dident feel like including that
    *
    * MIY Comment:
    *    A more standard way of doing this would be:
    *    while(true)
    *    This makes it easy for the compiler to spot infinite loops.
    */

   return 0; // returns 0 to int main() i guess i dunno for sure :-/
   /*
    * MIY Comment
    *   The value returned is passed to the program that started this
    *   one. Generally the operating system.
    *
    *   A value of 0 means everything is OK.
    *   Any other value is an indication of failure.
    */
}


FrozenKnight - January 23, 2004 01:35 PM (GMT)
actualy it's the compiler / processor which supports the valibles not windows. 16 bit compilers have 2 byte ints, 32 bit compilers have 4 byte ints, and i'd guess that 64 bit compilers have 8 byte ints (havent tested this yet, i dont have a 64 bit processor to test on and even though i can convert my compielr to 64 bit i can't run 64 bit apps yet)

myork - January 23, 2004 02:54 PM (GMT)


Actually, its the compiler that defines the size of integers.

The compiler usually defaults to the size that is most effeciently implemented by the underlying hardware. But most compiler will allow you to change the size of the integer types by specifing the correct flags.

Thus a processor that only supports 16 bit ints natively can be made to support 64 bit ints. The code generated by the compiler is just a lot more convoluted as it must generate the code to simulate 64 bit addition etc.

For most people (the windows users) (or pentium users) the sizes are:
short 2 bytes
int 4 bytes
long 4 bytes

Of course this will very with hardware.


sunkiller77 - January 24, 2004 12:21 AM (GMT)
I like this code. And when you put a number that is too large for the type of interger or a letter(Short, long... not sure which one) the return is (typing an odd or even number):

QUOTE

its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
its a even number!
(so on)....





* Hosted for free by InvisionFree