View Full Version : Quick C++ Question?
Brian
12-13-2002, 06:56 PM
Why is this code outputting "bad output" no matter what values I enter into the string array? I tried it as both a string and as a char array and still have the same problem. It's like my if statement isn't getting checked at all. I'm trying to make a program that converts phone numbers based on some criteria.
int main(){
// Declaration and initialization
string PhoneArray;
//Input and output section
cout << "Enter a phone number to convert using the format 303-xxx-xxx "<<endl;
cin>>PhoneArray;
if ((PhoneArray[5]==1)||(PhoneArray[5]==2)||(PhoneArray[5]==3)||(PhoneArray[5]==4)||(PhoneArray[5]==5))
cout<<PhoneArray<<endl;
else
cout<<"bad input"<<endl;
return 0;
}
Genesis
12-13-2002, 08:31 PM
I am not sure if this is relevant, but what headers are you using? do you have the whole code posted ir is this a piece of a puzzle?
Edit: jist noticed your phone number format is printed as 303-xxx-xxx. you are missing one digit.
what is the [5] after phonearray in the if statement for?
davidm_sh
12-14-2002, 09:10 AM
The problem is you are trying to compare a number to a character in the if statement. When you say:
PhoneArray[5]==1
You probably want to check to see if the 5th character is a 1 correct? If that is the case you need somthing like:
PhoneArray[5]=='1'
You want to compare char to char not char to integer.
HTH
Brian
12-14-2002, 09:42 AM
ahhh man you are completely correct. I knew it was some little detail, but I couldn't pit my finger on it.
Thanks
Brian
12-14-2002, 11:00 AM
How do I remove numbers from the front of a string? I need to remove the area code in the phone number and use strcat to place a new area code on the front. The strcat part is easy, but I can't think of an effective way to remove the area code on the first string.
int main(){
// Declaration and initialization
char PhoneArray[15];
char Area1[6] = "(720)";
//Input and output section
cout << "Enter a phone number to convert using the format 303-xxx-xxx "<<endl;
cin>>PhoneArray;
if ((PhoneArray[4]=='1')||(PhoneArray[4]=='2')||(PhoneArray[4]=='3')||(PhoneArray[4]=='4')||(PhoneArray[4]=='5'))
cout<<PhoneArray<<endl;
else
strcat(PhoneArray, Area1);
cout<<PhoneArray<<endl;
return 0;
}
davidm_sh
12-14-2002, 11:42 AM
The easiest thing I could think of would simply be to have the user input a string called "tempPhoneNumber" or somthing. Then just create a new string with a concatinatino of the desired new area code and substring of the temPhoneNumber.
I know in Java Strings are immutable which means you cannot really modify them at all. You can modify them in Java but all it's doing is creating a new STring object with the changes you want.
I am not sure how C++ handles that. Either way though it should work like I described above.
OR why not just do somthing like:
PhoneArray[0] = Area1[0]
...
PhoneArray[5] = Area1[5]
That would give you what you want as well. Just reassign the characters from the original phone number to the area1 characters?
Just some thoughts.
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.