fstream library

I have a homework assignment where I have to convert a list of words into a list of phone numbers (ex. turning GET LOAN into 438-5626) from an input file into an output file, however I am just overall confused on how to use the fstream library and its functions in order to achieve this. I understand how it works and have written the algorithm for it, as the algorithm was also required for my assignment, however, I do not know what to type that hasn't been from random guessing. Any hints on how to approach this code is appreciated.

My algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*Open input file
- check if input file failed
  -output error message
-continue if it didn't fail

Open output file
-all phone number translations go here
-check if output file opened correctly
 -output error message if it didn't

Read input file
-read each string a character at a time
 -after 3 digits, put a "-"
 -write translations to output file
 -output string that was translated as well

Repeat until the list is finished*/


and here's the code I've attempted so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

ifstream inFile;
ofstream outFile;

int counter;
char phoneLetters;
string phoneNumber;

inFile.open("input.txt");

if (!inFile){
cout<<"Error - try again"<<endl;
return 0;}
else{
outFile.open("output.txt");

if (!outFile.is_open()){
cout<<Error - try again"<<endl;
return 0;}

if (inFile.is_open()){
if (counter == 3){
cout<<"-";}}
}
return 0;
} 
You can use a char Array and work with "istream::getline" to get every single letter as a char.

To get every line from your Input file you can use a while-loop till "end of file".

If you don't know how to use a char Array let me know and i will help you.
Last edited on
That would be appreciated, we haven’t gone over char array yet so I’m not sure about the syntax for it.
GET LOAN into 438-5626

What's the formular for the conversion?
Can you show us the input file?
Okay then we should work with inFile.get(char).
Like this:
1
2
3
4
5
while(!inFile.eof())    // Reading till end of file
{
    inFile.get(phoneLetters);   // Reads every single character and give it to variable phoneLetters
    // here you can work with the letter
}

Thomas1965 wrote:
What's the formular for the conversion?

Don't you remember these, @Thomas1965?
https://en.wikipedia.org/wiki/Rotary_dial#/media/File:Rotarydial.JPG

Or
https://en.wikipedia.org/wiki/Telephone_keypad
Last edited on
@lastchance,
in Germany phones don't have this letters.

@OP
easiest is to read the file line line by line with getline and convert each letter into a digit and the space with '-'
@Thomas1965
Every mobile phone before they launched the Smartphone got a keypad.
I am of the few surviving dinosaurs who doesn't have a mobile phone.
@Thomas1965
Would the easiest way to convert the letters to numbers be a switch statement? For example:

1
2
3
4
5
switch (phoneLetters){
       case A:
       case B:
       case C:
       outFile << 2;
Yeah i think that would be the easiest way, but don't forget break after your outFile << 2;!
Yes a switch statement is probably the easiest.
Another option would be a lookup table.
Not sure if you are allowed to use arrays and functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

char char_to_digit(char ch)
{
  if (ch == ' ')
    return '-';

  const int N = 8;
  const int ERROR = -1;

  string letters[] = { "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY" };
  char   digits[] = { '2', '3', '4', '5', '6', '7', '8', '9' };
  
  for (size_t i = 0; i < N; i++)
    if (letters[i].find(ch) != string::npos)
      return digits[i];
  
  return ERROR;
}

int main(int argc, char **argv) 
{
  string s("GET LOAN");
  for (char ch : s)
  {
    char digit = char_to_digit(ch);
    if (digit != -1)
      cout << digit;
    else 
      cout << "ERROR";
  }
  cout << "\n\n";
}

Output
438-5626
std::map<char,int> or, maybe std::map<char,char>.

How do you get 1 or 0 in the 'phone number?
Topic archived. No new replies allowed.