Converting characters to numbers using data files

Goodday,

I need to convert characters in a input file to numbers in output file and display both the characters and numbers in a console window.

Only 7 numbers can be displayed, the rest needs to be dropped.

The input file contains the following data, with one number per line:
CALL HOME
GET LOAN
Hi THERE
BYE FOR NOW
HELP ME PASS

For example, once the first number in the input file has been processed, the console window should display the following:
CALL HOME 225 5466.

The output file should now also contain the number:
225 5466

Currently my console is displaying the following:


Enter the input file name.
infile.txt

Enter the output file name.
outfile.txt
2554663Invalid Input
4385626Invalid Input
4Invalid Input
84373Invalid Input
293367669Invalid Input
4357637277CALL HOME
GET LOAN
Hi THERE
BYE FOR NOW
HELP ME PASS Press any key to continue . . .


I am very new to this and need assistance

Here is my code

//Ass 1 Question 4

#include <iostream> // for screen/keyboard i/o
#include <fstream> // for file
#include <cstdlib> // for exit


using namespace std;

void openFile(ifstream& infile)
{
char letter;
infile.get(letter);

while(!infile.eof())
{
cout << letter;
infile.get(letter);
}
}

int main()
{

ifstream infile;
ofstream outfile;
ifstream inconsole;
ifstream outconsole;
string inputName, outputName;

cout << endl << "Enter the input file name. " << endl ;
cin >> inputName;

cout << endl << "Enter the output file name. " << endl;
cin >> outputName;

infile.open(inputName.c_str());

if (infile.fail())
{
cout << "Cannot open file " << inputName << " Aborting!" << endl;
exit(1);
}

outfile.open(outputName.c_str());

if (outfile.fail())
{
cout << "Cannot open file " << outputName << " Aborting!" << endl;
exit(1);
}


char letter;
int counter = 0;

infile.get(letter);

while(! infile.eof())
{

infile.get(letter);

for (counter = 0; counter <= 6; counter += 1) {

if (counter == 3)
cout << " ";

if (counter >= 6)
cin.ignore(1000, '\n');

switch (letter)
{
case 'A':
case 'B':
case 'C':
cout << 2;
break;
case 'D':
case 'E':
case 'F':
cout << 3;
break;
case 'G':
case 'H':
case 'I':
cout << 4;
break;
case 'J':
case 'K':
case 'L':
cout << 5;
break;
case 'M':
case 'N':
case 'O':
cout << 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cout << 7;
break;
case 'T':
case 'U':
case 'V':
cout << 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cout << 9;
break;
case ' ':
counter = counter - 1;
break;
default:
cout << "Invalid Input" << endl;
}
}
}

inconsole.open(inputName.c_str());

if (inconsole.fail())
{
cout << "Cannot open file " << inputName << " Aborting!" << endl;
exit(1);
}


openFile(inconsole);
inconsole.close();


outconsole.open(outputName.c_str());

if (outconsole.fail())
{
cout << "Cannot open file " << outconsole << " Aborting!" << endl;
exit(1);
}

openFile(outconsole);
outconsole.close();

infile.close();
outfile.close();

system("pause");
return 0;
}
closed account (Dy7SLyTq)
#include <string>
Topic archived. No new replies allowed.