Noob needs help with homework

Howdy,

So after 3 days of rewriting this over and over. I decided to ask for help...

This is the assignment, I realize rarely do coders just give out answers, but I am hoping someone can point me in the right direction of what I am doing wrong.

I am thinking I need the FOR function, could be wrong. Not sure how to implement it, or even sure if it's what I am needing. Remember this is a 101 class, the farthest we have gone is loops.

This assignment introduces you to C++ repetition statement:
HattiesburgTech asks you to write a program to help its employees remember the company’s phone numbers easily using letter. For instance, the phone number 438-5626 can be shown as GET LOAN. There are also instances where HattiesburgTech may use more than 7 letters to represent a phone. For instance, 225-5466 can be shown as CALL HOME, which uses 8 letter. Your job, as they described it, is to write a program that prompts the user to enter a telephone number in letters and output the corresponding number in digits. If the user enters more than 7 letters, then process only the first 7. You should also output the hyphen after the third digit. Your program should allow the user to enter the letters in any case (upper or lower) and should allow spaces between words. Also make your program process as many numbers as the user desires.
A simple look-and-feel for the sample output is provided below.
********************************
Enter Y/y to convert a phone number from letters to digits (enter any other letter to quit): Y
Enter a number using letter: CALL HOME
The corresponding number is 225-5466
Enter Y/y to convert a phone number from letters to digits (enter any other letter to quit): Y
Enter a number using letter: gEt Loan
The corresponding number is 438-5626
Enter Y/y to convert a phone number from letters to digits (enter any other letter to quit): Y
Enter a number using letter: A

--------------------------------------------------------------------------

This is what I have so far...

/* File Name: Programming_Assignment_4_Booker
Programmer: Lee M. Booker
Course: C++ Programming (CSC 101)
Instructor: Isaac Gang
Institution: Southern Miss
Problem: Write a C++ selection statement
Due Date: Friday July 8th 2011 */

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
//define variables
int numbers;
int counter;
char startProgram;
string letters;

//get data from user

cout <<"Enter Y/y to convert a phone number from letters to digits (enter any other letter to quit): "<<endl;
cin >> startProgram;
{
if (startProgram == 'y' || startProgram == 'Y')
cout <<"Enter a number using letter, or enter # to quit: "<<endl;
getline (cin,letters);
cout << endl;


for (counter=0,counter<8,counter++)
switch (letters[8])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << 2 <<endl;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << 3 <<endl;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << 4 <<endl;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << 5 <<endl;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << 6 <<endl;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << 7 <<endl;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << 8 <<endl;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << 9 <<endl;
break;
default:
cout <<"Now quitting..."<<endl;
}
else if (startProgram != 'y' || startProgram != 'Y')
cout <<"You did not enter Y or y, try again." <<endl;
}
counter++;
}

system ("pause");
return 0;
}
---------------------------------------------------------------------------

Any help would be appreciated...

In a for statement, the bit in the brackets is separated by semi-colons, not commas.

http://www.cplusplus.com/doc/tutorial/control/

You can't have an else without a preceding if; the else you're using doesn't have a preceding if because you've not used braces to wrap the contents of the if block.

Here's the output from clang++

178.cpp:34:35: error: expected ';' in 'for' statement specifier
for (counter=0,counter<8,counter++)
                                  ^
178.cpp:110:1: error: expected expression
}
^
178.cpp:112:1: error: expected expression
}
^
178.cpp:112:1: error: expected ')'
178.cpp:34:5: note: to match this '('
for (counter=0,counter<8,counter++)
    ^
178.cpp:34:23: warning: expression result unused [-Wunused-value]
for (counter=0,counter<8,counter++)
               ~~~~~~~^~
178.cpp:116:2: error: expected '}'
}
 ^
7 diagnostics generated.


Your compiler should have produced something similar you can use to guide you to the errors.


switch (letters[8])

So that handles the (non-existent) ninth letter. What about the first seven letters?

When you use a for statement, everything you want to be repeated needs to be inside braces.
Last edited on
Thanks, I am going to give it another run with these suggestions. Will let you know...
Hey, check out the switch (letters[8]) and the for()


What does the for loop do with the variable counter.
Whats the point of the counter variable if you're not using it.


Hope those hints help you.
Last edited on
The counter variable is "supposed" to be moving to the next line, but as you can see, I do not understand where to place it. So see if this code looks closer...

The concept I am obviously having trouble understanding, is how to read a string, then process it character by character. Am I using the wrong statements here? Should I be using if else nested instead? Anyway, here ya go...

I added switch (letters[x]) because I kept getting this error: error: switch quantity not an integer

I am only 4 weeks into this class, I am starting to get a better grasp on compiler errors.

So if I don't use a counter, how will I make this program move to the next character and not the same one over and over? Should I be using if...else statements instead of switch?

Here's my revision, still not correct, but it compiled...


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
//define variables
int numbers;
int counter;
char startProgram;
string letters;

//get data from user

cout <<"Enter Y/y to convert a phone number from letters to digits (enter any other letter to quit): "<<endl;
cin >> startProgram;
cout <<endl;

if (startProgram == 'y' || startProgram == 'Y') //start the program
cout <<"Enter a number using letter, or enter # to quit: "<<endl;
getline (cin,letters);
cout << endl;


{
for (counter=0;counter<8;counter++)
letters = 1;
switch(letters[7])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << 2 <<endl;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << 3 <<endl;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << 4 <<endl;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << 5 <<endl;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << 6 <<endl;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << 7 <<endl;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << 8 <<endl;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << 9 <<endl;
break;
default:
cout <<"Now quitting..."<<endl;
}
}
system ("pause");
return 0;
}

Last edited on
http://www.cplusplus.com/doc/tutorial/arrays/

Basically the same.

#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	const int SIZE = 5;
	int numberArray[5] = {1, 2, 3, 4, 5};

	// Address
	cout << numberArray << endl << endl;

	// Prints each element of the array
	for(int counter = 0; counter  < SIZE; counter++)
		cout << numberArray[counter] << endl;

	return 0;
}


Output:
002CF7FC

1
2
3
4
5
Press any key to continue . . .


Another example
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
#include <iostream>
using namespace std;

int main()
{
	const int SIZE = 5;
	int numberArray[5] = {1, 2, 3, 4, 5};

	// Address
	cout << numberArray << endl << endl;

	// Prints each element of the array
	for(int counter = 0; counter < SIZE; counter++)
	{
		switch(numberArray[counter])
		{
		case 1:
			cout << "One\n";
			break;
		case 2:
			cout << "Two\n";
			break;
		case 3:
			cout << "Three\n";
			break;
		case 4:
			cout << "Four\n";
			break;
		case 5:
			cout << "Five\n";
			break;
		default:
			cout << "Default\n";
			break;
		}
	}
	return 0;
}


0024FCE0

One
Two
Three
Four
Five
Press any key to continue . . .
Last edited on
THANK YOU!

This example helps greatly...
ok, between here and a classmate, I think I have put this all together. However, I still have one problem, and I would like a critique on the code, especially around these lines.

length = letters;
maxLength = length.length();
if(maxLength > 8)
{
maxLength = 7;
}

The only issue I am having now is the space. As long as the string is input without it, it runs fine. Otherwise, it throws an error. Isn't there an ignore function that will ignore spaces? I cant find an example that fits my needs.

/* File Name: Programming_Assignment_4_Booker
Programmer: Lee M. Booker
Course: C++ Programming (CSC 101)
Instructor: Isaac Gang
Institution: Southern Miss
Problem: Write a C++ selection statement
Due Date: Friday July 8th 2011 */

#include <iostream>
#include <string>
using namespace std;
int main()
{
char startProgram;
char letters[256];
string length;
int maxLength;
bool run = true;

while(run=true)
{
cout <<"\nEnter Y or y to convert phone number from letters to numbers \n(enter any other letter to quit)"<<endl;
cin >> startProgram;

if(startProgram=='Y'|| startProgram=='y')
{
cout << "\nEnter the letters you would like your phone number to represent: "<<endl;
cin.get();
cin.getline(letters,300);
cout << "\nThe corresponding number is: " << " "<<endl;
cout << endl;

length = letters;
maxLength = length.length();
if(maxLength > 8)
{
maxLength = 7;
}

for(int i=0; i < maxLength ; i++)
{
switch(letters[i])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << 9;
break;
case ' ':
cout << " ";
default:
cout <<"Now quitting..."<<endl;
break;
}
if(i==2)
cout<<"-";}
}
else return 1;
}

system("pause");
return 0;
}
Your case statement that deals with space:

case ' ':

does not have a break; after it, so it flows into the default: case.
Thanks for the advice, fixed it...still having space problems..

case ' ':
cout <<"";
break;
default:
cout <<"Now quitting..."<<endl;
break;

fairly sure this is the problem..

if(i==2)
cout<<"-";}


Any advice?
Last edited on
Hey Lee, I modified your code and commented the changes. Check it out and tell me if you have another other doubts.

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include <iostream>
#include <string>
using namespace std;
int main()
{
    char startProgram;
    string letters;		// Changed from char[] to string
    string length;		// No need for this variable
    int maxLength;		// No need for this variable
    bool run = true;	// No need for this variable, check comment below

    while(true) // This while(run = true) is incorrect, it will always be true, change to while(run) or while(true)
    {
        cout <<"\nEnter Y or y to convert phone number from letters to numbers \n(enter any other letter to quit)"<<endl;
        cin >> startProgram;

		/* I added this lines of code
		 When using (cin >>) and getline together, after avery (cin >>) 
		 the NULL character or '\n' (enter) is stored in the buffer,
		 cin.ignore() clears the input buffer */
		cin.ignore(10000,'\n');

        if(startProgram=='Y'|| startProgram=='y')
        {
            cout << "\nEnter the letters you would like your phone number to represent: "<<endl;
            // This cin.getline(letters,300); changes to
			getline(cin, letters);

			// I removed the couple of endl you had here so the output looks better
            cout << "\nThe corresponding number is: ";

			// No need for these operations:
            //length = letters;
            //maxLength = length.length();


			// This doesnt make sense:
			//if(maxLength > 8)
            //{
                //maxLength = 7;
            //}

			for(unsigned int i=0; i <= 7 ; i++) // You could just set it to <= 7 or < 8
			// There is a function from the string class that gives you the size letters.length()
            {
				// Put this if BEFORE the switch statement, if its AFTER, 
				// it will first do the operations on the switch and then check
				// for this condition

				// Its if i == 3, not 2
                if(i==3)
                    cout<<'-';  // If you are only outputting a single character use single quotes

                switch(letters[i])
                {
                case 'A':
                case 'a':
                case 'B':
                case 'b':
                case 'C':
                case 'c':
                    cout << 2;
                    break;
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'F':
                case 'f':
                    cout << 3;
                    break;
                case 'G':
                case 'g':
                case 'H':
                case 'h':
                case 'I':
                case 'i':
                    cout << 4;
                    break;
                case 'J':
                case 'j':
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                    cout << 5;
                    break;
                case 'M':
                case 'm':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                    cout << 6;
                    break;
                case 'P':
                case 'p':
                case 'Q':
                case 'q':
                case 'R':
                case 'r':
                case 'S':
                case 's':
                    cout << 7;
                    break;
                case 'T':
                case 't':
                case 'U':
                case 'u':
                case 'V':
                case 'v':
                    cout << 8;
                    break;
                case 'W':
                case 'w':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'Z':
                case 'z':
                    cout << 9;
                    break;

				// Removed the case ' '
                //case ' ':
                    //cout << " ";
                default:
					// Just do a break here
                    //cout <<"Now quitting..."<<endl;
                    break;
                }
			
            }
        }
        else 
			// Changed the return 1 to break so it gets out of the while loop
			break;
    }
	cout << "Now quitting..." << endl;
    //system("pause");
    return 0;
}


Also, when posting your code put [(code] Before the start of the code and [(/code] and the end.
Without the ( inside the [
Last edited on
Unfortunately, I chose to take my first programming course during the summer, which cuts it from 16 weeks to ten. On top of that, when several classmates brought this problem to the attention of the teacher (the entire class was having issues), he couldn't give us an answer. He inherited the course from the previous professor and hasn't looked over the assignments he is giving out...

I cannot thank you enough Dukaim.

There were several issues that we haven't covered yet, and reading your code not only answered my questions, but several others.

According to the C++ professor, he is quitting after this semester to go teach at a junior college. Need a job? Lol...

The simplicity of your answer struck me like lightening. At this point, I am building code kind of like I work on a car, just keep hitting it with something until it either works, or breaks completely. From now on I will be more reluctant to assign variables when it can be handled in the formulas.

Thanks again!
Hi Lee Booker, professor teaching is a job just like a developer. Very often we inherit legacy stuff left by the predecessor. The legacy stuff can be a *gem* or *BS*. And it depends a lot on the current owner to clean-up the legacy stuff or just let it be. If it ain't broke why fix it correct ? :P

We are treading on human character and morals here. Nothing technical and I guess it applies to all professions not limited to teaching and software development :)

But somehow I feel ppl who are in teaching profession should not only consider the pay-check but look at the in-tangibles of you imparting precious knowledge to new generation of students who are going to power the country economy in a few years time. This is a "sacred duty" and it should not be tied to just monetary gains isn't it ? For this I remain a software developer than a teacher :)

In summary, teaching as a career involves not only monetaries but also the sacred duty you are going to undertake. The same goes for nurses and doctors. Lawyers err.... next topic please.
Topic archived. No new replies allowed.