Thread: Looping???

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    Looping???

    I made a program but I want to go a little more into detail than the Prof. wants us to. How can I make this loop so after I put a letter in and the answer is given it promts me to enter another letter vs. shutting down the program. Thanks

    Code:
    //*****************************************
    //This program lets the user enter
    //a single letter and prints out the 
    //corresponding digit on the telephone.
    //*****************************************
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
       char Letter;
       int Digit;
       string message;
       
       cout << "Please enter the letter: ";
       cin >> Letter;
       
       switch( Letter )
       
       {
          case 'A':
          case 'B':
          case 'C': Digit = 2;
                    break;
          
          case 'D':
          case 'E':
          case 'F': Digit = 3;
                    break;
          
          case 'G':
          case 'H':
          case 'I': Digit = 4;
                    break;
          
          case 'J':
          case 'K':
          case 'L': Digit = 5;
                    break;
          
          case 'M':
          case 'N':
          case 'O': Digit = 6;
                    break;
         
          case 'P':
          case 'R':
          case 'S': Digit = 7;
                    break;
          
          case 'T':
          case 'U':
          case 'V': Digit = 8;
                    break;
          
          case 'W':
          case 'X':
          case 'Y': Digit = 9;
                    break;
          
          case 'Q':
          case 'Z': Digit = -1;
                    message = "There is no digit on the telephone that corresponds";
                    message += " to ";
                    break;
          
          default:
              Digit = -1;
              message = "Invalid letter, ";
              break;
       }
          
       if( Digit > 1 )
         cout << "The digit " << Digit << " corresponds to " << Letter << endl;
         
       else
         cout << message << Letter << '.' << endl;
    
     return 0;
      
    }

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You could put all of that code in main into a do while loop, after the answer is output, get a character from the user, if that character is y or Y continue loop, else end program.
    Code:
    do
    {
              char c;
              ...
              cout << "Continue (Y/N)? ";
              cin >> c;
    }
    while (c == 'y' || c == 'Y');
    return 0;
    Last edited by slaveofthenet; 06-27-2003 at 08:34 PM.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM