|
-
Mar 14th, 2002, 12:57 PM
#1
!!help!!!!!
i need to know how i would have an array of letters/numbers, and when i read it, if it finds a letter, changer it to a number, heres what i have... the ****** is where i need help.
#include <iostream.h>
#include<apvector.h>
char number[25];
char NewNumber[25];
int main()
{
int i;
cout<<"Enter the phone number ==>: ";
cin.getline(number,80);
for(i=0;i<26;i++)
*********find if letter, make it a number i.e. a,b,c, = 2**********
cout<<number[i];
cout<<"\nThe original number is : "<<number<<"\n";
return 0;
}
thanks
-
Mar 14th, 2002, 01:55 PM
#2
Frenzied Member
Something like this would work:
I am hand writing this without checking it
PHP Code:
for(i=0;i<26;i++)
{
switch(number[i])
{
case 'a':
case 'b':
case 'c':
number[i] = '2';
break;
case 'd':
case 'e':
case 'f:
number[i] = '3';
break;
//.......I think you can take it from here
}
}
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Mar 15th, 2002, 08:39 AM
#3
more memory, but less code and faster execution would be a lookup table (see the code below)
Then you can index into the field with the uppercase letter - 'A' or the lowercase letter - 'a':
Before I list my code, here are some errors of yours:
char number[25];
cin.getline(number,80);
You have a buffer that can hold a max of 24 characters (size - 1), and then you specify a max of characters read of 80. That cries for an overflow.
for(i=0;i<26;i++)
Valid indices in an array of size n are 0 to n-1. You travel from 0 to n. Another access violation.
Another thing: always wrap your code in [ code] [/code ] tags and indent it when posting here. Makes it more readable and some people won't answer your question if you don't.
Here's my code (not complete):
Code:
#include <iostream> // !!!
#include <vector> // !!!
#include <cctype> // for the is* and to* macros
using namespace std;
// the lookup table
char numOfChar[] =
{ '2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6',
'7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9' };
// ...
for(char* p = number; *p, p++)
{
if(!isdigit(*p))
{
if(isalpha(*p))
{
*p = numOfChar[(toupper(*p) - 'A')];
}
else
cerr << "Invalid character in number" << endl;
}
}
// ...
Last edited by CornedBee; Mar 15th, 2002 at 08:42 AM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|