2 Replies - 3937 Views - Last Post: 22 October 2012 - 09:16 AM Rate Topic: -----

#1 jaynajay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

Why my program won't loop

Posted 21 October 2012 - 01:58 PM

Below is my code, which works fine, with the exception of looping. I need to loop it so that the user will have the option to re-enter numbers. Thanks for the help!

#include<iostream>
#include<string>

using namespace std;


char Letter2Number(char);
int main ()
{
	char ch='0';
	char newLetters[11];
	char letters[11];
	while (ch !='#')
	{
	
	cout<<"\nThis program converts letters to their corresponding telephone digits" <<endl;
	cout<<"Enter a phone number: ";
	cin.get(letters, 11);
	

	for (int j=0; j<11;j++)
	{
		if (letters[j]==' ')
		{
			while(j<11)
			{
				letters[j]=letters[j+1];
				j++;
			}
		}	
	}
	
	
	cout<<"\nPhone Number before convert: ";
	cout << letters;

	for(int i=0; i<11; i++)
		{
			if (i<3)
			{
				newLetters[i]=Letter2Number(letters[i]);
			}

			if (i==3)
			{
				newLetters[i]='-';
			}
		
			if (i>3)
			{
				newLetters[i]=Letter2Number(letters[i-1]);
			}
		
	}
		

	cout<<"\nPhone Number after convert: " <<newLetters;
	cout<<"\nTo stop the program enter #.";	
	cin>>ch;
	
	}

	}


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

			/*case ' ':
				return '\0';*/

			default:
				return letter; // in case it is not a letter or is already a number
				break;
				
				
		}

}









Is This A Good Question/Topic? 0
  • +

Replies To: Why my program won't loop

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Why my program won't loop

Posted 22 October 2012 - 05:39 AM

You've stated a need - but no description of which loop you're talking about.

You have a loop at 21, 25, 37... Which loop do you feel isn't doing anything? You're loops are syntactically correct. They're probably running but not doing what you think they should. Have you put in breakpoints and stepped through the execution?

I have concerns about your loop starting at 21.
If your input letters array is 11 long it would be 0-10 indexes
at line 27, when j equals 10 line 27 is trying to access index j+1 which would be 11 - outside the bounds of the array. The program may be crashing here.

Try adding a console write line within the loop just to confirm it is running. Have it output the value of the variable you are incrementing for exmaple:
j=0
j=1
j=2
.....

Personally I don't care for architecture of putting everything as one long main method. The code isn't clean. You should JUST input the phone number, with no processing: That should be one method/function you call. Then another method to parse it. And a third to display it. Its never too early to form good coding habits. This would let you debug one method at a time. Confirm its good. Then stop worrying about it.


int main ()
{
   string originalNumber = GetUserInput();
   string convertedNumber = ParseUserInput(originalNumber);
   DisplayResults(originalNumber, convertedNumber);
}

string GetUserInput()
{
   //  Get the user to enter the phone number
   // and return it to the caller
}

string ParseUserInput(string somePhoneNumber)
{
    // parse the numbers 123-456-7890 and turn it into words
    // then return the words
}

Was This Post Helpful? 0
  • +
  • -

#3 jaynajay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

Re: Why my program won't loop

Posted 22 October 2012 - 09:16 AM

Thanks for the reply, and sorry about not mentioning the loop i was talking about. I would like to loop the output for the user to enter phone numbers.

Also, I would definitely look into the good code habits, as suggested by you. Thanks again.
cout<<"\nThis program converts letters to their corresponding telephone digits" <<endl;
017
    cout<<"Enter a phone number: ";

cout<<"\nPhone Number before convert: ";


cout<<"\nPhone Number after convert: " <<newLetters;
058
    cout<<"\nTo stop the program enter #.";  




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1