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
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.
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
#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');
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