Thread: c++ LOOP, HELP!

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    c++ LOOP, HELP!

    1.Write a program that will ask the user to input a hexadecimal number then convert and display the decimal equivalent of the hexadecimal number.

    The program should process the program as many times as the user wants.





    2. To make a telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. Write a program that prompts the user to enter a telephone number in digits. Allow the user to use both uppercase and lowercase letters as well as space between words.

    The program should process the program as many times as the user wants.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you done so far?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    main:
        char ch1;
        int num1;
        bool repeat;
        char yes;
    
        cout << "Enter your telephone number in letters." << endl;
        cin >> ch1;
        cout << "\n";
    
            for (int i=0; i < 7; i++)
            {
                    switch (ch1)
                    {
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'a':
                        case 'b':
                        case 'c':
                                cout << "2";
                                cin >> ch1;
                                break;
                        case 'D':
                        case 'E':
                        case 'F':
                        case 'd':
                        case 'e':
                        case 'f':
                                cout << "3";
                                cin >> ch1;
                                break;
                        case 'G':
                        case 'H':
                        case 'I':
                        case 'g':
                        case 'h':
                        case 'i':
                                cout << "4";
                                cin >> ch1;
                                break;
                        case 'J':
                        case 'K':
                        case 'L':
                        case 'j':
                        case 'k':
                        case 'l':
                                cout << "5";
                                cin >> ch1;
                                break;
                        case 'M':
                        case 'N':
                        case 'O':
                        case 'm':
                        case 'n':
                        case 'o':
                                cout << "6";
                                cin >> ch1;
                                break;
                        case 'P':
                        case 'Q':
                        case 'R':
                        case 'S':
                        case 'p':
                        case 'q':
                        case 'r':
                        case 's':
                                cout << "7";
                                cin >> ch1;
                                break;
                        case 'T':
                        case 'U':
                        case 'V':
                        case 't':
                        case 'u':
                        case 'v':
                                cout << "8";
                                cin >> ch1;
                                break;
                        case 'W':
                        case 'X':
                        case 'Y':
                        case 'Z':
                        case 'w':
                        case 'x':
                        case 'y':
                        case 'z':
                                cout << "9";
                                cin >> ch1;
                                break;
                    }
            }
    
                cout << "\n\nwould you like to repeat the operation? Y/N" << endl;
                cin >> yes;
                cout << "\n" << endl;
            if (yes == 'Y')
            {
               goto main;
            }
            else
            {
                goto end;
            }
    end:
        return 0;
    }
    I don't know how to convert from numbers to letters T_T

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of two goto statements and two labels, make use of a loop, e.g., a while loop or a do while loop.

    Quote Originally Posted by Ejing2
    I don't know how to convert from numbers to letters
    First, you need to be clear on what your program is supposed to do. You are supposed to "write a program that prompts the user to enter a telephone number in digits". Then, what output is your program supposed to have? After all, a digit corresponds to multiple possible letters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM