cout << "Enter a phone number(Enter Q to quit): ";
cin >> a >> b >> c >> d >> e >> f >> g >> h;
In this program I'm trying to get a phone number. If the user enters a q it's supposed to exit. The program is for a programming class and the instructor wants us to not use arrays. The problem is, if the user enters a q, the program still wants the other 7 variables. I can't make 8 separate cin statements. The program won't exit when I enter a q, and I can't figure out how to fix this problem.
> How do I check one variable before inputting the others on the same line?
Read the first variable && check it to see if we should proceed && read the remaining variables.
Or: Read the first variable if we should proceed is true then read the remaining variables.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main()
{
char a,b,c,d,e,f,g,h ;
while( std::cout << "Enter a phone number(Enter Q to quit): " &&
std::cin >> a && a != 'Q' && a != 'q' &&
std::cin >> b >> c >> d >> e >> f >> g >> h )
{
// whatever
}
}
Write a program that simulates the dialing of a phone number.
A user will input an 8-place number, for example: 359-3177 (note that the hyphen is considered a digit).
The rules for entering phone numbers follow.
• 8 places
• It may have numbers, letters, or both.
• The phone number cannot begin with 555.
• The phone number cannot begin with 0.
• The hyphen must be in the 4th position.
• No other characters (@#$%^&*()_+=\|/><etc.) are allowed.
• If a letter is entered, its output will be a number (check your phone pad).
• Enter Q to Quit.
The focus of this project is supposed to be on functions. We are to use a readDials function to input the phone number, a toDigit function to convert the user input into numbers, and an acknowledgeCall function to output the number. The instructions specifically say to make 8 separate char variables and not to use an array.
He won't be able to use 'q' to quit. Because there is a chance the number has a q in it. So you may want to prompt ppl to use lower case letters when inputting a letter. After tinkering with this the code I got to work was this. I truncated it to save some space.
#include <iostream>
usingnamespace std;
char num1,num2,num3,num5,num6,num7,num8;
char num4 = '-';
void toDigit(char)
{
if (num1=='a' || num1 =='b' || num1 == 'c')
num1='2';
if (num1=='d' || num1 =='e' || num1 == 'f')
num1='3';
if (num1=='g' || num1 == 'h' || num1 == 'i')
num1='4';
if (num1=='j' || num1 == 'k' || num1 =='l')
num1='5';
if (num1=='m'||num1=='n'||num1=='o')
num1='6';
if (num1=='p'||num1=='q'||num1=='r'||num1=='s')
num1='7';
if (num1=='t'||num1=='u'||num1=='v')
num1='8';
if (num1=='w'||num1=='x'||num1=='y'||num1=='z')
num1='9';
if (num2=='a' || num2 =='b' || num2 == 'c')
num2='2';
if (num2=='d' || num2 =='e' || num2 == 'f')
num2='3';
if (num2=='g' || num2 == 'h' || num2 == 'i')
num2='4';
if (num2=='j' || num2 == 'k' || num2 =='l')
num2='5';
if (num2=='m'||num2=='n'||num2=='o')
num2='6';
if (num2=='p'||num2=='q'||num2=='r'||num2=='s')
num2='7';
if (num2=='t'||num2=='u'||num2=='v')
num2='8';
if (num2=='w'||num2=='x'||num2=='y'||num2=='z')
num2='9';
// u get the idea...
}
void readDials()
{
cin>>num1;
toDigit(num1);
cin>>num2;
toDigit(num2);
cin>>num3;
toDigit(num3);
cin>>num5;
toDigit(num5);
cin>>num6;
toDigit(num6);
cin>>num7;
toDigit(num7);
cin>>num8;
toDigit(num8);
}
void acknowledgeCall()
{
cout<<num1<<num2<<num3<<num4<<num5<<num6<<num7<<num8<<endl;
}
int main ()
{
cout<<"Please enter in the phone number."<<endl;
cout<<"When inputing letters please use lower-case"<<endl;
cout<<"Type 'Q' when you would like to quit"<<endl;
do
{
readDials();
acknowledgeCall();
cout<<"Please enter in another phone number."<<endl;
}
while (num1 !='Q');
}
wish i could of made the code shorter but couldnt use arrays, dont know why your professor doesn't want you to use them. If you are just gonna use this a tip to type in the other num rules is to highlight the info paste it a bunch of times highlight em again go to edit and do replace num1 with num whatever then do selection and replace all.