1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help in Control Repetition Structure

Discussion in 'C++' started by noratiqah, Feb 23, 2011.

  1. noratiqah

    noratiqah New Member

    Joined:
    Feb 23, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    actually i have make some exercise in book c++ programming by D.S Malik page 314 Programming exercise no 5...

    i dunno how to make the repetition
    this is the long way...without using the control repetition structure

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    
    {
    char firstnum, secondnum, thirdnum, forthnum, fifthnum, sixthnum, seventhnum;
    int numA, numB, numC, numD, numE, numF, numG;
    
    cout<<"insert letters for phone number";
    cin>>firstnum>>secondnum>>thirdnum>>forthnum>>fifthnum>>sixthnum>>seventhnum;
    cout<<endl;
    
     
    switch (firstnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numA=2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numA=3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numA=4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numA=5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numA=6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numA=7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numA=8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numA=9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numA=0;
        break;
        default:
            numA=1;
            }
        switch (secondnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numB=2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numB=3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numB=4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numB=5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numB=6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numB=7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numB=8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numB=9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numB=0;
        break;
        default:
            numB=1;
        }
    switch (thirdnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numC=2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numC=3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numC=4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numC=5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numC=6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numC=7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numC=8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numC=9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numC=0;
        break;
        default:
            numC=1;
        }
    
    switch (forthnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numD=-2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numD=-3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numD=-4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numD=-5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numD=-6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numD=-7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numD=-8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numD=-9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numD=0;
        break;
        default:
            numD=1;
        }
        switch (fifthnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numE=2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numE=3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numE=4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numE=5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numE=6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numE=7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numE=8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numE=9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numE=0;
        break;
        default:
            numE=1;
        }
        switch (sixthnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numF=2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numF=3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numF=4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numF=5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numF=6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numF=7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numF=8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numF=9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numF=0;
        break;
        default:
            numF=1;
        }
        switch (seventhnum)
    {
        case 'A':
        case 'a':
        case 'B':
        case 'b':
        case 'C':
        case 'c':
            numG=2;
        break;
        case 'D':
        case 'd':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
            numG=3;
        break;
        case 'G':
        case 'g':
        case 'H':
        case 'h':
        case 'I':
        case 'i':
            numG=4;
        break;
        case 'J':
        case 'j':
        case 'K':
        case 'k':
        case 'L':
        case 'l':
            numG=5;
        break;
        case 'M':
        case 'm':
        case 'N':
        case 'n':
        case 'O':
        case 'o':
            numG=6;
        break;
        case 'P':
        case 'p':
        case 'Q':
        case 'q':
        case 'R':
        case 'r':
        case 'S':
        case 's':
            numG=7;
        break;
        case 'T':
        case 't':
        case 'U':
        case 'u':
        case 'V':
        case 'v':
            numG=8;
        break;
        case 'W':
        case 'w':
        case 'X':
        case 'x':
        case 'Y':
        case 'y':
        case 'Z':
        case 'z':
            numG=9;
        break;
        case '+':
        case '=':
        case '<':
        case '>':
        case '%':
        case '&':
            numG=0;
        break;
        default:
            numG=1;
        }
    cout<<numA<<numB<<numC<<numD<<numE<<numF<<numG<<endl;
    return 0;
    }
    please do help me i think we can use 'while' but i dunno how
     
    Last edited by a moderator: Feb 23, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Could you clarify the question please? We do not all have the same books as you. Or copy the question here (which might be better than a beginner's paraphrase).

    Or advise where it can be downloaded from, if that is possible. Found lots of hits in Google, but all the ones I followed were dead ends.
     

Share This Page