I have to use a class that converts number (2-9) to words ie; 368253 is double.
I have a file of 100,000 words that I have to "bounce" the string of numbers off of to form any possible string of words. It also has to be able to convert a full message such where the user would input numbers that correspond to words separated by a white space. I have the class written, but I'm not sure if that's even right. I have been burying my head in a few different text books as well as searching the internet with no luck. I don't want it done for me but I could use a few pushes in the right direction. Here is what I have so far (not much I know).

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class KeyPad
{
      private:
              string Number;
              string Word;
      public:
             void readnumbers(string nums);
             void readwords(string words);
             void check_numbers();
             void convert_nums_to_words();
             void convert_words_to_nums();
};

Recommended Answers

All 43 Replies

Once you fill in some of the definitions of those functions, we'll be able to help you. This is just a very very high level outline.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class KeyPad
{
      private:
              string nums;
              string words;
      public:
             void readnumbers(string nums);
             void readwords(string words);
             void check_numbers();
             void convert_nums_to_words();
             void convert_words_to_nums();
};

void KeyPad::readnumbers ()
{
     cout << "Enter numbers you wish to convert." << endl;
     cin >> nums;
}
void KeyPad::check_numbers (string nums)
{
   if  (int nums.find(1, 0))
       cout << "Then number 1 does not corrospond to a letter on a standard"
               "keypad." << endl;
}
void KeyPad::readwords (string words);
{
     cout << "Enter words you wish to convert to numbers."
     cin >> words;
     Lower = lowerwords(words);
}

    
void KeyPad::convert_nums_to_words()
{     
     ifstream input_file("USdictionary.txt");
     2=(a||b||c);
     3=(d||e||f);
     4=(g||h||i);
     5=(j||k||l);
     6=(m||n||o);
     7=(p||q||r||s);
     8=(t||u||v);
     9=(w||x||y||z);
}
void  KeyPad::convert_nums_to_words()
{
     2=(a||b||c);
     3=(d||e||f);
     4=(g||h||i);
     5=(j||k||l);
     6=(m||n||o);
     7=(p||q||r||s);
     8=(t||u||v);
     9=(w||x||y||z);
}

That is what I have so far. My main problem is that I know how to read a file into a program and do stuff with it but I don't know how to just "reference" a file. My main problem is that and then how to actually make the conversion from one to the other. I also know how to write the results to an output file. Any help is very appreciated. I am in a bit over my head with this assignment I think. Thank you so much.

This

2=(a||b||c);

is not valid c++ syntax. You should look up conditional (if) statments:

if( yourChar == a || yourChar == b || yourChar == c)
{
  // do something with number '2'
}

This tutorial is excellent:
http://www.cplusplus.com/doc/tutorial/

I suggest you start there.

David

The tutorial is very helpful, but I still have a few questions:

if( yourChar == a || yourChar == b || yourChar == c)
{
 num=1
}

Is that kind of the direction I should be going? My only concern is would that work going both ways? ie: numbers to letters and letters to number. Also will my code use the file USdictionary to get words?

Of course that won't work both ways - you have only written one way :)

Also, it doesn't look like you've written anything to read a dictionary file, but you can certainly do so if you wish.

There are some IO examples here:
http://programmingexamples.net/index.php?title=CPP

Reading files into a program isn't the problem. My code may not do it yet because I am not sure how to use the file the way I need. But how do I take user inputs and compare them to the file and then output the word or group of words?

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class KeyPad
{
      private:
              string nums;
              string words;
      public:
             void read_file ();
             void readnumbers();
             void readwords();
             void check_numbers();
             double convert_nums_to_words(string stringW);
             double convert_words_to_nums(string stringN);
};
void read_file ()
{
   ifstream input_file ("USdictionary.txt");
}  



void KeyPad::readnumbers ()
{
     cout << "Enter numbers you wish to convert." << endl;
     cin >> nums;
}
void KeyPad::check_numbers ()
{
   if  (int nums.find(1, 0))
       cout << "Then number 1 does not corrospond to a letter on a standard"
               "keypad." << endl;
   if (int nums.find(0, 0))
      cout << "The number 0 does not correspond to a letter on a standard"
              "keypad." << endl;
}
return (fail);
void KeyPad::readwords (string words);
{
     cout << "Enter words you wish to convert to numbers."
     cin >> words;
     Lower = lowerwords(words);
}

    
double KeyPad::convert_nums_to_words()
char yourChar, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v;
               w, x, y, z;
int num;
{     
    if( yourChar == a || yourChar == b || yourChar == c)
{
 num=2;
}
if( yourChar == d || yourChar == e || yourChar == f)
{
 num=3;
}
if( yourChar == g || yourChar == h || yourChar == i)
{
 num=4;
}
if( yourChar == j || yourChar == k || yourChar == l)
{
 num=5;
}
if( yourChar == m || yourChar == n || yourChar == o)
{
 num=6;
}
if( yourChar == p || yourChar == q || yourChar == r || youChar == s)
{
 num=7
}
if( yourChar == t || yourChar == u || yourChar == v)
{
 num=8;
}
if( yourChar == w || yourChar == x || yourChar == y || yourChar == z)
{
 num=9
}
}     return (string stringW);

double  KeyPad::convert_nums_to_words()
char yourChar, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v;
               w, x, y, z;
{
if ( yourNum == 2 )
{
     yourChar == a || b || c;    
}
if ( yourNum == 3 )
{
     yourChar == d || e || f;
}
if ( yourNum == 4 )
{
     yourChar == g || h || i;
}
if ( yourNum == 5 )
{
     yourChar == j || k || l;
}
if ( yourNum == 6 )
{
     yourChar == m || n || o;
}
if ( yourNum == 7 )
{
     yourChar == p || q || r || s;
}
if ( yourNum == 8 )
{
     yourChar == t || u || v;
}
if ( yourNum == 9 )
{
     yourChar == w || x || y || z;
}
}    return (string stringN);

This is what I have so far. Still having trouble with using the file to compare (among many other things). This is obviously not my thing but I need to get through this or I'll have to take it again next semester. So please any help would be very appreciated.

Mike

I have now broken the code up into three sections (project requirement)
Here is the class:

#include <cstdlib>
#include <iostream>
#include <string>

//Class definition
using namespace std;

class KeyPad
{
      private:
              string nums;
              string words;
      public:
             void read_file ();
             void readnumbers();
             void readwords();
             void check_numbers();
             double convert_nums_to_words(string stringW);
             double convert_words_to_nums(string stringN);
             void disp_message_words;
             void disp_message_nums;
             
};

Here is the class implementation:

#include <cstdlib>
#include <iostream>
#include <string>
#include "KeyPad.h"
//Class Implementation

void read_file ()
{
   
   ifstream input_file("USdictionary.txt");
   if (inFile.fail())
{
      cout << "\nThe file was not opened successfully, please check"
              "\the file exists. " << endl;
      exit (1);
}
}  

       


void KeyPad::readnumbers ()
{
     cout << "Enter numbers you wish to convert." << endl;
     cin >> nums;
}
void KeyPad::check_numbers ()
{
   if  (int nums.find(1, 0))
       cout << "Then number 1 does not corrospond to a letter on a standard"
               "keypad." << endl;
   if (int nums.find(0, 0))
      cout << "The number 0 does not correspond to a letter on a standard"
              "keypad." << endl;
}
return (fail);
void KeyPad::readwords (string words);
{
     cout << "Enter words you wish to convert to numbers."
     cin >> words;
     Lower = lowerwords(words);
}

    
double KeyPad::convert_words_to_nums()
char yourChar, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v;
               w, x, y, z;
int num;
{     
    if( yourChar == a || yourChar == b || yourChar == c)
{
 num=2;
}
if( yourChar == d || yourChar == e || yourChar == f)
{
 num=3;
}
if( yourChar == g || yourChar == h || yourChar == i)
{
 num=4;
}
if( yourChar == j || yourChar == k || yourChar == l)
{
 num=5;
}
if( yourChar == m || yourChar == n || yourChar == o)
{
 num=6;
}
if( yourChar == p || yourChar == q || yourChar == r || youChar == s)
{
 num=7
}
if( yourChar == t || yourChar == u || yourChar == v)
{
 num=8;
}
if( yourChar == w || yourChar == x || yourChar == y || yourChar == z)
{
 num=9
}
}     return (string stringW);

double  KeyPad::convert_nums_to_words()
char yourChar, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v;
               w, x, y, z;
{
if ( yourNum == 2 )
{
     yourChar == a || b || c;    
}
if ( yourNum == 3 )
{
     yourChar == d || e || f;
}
if ( yourNum == 4 )
{
     yourChar == g || h || i;
}
if ( yourNum == 5 )
{
     yourChar == j || k || l;
}
if ( yourNum == 6 )
{
     yourChar == m || n || o;
}
if ( yourNum == 7 )
{
     yourChar == p || q || r || s;
}
if ( yourNum == 8 )
{
     yourChar == t || u || v;
}
if ( yourNum == 9 )
{
     yourChar == w || x || y || z;
}
}    return (string stringN);

void KeyPad::disp_message_words ()
{
       cout << stringW << endl;
       
}
void KeyPad;;disp_message_nums ()      
{
     cout << stringN << endl;

And here is the main function:

#include <cstdlib>
#include <iostream>
#include "implement.cpp"
using namespace std;


{
    int main ()
{
    KeyPad W1, N1 M1, M2;
    
    cout << "Your entered message in numbers is " << nums << endl;
    W1.convert_nums_to_words ();
    cout << "Your entered message in words is "  << words << endl;
    N1.convert_words_to_nums ();
    cout << "Your message converts to " << endl;
    M1.disp_message_nums;
    cout << "Your message converts to " << endl;
    M2.disp_message_words;
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

Am I remotely close?

It seems reasonable. What is the problem? Does it not work correctly? Are there compiler errors?

There are tons of errors.

Ok, let's start with the first one. Usually one or two errors at the beginning cause "lots" of other errors that are not actually themselves additional errors but the compiler gets confused and reports a lot more than is necessary to fix the problem.

3 C:\Dev-Cpp\main.cpp In file included from main.cpp
5 C:\Dev-Cpp\implement.cpp KeyPad.h: No such file or directory.

Is KeyPad.h the name of the first code snippet you posted (where you said "Here is the class") ? Is this in the same directory as the rest of the files? Are you using Visual Studio?

Project #3.docx

Here is an attachment of the assignment. That may tell you what I have to do better than I can.

I'm using bloodshead dev C++. Yes KeyPad.h is the class

I'm not familiar with Bloodshead, but my guess is that your files are either not in the same directory or you need to set your INCLUDE_PATH to include the current directory (.) (which should definitely already be set).

Also, you should never include a .cpp file

#include "implement.cpp"

how do I do that?

Ok I have no saved all three elements to one file and I get a couple of different errors, but a ton of errors none the less. Here are the first couple:

1) 3 C:\Dev-Cpp\implement.cpp:5, from main.cpp In file included from implement.cpp:5, from main.cpp
2)3 C:\Dev-Cpp\main.cpp from main.cpp
3)20 C:\Dev-Cpp\KeyPad.h variable or field `disp_message_words' declared void
4)21 C:\Dev-Cpp\KeyPad.h variable or field `disp_message_nums' declared void

You cannot call a function like this:

M2.disp_message_words;

You have to call it like this:

M2.disp_message_words();

Ok, fixed that. (I should have seen that).
still getting lots of errors.

Look, if I take your code, compile it, and tell you everything to fix, no one wins. I wasted my time, you didn't learn anything, and no one else reading this likely learned anything. If you give it a shot and try to read the error the compiler gives you and then ask us specific questions if you get stuck, that is a much better process.

Thats what I am trying to do now.
It's giving me this error:
21 C:\Dev-Cpp\KeyPad.h variable or field `disp_message_nums' declared void

I am guessing that I should declare that differently than void?

It is the same problem as last time. You need to add (). Change

void disp_message_nums;

to

void disp_message_nums();

I am finding a little success in clearing errors. But I have run into this:
38 C:\Dev-Cpp\implement.cpp declaration of `void KeyPad::readwords(std::string)' outside of class is not definition
I think the error is in what I have in the parenthesis. I know if I want to return a double I put (double), but I am not sure how to do that with a string.

You have declared it as:

void readwords();

but defined it as

void KeyPad::readwords (string words);

These need to match.

in this snippet

double KeyPad::convert_words_to_nums(string stringN)
int num;
char yourChar, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,w, x, y, z;

I am getting this error. I have tried putting a semi colon after (string stringN), but that didn't work.

You need to enclose the function in braces:

double KeyPad::convert_words_to_nums(string stringN)
{
int num;
char yourChar, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,w, x, y, z;

...

return something;
}

for my functions:

void KeyPad::disp_message_words ()
{
       cout << stringW << endl;
       
}

and

void KeyPad::disp_message_nums ()      
{
     cout << stringN << endl;
       
}

Its giving me an error that stringW and string N are undeclared variables, but thats what I return in the previous function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.