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
/* 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;
}
---------------------------------------------------------------------------
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.
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;
}
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.
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;
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;
}
#include <iostream>
#include <string>
usingnamespace 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(unsignedint 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 [
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.
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.