Homework

This home work assignment is kicking my butt. I have to create a program that converts words into phone numbers. The code I have so far does part of what it needs to do. My loop is infinite and starts the output from the second letter not the first. The program also outputs a zero at the end. I need to make the program end if the user enters #. I have created two functions, one that uses the cin.get(), and one to convert the charaters into numbers. Here is what I have so far.

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
int main()
{
    
    char tryThis;
    char quit;
    int count = 1;
    int exit;
    string tryhis;
    int temp;
        
        
        do
        {   
            cout << "enter your word or # to quit: "; 
            
            characters(tryThis);
           
            do
            { characters(tryThis);
                if (tryThis == 'Q')
                {
                    cin.clear();        
                    cout << "gameover";
                    tryThis = '\n';
                }
                else if (tryThis == 'Z')
                {
                cin.clear();
                cout << "what the heck";     
                }
            else
            {
            exit = characterToNumber(tryThis); 
            cout << exit;    
        
            if (count == 3)
                cout << "-";
        
            count ++;
   
                if(count > 7)
                    cout << "Phone number is to long";
                    }        
            }while(tryThis != '\n');
}
    while (tryThis != '#');
OK, I have a number of questions here.

Have you tried to debug your code? If you aren't confident using the debugger, you can insert cout statements that print the value of variables. Put them all around where you think there is a problem so you can see how they change, to give a clue as to what is causing the problem.

What is the the implementation of the characters function?

Why do you call the characters function in the first do loop and then again in the first statement in the inner do loop?

What is the the implementation of the characterToNumber function?

Some other observations:

I am guessing the following is a typo

 
 string tryhis;


 
characters(tryThis);


Look carefully at the count variable. Where is it set to hold a value that is meaningful for the tests that you have later in the code?

There is a typo when you increment count. It's very nearly right.

Also think carefully what you name your variables. Is exit a good name for what I suspect is the answer that you want. A variable called exit usually implies exit from the program. There is a function called exit() which does exactly that.

Variables names are important because they allow others to understand what you are trying to acheive. Well named variables can help prevent confusion.

Comments are an important part of code, use them to explain what each of you variables are (on the same line as the declaration). I always declare a variable on it's own line. The other thing I do right at the start, is to write out your design ( or at least the algorithm) as psuedocode using comments. Then go through and turn the psuedocode into real code, leaving the comments. This will give you a clearer idea of what you are doing.

We need to see the rest of your code to help further.

Hope this helps.

TheIdeasMan

Topic archived. No new replies allowed.