string skip

Hi there , may you please help me almost going crazy . cant get why my code is truncating part of the output which are numbers . its just selecting the first number only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
     case 'V':cout << "8" << endl;
               break;
               case 'W':
               case 'X':
               case 'Y':
               case 'Z':cout << "9" << endl;
               break;
               default:
               cout << "*" << endl;
               break ;
               // complete the loop
               // display digit corresponding to letter

            }
            //skip letters more than 7
            string skip;
            getline(cin, skip, '\n');
            cout << endl;

        }

        // prompt user to input another number to convert
        cout << "\nWould you like to convert another number? Indicate by Y - yes or N - no"<< endl;
        cin >> answer;

        }//stop when user no longer wants to convert phone numbers
        while (toupper(answer) != 'N');

        cout << "Goodbye!" << endl;
return 0;
}
It's not clear from this which value is being truncated. But at a guess, the mixing of cin >> with getline() may be causing problems.

After the user enters the value here cin >> answer; there will be a newline character '\n' remaining in the input buffer. The next getline() will read everything up to the newline, resulting in an empty string.
The usual solution is to put cin.ignore(); after the cin>> answer; in order to remove the unwanted newline.

Or I may have missed the point completely :)
Last edited on
the switch statement is supposed to display the digit corresponding to the letter eg ABC should be 222 but its just outputting 2 only and its supposed to ask if you want to convert another number but its only doing that after 7 inputs
Last edited on
Well, I don't think there is enough to be gleaned from that code fragment to tell what is going on.
here is the full program

#include <iostream>
#include <cstdlib>
using namespace std;
const int NUMBERLETTERS = 7;
int main()
{
char letter, answer;
cout << "This program displays phone numbers expressed as";
cout << " letters in digital format";

// loop while user wants to convert phone numbers
do
{
cout << "Please enter the phone number in CAPITAL LETTERS: ";
// prompt for telephone number in capitals

// loop to input 7 letters for phone number
for (int i = 1 ;i <= NUMBERLETTERS ; i++ )
{
cin >> letter;
// print space between third and fourth digit
if (i == 3)
cout << " ";

switch(letter)
{
case 'A':
case 'B':
case 'C':cout << "2" << endl;
break;
case 'D':
case 'E':
case 'F':cout << "3" << endl;
break;
case 'G':
case 'H':
case 'I':cout << "4" << endl;
break;
case 'J':
case 'K':
case 'L':cout << "5" << endl;
break;
case 'M':
case 'N':
case 'O':cout << "5" << endl;
break;
case 'P':
case 'Q':
case 'R':
case 'S':cout << "7" << endl;
break;
case 'T':
case 'U':
case 'V':cout << "8" << endl;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':cout << "9" << endl;
break;
default:
cout << "*" << endl;
break ;
// complete the loop
// display digit corresponding to letter

}
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;

}

// prompt user to input another number to convert
cout << "\nWould you like to convert another number? Indicate by Y - yes or N - no"<< endl;
cin >> answer;

}//stop when user no longer wants to convert phone numbers
while (toupper(answer) != 'N');

cout << "Goodbye!" << endl;
return 0;
}
This part:
1
2
3
4
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;

is currently inside the for-loop. I think it should be placed after the end of the loop.

The comment: // complete the loop is at the end of the switch-case statement, not the end of the loop. Proper indentation helps to make that clear.
Last edited on
There is a break and return 0 ; after the loop , aren't these supposed to complete the loop
Last edited on
Here is the corrected code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
               case 'W':
               case 'X':
               case 'Y':
               case 'Z':cout << "9" << endl;
               break;
               default:
               cout << "*" << endl;
               break ;
               // complete the loop
               // display digit corresponding to letter

            } // ---- end of switch ----


        } //  ---- end of for ----
        
        //skip letters more than 7
        string skip;
        getline(cin, skip, '\n');
        cout << endl;        

        // prompt user to input another number to convert
        cout << "\nWould you like to convert another number? Indicate by Y - yes or N - no"<< endl;
        cin >> answer;

    } //---- end of do-while ---- 
    //stop when user no longer wants to convert phone numbers
    while (toupper(answer) != 'N');

        cout << "Goodbye!" << endl;
    return 0;
} // ---- end of main ---- 

I added comments // ---- end ofxxxxx ---- to make the context clearer, though the indentation should show that in the full code.
wow ! Its amazing what improper placing of brackets can do to damage a program . Thank you so much Chervil , i will always make use of proper indentation from today on wards . It runs perfectly . Now let me tackle variable diagrams
Last edited on
Topic archived. No new replies allowed.