Phone Conversion Program

This is a discussion on Phone Conversion Program within the C++ Programming forums, part of the General Programming Boards category; Code: // Program: PhoneConvert5_p301 // Programmer: Johnathan Weidman // Date: 3/1/2010 // Description: This Program takes a stream of charactor ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Phone Conversion Program

    Code:
    // Program:		PhoneConvert5_p301
    // Programmer: Johnathan Weidman
    // Date:		3/1/2010
    // Description:	This Program takes a stream of charactor values and turns it into numerical data
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    
    
    
    
    int main()
    {
    
    	char ch;                        // creates a char called ch to hold currint letter to be changed
    	int phoneNumber[7];        // creates a array called phoneNumber with 8 items.
    	int count = 0;               // this is set up to count how many charecters are inputed
    	int n = 0;
    	char answer = 'Y';
    
                    // Thefollowoing common out produce the banner
    		cout << "**********************************" << endl; 
    		cout << "*   Programming Assignment V     *" << endl;
    		cout << "*     Computer Programming I     *" << endl;
    		cout << "*      Author: John Weidman      *" << endl;
    		cout << "*    Due Date: Monday, march 1   *" << endl;
    		cout << "**********************************" << endl;
    while ( answer == 'Y')
    {	
    	
    	
    
    	cout << "Please enter a phrase that is atlease seven letter long. \n";
    
     
    	while (count != 7) // checks to see if count is less than 8 and does the following
    	{
    	
    		
    
    		count = count++;// adds 1 to o........
    		cin.get(ch); // gets a char out of input stream
    		if (ch == ' ')
    		{
    			count = count--;
    		}
    		else // checks to make sure that the item is not blank
    		{
    			// the following check the letters and change char ch to the corisponding number
    			switch(ch)
    			{
    			case 'a':
    			case 'A':
    			case 'b':
    			case 'B':
    			case 'c':
    			case 'C':
    				ch = 2;
    				break;
    			case 'd':
    			case 'D':
    			case 'e':
    			case 'E':
    			case 'f':
    			case 'F':
    				ch = 3;
    				break;
    			case 'g':
    			case 'G':
    			case 'h':
    			case 'H':
    			case 'i':
    			case 'I':
    				ch = 4;
    				break;
    			case 'j':
    			case 'J':
    			case 'k':
    			case 'K':
    			case 'l':
    			case 'L':
    				ch = 5;
    				break;
    			case 'm':
    			case 'M':
    			case 'n':
    			case 'N':
    			case 'o':
    			case 'O':
    				ch = 6;
    				break;
    			case 'p':
    			case 'P':
    			case 'q':
    			case 'Q':
    			case 'r':
    			case 'R':
    			case 's':
    			case 'S':
    				ch = 7;
    				break;
    			
    			case 't':
    			case 'T':
    			case 'u':
    			case 'U':
    			case 'v':
    			case 'V':
    				ch = 8;
    				break;
    			
    			case 'w':
    			case 'W':
    			case 'x':
    			case 'X':
    			case 'y':
    			case 'Y':
    			case 'z':
    			case 'Z':
    				ch = 9;
    				break;	
    			}
    			phoneNumber[n] = ch; // stores the value of ch into an array position n
    			
    			n = n++; //Accumulates N to change position in aray to store ch
    		}
    		
    
    			
    		
    		
    		
    	}
           
    		// outputs array elements and puts dash in phone number
    		cout << phoneNumber[0] << phoneNumber[1] << phoneNumber[2] << "-"             <<           phoneNumber[3] << phoneNumber[4] << phoneNumber[5] <<     phoneNumber[6];
      		// Clears the current input 
    		cin.clear();
    		cout << "\nwould you like to change a phrase to a number? Enter Y for yes, and N for No\n";
    		cin  >> answer;
    		// reinitializes count and n to 0
    		count = 0;
    		n = 0;
    }
    system("pause");
    return 0;
    }
    The Program above takes an input from a user, takes out spaces, truncates to 7 letters and turns them into a phone number. (ie: call sam (225-5726). The program works the first time around. The program prompts a user to input a phrase and it outputs the right number. I have it set up to loop as long as the user inputs Y for yes. The problem is the program on the second time through outputs the 1 0 and then starts outputing the right numbers. I need it to figure out how 1 and 0 are being put into my array. I don't have any condition that would put a 1 or a 0 into my array. Thanks in advance to anyone who helps and I am sorry for any bad coding techniques as I am new and this is my first c++ class.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's a suggestion to help start getting this code under control:
    Code:
    		{
    			switch(ch)
    			{
    			case 'a':
    			case 'A':
    			case 'b':
    			case 'B':
    			case 'c':
    			case 'C':
    				ch = 2;
    				break;
    			case 'd':
    			case 'D':
    			case 'e':
    			case 'E':
    			case 'f':
    			case 'F':
    				ch = 3;
    				break;
    			case 'g':
    			case 'G':
    			case 'h':
    			case 'H':
    			case 'i':
    			case 'I':
    				ch = 4;
    				break;
    How about:

    Code:
    if ((ch >= 'a' && ch <= 'c') || (ch >= 'A' && ch <= 'C')) ch = 2;
    else if ((ch >= 'd' && ch <= 'f') || (ch >= 'D' && ch <= 'F')) ch = 3;
    else if ((ch >= 'g' && ch <= 'i') || (ch >= 'G' && ch <= 'I')) ch = 4;
    else if...and so on
    instead.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Thank you that looks much cleaner that what I have. My teacher asked me to use switch case statements (my teacher sucks).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    I would not say that your teachers sucks. That suggestion actually results in code that is more portable than what MK27 suggested. The thing is, this portability is rather wasted as you will probably not use a character set that is not ASCII based.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    After you input the "answer" there is a trailing newline in the input stream. This is picked up by the cin.get() call. Since you are not trapping the newline character like you are for the space, your code falls through to the else where you then assign this newline character to an integer value in the array (phoneNumber[0]). The newline character has an ascii value of 10. One way to deal with this would be to have an extra cin.get() call after you get the answer from the user to eat up this trailing newline.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    When I changed the case statments for the if statment I was given a bunch of errors at the end of my code.

    I am using visual Studio 2008


    here they are

    Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 83 PhoneConvert5_p301

    Error 2 error C2365: 'system' : redefinition; previous definition was 'function' c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 83 PhoneConvert5_p301

    Error 3 error C2440: 'initializing' : cannot convert from 'const char [6]' to 'int' c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 83 PhoneConvert5_p301

    Error 4 error C2059: syntax error : 'return' c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 84 PhoneConvert5_p301

    Error 5 error C2059: syntax error : '}' c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 85 PhoneConvert5_p301

    Error 6 error C2143: syntax error : missing ';' before '}' c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 85 PhoneConvert5_p301

    Error 7 error C2059: syntax error : '}' c:\users\john\documents\c++\phoneconvert5_p301\pho neconvert5_p301\phoneconvert5_p301.cpp 85 PhoneConvert5_p301

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Quote Originally Posted by hk_mp5kpdw View Post
    After you input the "answer" there is a trailing newline in the input stream. This is picked up by the cin.get() call. Since you are not trapping the newline character like you are for the space, your code falls through to the else where you then assign this newline character to an integer value in the array (phoneNumber[0]). The newline character has an ascii value of 10. One way to deal with this would be to have an extra cin.get() call after you get the answer from the user to eat up this trailing newline.
    Thank you so much, I had no Idea where the 10 was coming from.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    By the way, do not do this:
    Code:
    count = count--;
    Just write:
    Code:
    count--;
    The same goes for this:
    Code:
    n = n++;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Quote Originally Posted by laserlight View Post
    By the way, do not do this:
    Code:
    count = count--;
    Just write:
    Code:
    count--;
    The same goes for this:
    Code:
    n = n++;
    Thanks. I wrote it like that because it is easier for me to grasp (more like math). I have changed them all to how they should be.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Quote Originally Posted by MK27 View Post
    Here's a suggestion to help start getting this code under control:
    Code:
    		{
    			switch(ch)
    			{
    			case 'a':
    			case 'A':
    			case 'b':
    			case 'B':
    			case 'c':
    			case 'C':
    				ch = 2;
    				break;
    			case 'd':
    			case 'D':
    			case 'e':
    			case 'E':
    			case 'f':
    			case 'F':
    				ch = 3;
    				break;
    			case 'g':
    			case 'G':
    			case 'h':
    			case 'H':
    			case 'i':
    			case 'I':
    				ch = 4;
    				break;
    How about:

    Code:
    if ((ch >= 'a' && ch <= 'c') || (ch >= 'A' && ch <= 'C')) ch = 2;
    else if ((ch >= 'd' && ch <= 'f') || (ch >= 'D' && ch <= 'F')) ch = 3;
    else if ((ch >= 'g' && ch <= 'i') || (ch >= 'G' && ch <= 'I')) ch = 4;
    else if...and so on
    instead.
    I changed it back to case statements and the code compiles. I don't know if it is just because of visual studio. But I get the Idea. Try to make the code more manageable

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Talking

    I have the program working now. Thank you all for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-22-2009, 10:23 AM
  2. Help me with a program for my class!
    By seandugan890 in forum C Programming
    Replies: 4
    Last Post: 11-12-2009, 01:42 AM
  3. Currency Conversion Program
    By Dangerous_Dave in forum C Programming
    Replies: 2
    Last Post: 11-11-2003, 07:54 PM
  4. phone directory program
    By cdummy in forum C Programming
    Replies: 1
    Last Post: 04-13-2002, 11:23 AM
  5. Phone Class
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2002, 06:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21