Unwanted output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crashdown47max
    New Member
    • Aug 2007
    • 2

    Unwanted output

    Hello,
    This program uses the telephone key alphabet, the user enters 7 or more chars and the program is suppose to take the first 7 char and turn them into the corresponding 7 digit telephone number, I keep getting 222-2222, the first "if "statement in my for loop. Can anybody help?

    [code=cpp]
    #include <iostream>


    using namespace std;

    int main()
    {

    char ch[7];
    int n[7];
    int i;




    cout << "Enter the letters your wish to convert to a 7 digit phone number" << endl;
    cin >> ch[0] >> ch[1] >> ch[2] >> ch[3] >> ch[4] >> ch[5] >> ch[6];



    for (i=0;i<7;i++)
    {



    if (ch[i] == 'a'||'A'||'b'|| 'B'||'c'||'C'){

    n[i]=2;}

    else if (ch[i] == 'd'||'D'||'e'|| 'E'||'f'||'F'){

    n[i]=3;}

    else if (ch[i] == 'g'||'G'||'h'|| 'H'||'i'||'I'){

    n[i]=4;}

    else if (ch[i] == 'j'||'J'||'k'|| 'K'||'l'||'L'){

    n[i]=5;}

    else if (ch[i] == 'm'||'M'||'n'|| 'N'||'o'||'O'){

    n[i]=6;}

    else if (ch[i] == 'p'||'P'||'q'|| 'Q'||'r'||'R'|| 's'||'S'){

    n[i]=7;}

    else if (ch[i] == 't'||'T'||'u'|| 'U'||'v'||'V'){

    n[i]=8;}

    else if (ch[i] == 'w'||'W'||'x'|| 'X'||'y'||'Y'|| 'z'||'Z'){

    n[i]=9;}
    else
    {cout << "error" << endl;}



    }
    cout << n[0] << n[1] << n[2] << "-" << n[3] << n[4] << n[5] << n[6] << endl;




    system ("pause");
    return 0;
    }
    [/code]
    Last edited by Banfa; Aug 22 '07, 11:42 PM. Reason: Added code tags as per posting guidelines
  • 2wycked
    New Member
    • Aug 2007
    • 14

    #2
    Originally posted by crashdown47max
    if (ch[i] == 'a'||'A'||'b'|| 'B'||'c'||'C'){
    n[i]=2;}

    You're using the logical or operator ( || ) wrong. You would want to compare
    [code=cppp]
    if( (this == that) || (this == theOther) )
    [/code]
    As it is, you are comparing if ch[i] == 'a', and then oring the result with a value that will always evaluate to true ('A'), which forces the first if statement to always be true.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by crashdown47max
      [code=cpp]
      if (ch[i] == 'a'||'A'||'b'|| 'B'||'c'||'C'){

      n[i]=2;}
      [/code]
      You can not short cut an if statement like this.

      What is does is evaluate the expression ch[i] == 'a' if it is true then it enters the code block, otherwise it evaluates the expression 'A'. Since any non-zero value is true and the only zero value character is '\0' this always evaluates to true and so the code block is entered and n[i] = 2 is evaluated.

      You should write you if statements as

      [code=cpp]
      if (ch[i] == 'a' || ch[i] == 'A' || ch[i] == 'b' || ch[i] == 'B' || ch[i] == 'c' || ch[i] == 'C'){

      n[i]=2;}
      [/code]

      Comment

      • crashdown47max
        New Member
        • Aug 2007
        • 2

        #4
        Thanks for your time and help

        Comment

        Working...