How do I check one variable before inputting the others on the same line?

1
2
	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.
I can't make 8 separate cin statements.

Not sure quite what this means. I assume there are certain restrictions on how the project is to be approached.

For example are you allowed to use a string?
What constitutes a phone number, can it include spaces or hyphens etc?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct phone
{
    int a, b, c, d, e, f ,g, h;
};

int main()
{
phone number;
unsigned index(0);
while(std::cin >> *(&(number.a) + index++)) //Using POD structs
    if (index == 8) break;                  //No undefined behavior here.
if(std::cin.good())
    std::cout << number.a << ' ' << number.b << ' ' << number.c << ' ' << 
                 number.d << ' ' << number.e << ' ' << number.f << ' ' << 
                 number.g << ' ' << number.h;
}


Last edited on
> 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
    }
}
@Chervil - These are the rules for the project:

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.
Last edited on
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.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

using namespace 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.
Last edited on
Topic archived. No new replies allowed.