Logical Error!
Oct 10, 2015 at 3:29pm UTC
Hey guys! So I executed this program and for some reason, whenever I input valid values for areacode, exchange, etc. it returns as an invalid statement and asks the user to reenter it. Somewhere along the way there's a simple logical error that needs fixing. Can any of you wonderful people find what's wrong?
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 85 86 87 88 89
#include "phone.h"
Phone::Phone():msg(10){ //intialize const data member
areacode = 999;
exchange = 999;
line = 9999;
}
Phone::Phone(int ac, int ex, int l, int mval):msg(mval){ //initialize const data member
areacode = ac;
exchange = ex;
line = l;
}
bool Phone::setAreaCode(int ac){
bool validate = false ;
if (ac >= 0 && ac <= 999){
areacode = ac;
bool validate = true ;
}
return (validate);
}
bool Phone::setExchange(int ex){
bool validate = false ;
if (ex >= 0 && ex <= 999){
exchange = ex;
bool validate = true ;
}
return (validate);
}
bool Phone::setLine(int l){
bool validate = false ;
if (l >= 0 && l <= 9999){
line = l;
bool validate = true ;
}
return (validate);
}
void Phone::input(){
char slash;
int ac, ex, l;
do {
cout << "Please input your phone number EX: (123) 456 - 7890." << endl;
cin >> slash >> ac >> slash >> ex >> slash >> l;
} while (!setAreaCode(ac) && !setExchange(ex) && !setLine(l));
}
void Phone::display (){
cout << "The maximum number text messages you have are: " << msg << endl;
cout << "(" << getAreaCode() << ") " << getExchange() << " - " << getLine() << endl;
}
int Phone::getAreaCode(){
return areacode;
}
int Phone::getExchange(){
return exchange;
}
int Phone::getLine(){
return line;
}
Oct 10, 2015 at 3:56pm UTC
1 2 3 4 5 6
bool validate = false ;
if (ac >= 0 && ac <= 999){
areacode = ac;
bool validate = true ; //here you declare _another_ variable
}
Oct 10, 2015 at 4:23pm UTC
I'm sorry, but when you say to declare another variable, what do you mean?
Oct 10, 2015 at 5:10pm UTC
1 2 3 4 5 6 7 8
bool Phone::setAreaCode(int ac){
bool validate = false ; // (Declared as a bool type)
if (ac >= 0 && ac <= 999){
areacode = ac;
bool validate = true ; // (Declared as a bool type)
}
When you declare a variable, you name the type, bool.
after that you only need to name the variable. So it would become
1 2 3 4 5 6 7 8
bool Phone::setAreaCode(int ac){
bool validate = false ; // (Declared as a bool type)
if (ac >= 0 && ac <= 999){
areacode = ac;
validate = true ; // reassigned variable (no declaration)
}
Oct 11, 2015 at 1:49am UTC
Thank you so much guys! Appreciate all the help!
Topic archived. No new replies allowed.