Thread: converting a char to a numeric number

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    37

    converting a char to a numeric number

    having trouble detecting where char d1-8 are converted to a numeric number
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    //Prototypes
    int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8);
    int ToDigit (char &d);
    void acknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);
    char d = 1;
    
    using namespace std;
    
    
     int main()
     {
        char d1, d2, d3, d4, d5, d6, d7, d8;
        int ReturnValue = 0;
    
        while (ReturnValue != -5)
        {
            ReturnValue = ReadDials(d1,d2,d3,d4,d5,d6,d7,d8);
            switch(ReturnValue)
            {
            case -1: cout <<"ERROR- An invalid character was entered." << endl; break;
            case -2: cout <<"ERROR- Phone number can't begin with 0." << endl; break;
            case -3: cout <<"ERROR- Phone number can't begin with 555." << endl; break;
            case -4: cout <<"ERROR- Hyphen is not in the correct posistion." << endl; break;
            default: acknowledgeCall(d1,d2,d3,d4,d5,d6,d7,d8);
            }
        }
    
     }
    
     int ToDigit (char & d)
    
    {
        toupper(d);
    
            switch(d)
            {
            case 'A': case 'B': case 'C':
                d = '2'; return 0; break;
            case 'D': case 'E': case'F':
                d = '3'; return 0; break;
            case 'G': case 'H': case 'I':
                d = '4'; return 0; break;
            case 'J': case 'K': case 'L':
                d = '5'; return 0; break;
            case 'M': case 'N': case 'O':
                d = '6'; return 0; break;
            case 'P': case 'Q': case 'R': case 'S':
                d = '7'; return 0; break;
            case 'T': case 'U': case 'V':
                d = '8'; return 0; break;
            case 'W': case 'X': case 'Y': case 'Z':
                d = '9'; return 0; break;
            }
        }
    
    
            void acknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8)
            {
                        cout << "Phone Number Dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8;
            }
    
     int ReadDials (char & d1, char & d2, char & d3, char & d4, char & d5, char & d6, char & d7, char & d8)
    {
        cout << "Enter a phone number: ";
        cin >> d1;
        if (d1 == 'Q' || d1 == 'q')
            return -5;
            cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
        int result = ToDigit(d);
            ToDigit(d);
        if (d == 0)
            return -2;
        if (d1 == 5 && d2 == 5 && d3 == 5)
        return -3;
        if (d4 != '-')
        return -4;
        if (result == -1)
            return -1;
        else
            return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    In your "ToDigit" function, there should only be one "return 0" and it's not in the switch statement.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Oh actually ... I'm stupid it's not that. Disregard my last message. I'm still pretty new to the whole thing.

    Code:
    int ReadDials (char & d1, char & d2, char & d3, char & d4, char & d5, char & d6, char & d7, char & d8){
        cout << "Enter a phone number: ";
    
        cin >> d1;
        if (d1 == 'Q' || d1 == 'q')
            return -5;
            cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
        
        //in this area right here it should actually be something like this
        //int result = ToDigit(d1);
          //if(result == -1)
        //  return -1;
        //after that, you have to do the same thing to each char variable except for d4
        
    
        if (d1 == 0)
    
            return -2;
    
        if (d1 == 5 && d2 == 5 && d3 == 5)
    
            return -3;
    
        if (d4 != '-')
        return -4;
    
        if (result == -1)
    
            return -1;
    
        //leave this here
    
        return 0;
    
     
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char d1, d2, d3, d4, d5, d6, d7, d8;
    When you start putting numeric suffixes on your variables, it's a sure sign you need an array and a for loop to deal with it.

    > toupper(d);
    I'm pretty sure toupper does NOT have a pass-by-reference interface.
    d = toupper(d);

    > int result = ToDigit(d);
    > ToDigit(d);
    You don't have a d, you have d1 to d8
    You just have a global called d just to make this compile.
    Also, why call it twice?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Numeric Characters to Hex
    By Pharrox in forum C++ Programming
    Replies: 13
    Last Post: 11-20-2008, 10:55 AM
  2. Replies: 6
    Last Post: 07-15-2008, 02:27 PM
  3. numeric header - number of digits
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2006, 12:02 PM
  4. C++: Converting Numeric String to Alpha String
    By JosephCardsFan in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:07 AM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM

Tags for this Thread