An interactive program using std::cin, std::cout, while loop and switch which will take from user no. 0 to 9 and print the number in characters. (eg. if user inters 0 the program shall respond You Have Entered : ZERO)

If any negative number is given program should terminate.

71 Views
#include <iostream>
#include <cstring> // used for strcpy fuction.
 
int main()
{
     //to store integer
     int num = 0;
     char numInString[10];
          
     //infinite while loop for interactivity
    while (1)
    { 
          //Ask user to enter the number between 0 and 9. Negative to exit.
        std::cout << "Please Enter an Integer between 0 and 9 : ";
          
        //Store the variable entered by the user.
        std::cin >> num;
 
          //if the variable entered is negative, break the loop.
        if (num < 0) break;
          
          //use switch for printing the character variables.
        switch (num)
        {
               case 0 : strcpy (numInString, "ZERO");
                        break;
               case 1 : strcpy (numInString, "ONE");
                        break;
               case 2 : strcpy (numInString, "TWO");
                        break;
               case 3 : strcpy (numInString, "THREE");
                        break;
               case 4 : strcpy (numInString, "FOUR");
                        break;
               case 5 : strcpy (numInString, "FIVE");
                        break;
               case 6 : strcpy (numInString, "SIX");
                        break;
               case 7 : strcpy (numInString, "SEVEN");
                        break;
               case 8 : strcpy (numInString, "EIGHT");
                        break;
               case 9 : strcpy (numInString, "NINE");
                        break;
               default : strcpy (numInString, "INVALID");
                         break;
        }
 
          //show user what they have entered.
        std::cout << "You Have Entered : " << numInString << ".\n";
    }
    return 0;
}
About the Author

I’m from manila, I currently working as a Software Engineer at a multinational company who’s known on in the field on information technology. At 2006, I’ve finished my bachelor in Computer Engineering at Lyceum of the Philippines University. Private and Unique Smart Home is one of my good project back at my college days. The only thing that triggers me to love programming is when I had my first console back at my childhood. It keeps my interest motivate by aiming to build a game but I fall

http://www.ronaldalversado.com

Very nice example of switch case!