no I am having trouble understanding what eylsia meant by having the main function defined in two or more sources.
no I am having trouble understanding what eylsia meant by having the main function defined in two or more sources.
actually I figured it out, I created a second file to test and had to remove it, now I can compile but for some reason it doesn't seem to be properly reading the numbers and relaying to my switch with the errors.
when I enter the number 555 it is supposed to give back an error from the main function switch that says you can't have a number that starts with 555 but instead all it does is sit there blinking and now responding. Not sure what in my code is wrong because in theory I thought everything looked good. not to mention if I type in a 8 character value like 565-2311 it says hyphen is in wrong position which is from my error switch and that isnt true. I don't know what I did wrong here.
Last edited by zonen; 09-23-2011 at 06:10 PM.
matze when you made the changes you were talking about did it actually compile and do what it is supposed to do? Mine never actually properly read the numbers and through out errors like I needed it too. I am still stuck on what I messed up that it isnt doing right.
Thanks for all the updates however it really isn't necessary, you could have just edited your last post instead of bumping your own thread. You need to post your updated code.
here is my updated code with issues still:
Code:#include <iostream> #include <string> #include <iomanip> #include <cctype> //Prototypes int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); int ToDigit (char &d); void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8); using namespace std; int main() { char d1, d2, d3, d4, d5, d6, d7, d8; int ReturnValue = 0; while (ReturnValue != -5) { ReturnValue = ReadDials(d1,d2,d3,d4,d5,d6,d7,d8); switch(ReturnValue) { case -1: cout <<"ERROR- An invalid character was entered." << endl; break; case -2: cout <<"ERROR- Phone number can't begin with 0." << endl; break; case -3: cout <<"ERROR- Phone number can't begin with 555." << endl; break; case -4: cout <<"ERROR- Hyphen is not in the correct posistion." << endl; break; default: AcknowledgeCall(d1,d2,d3,d4,d5,d6,d7,d8); } } return 0; } int ReadDials (char & d1, char & d2, char & d3, char & d4, char & d5, char & d6, char & d7, char & d8) { char d = 0; cout << "Enter a phone number or Q to quit: "; cin >> d1; if (d1 == 'Q' || d1 == 'q') return -5; cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8; int result = ToDigit(d); if (d1 == 0) return -2; if (d1 == 5 && d2 == 5 && d3 == 5) return -3; if (d4 != '-') return -4; if (result == -1) return -1; else return 0; } int ToDigit (char & d) { toupper(d); switch(d) { case 'A': case 'B': case 'C': d = 2; break; case 'D': case 'E': case'F': d = 3; break; case 'G': case 'H': case 'I': d = 4; break; case 'J': case 'K': case 'L': d = 5; break; case 'M': case 'N': case 'O': d = 6; break; case 'P': case 'Q': case 'R': case 'S': d = 7; break; case 'T': case 'U': case 'V': d = 8; break; case 'W': case 'X': case 'Y': case 'Z': d = '9'; break; } return 0; } void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8) { cout << "Phone Number Dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8; }
You only call your Todigit function once, and that is with a d you declared in the calling function. No where in your code are you actually converting anything from your input. What exactly do the requirements say?
I am supposed to be able to enter any form of a phone number for example 459-5647 should return that exactly, however if I ahve a 555-4513 it should bring back an error stating that you cant have a number with 555 as well as if you start with a 0 then there should be an error. You also need to be able to type letters so
abc-defg should come out as 222-3334 when ToDigit converts it.
Ok, well you aren't really doing any of that. Can you use a char array? Do you know of the ctype library and the functions isdigit and isalpha? Those would be great starting points.
I know that isalpha is supposed to check for the case I believe
I dont think I can use arrays yet though.
I think I finally understand why your saying that my program isnt doing what it should be doing. How about if I do my ReadDials function like this:
Code:int ReadDial (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8) { int returnValue = 0; cout << "Enter a phone number (Q to quit): "; cin >> d1; if(d1 == 'Q' || d1 == 'q') return -5; cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8; returnValue = Todigit(d1); if (returnValue == -1) return returnValue; returnValue = Todigit(d2); if (returnValue == -1) return returnValue; returnValue = Todigit(d3); if (returnValue == -1) return returnValue; if (d4 != '-') return -4; returnValue = Todigit(d5); if (returnValue == -1) return returnValue; returnValue = Todigit(d6); if (returnValue == -1) return returnValue; returnValue = Todigit(d7); if (returnValue == -1) return returnValue; returnValue = Todigit(d8); if (returnValue == -1) return returnValue; if (d1 == '0') return -2; if (d1 == '5' && d2 == '5' && d3 == '5') return -3; return 0; }
Are you sure? This seems like an array problem to me. Did you cover them in class yet? Does the hw assignment say a "grouping" of chars or any derivation. This assigment basically flows:
- Get input from user in format xxx-xxxx
- iterate through input:
- if first num is 0 then error else if alpha convert to number and store
- continue iteration if - located any position besides 4, error. Else if alpha convert number and store
- if first 3 numbers are 5 then error
- output converted number.
ya I just looked, says no arrays, but does that code above look right?