Skip to content

Instantly share code, notes, and snippets.

@peterchaula
Last active February 17, 2019 11:47
Show Gist options
  • Save peterchaula/6dde173190faac7a6afb155cb2986626 to your computer and use it in GitHub Desktop.
Save peterchaula/6dde173190faac7a6afb155cb2986626 to your computer and use it in GitHub Desktop.
Put that cleanLine function back in
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//void function declaration
string cleanLine(string line);
int main( )
{
//Declare variables to open files.
char inFileName[16], outFileName[16];
//Choose stream names for the input and output streams and
//declare them as variables of type ifstream and ofstream respectively.
ifstream inStream;
ofstream outStream;
// Inputs the input file name.
cout << "Enter the input file name: ";
cin >> inFileName;
// Inputs the output file name.
cout << "Enter the output file name: ";
cin >> outFileName;
// Opens the input file or displays an error message if unable to open.
inStream.open(inFileName);
if (inStream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
// Opens the output file or displays an error message if unable to open.
outStream.open(outFileName);
if (outStream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
//Declare variables.
string line, convertedNum;
//Loop through each line in the infile stream.
while(getline(inStream, line))
{
// Output the line to console.
cout << line;
convertedNum=" ";
// Find and replace all spaces
line = cleanLine (line);
//Looping through the line.
for (int i=0; i<line.length(); i++)
{
//Displays only the first 7 digits
convertedNum = convertedNum.substr(0,8);
//Calling the switch statement
switch(line[i])
{
case 'A':
case 'B':
case 'C':
convertedNum.append("2");
break;
case 'D':
case 'E':
case 'F':
convertedNum.append("3");
break;
case 'G':
case 'H':
case 'I':
convertedNum.append("4");
break;
case 'J':
case 'K':
case 'L':
convertedNum.append("5");
break;
case 'M':
case 'N':
case 'O':
convertedNum.append("6");
break;
case 'P':
case 'Q':
case 'R':
case 'S':
convertedNum.append("7");
break;
case 'T':
case 'U':
case 'V':
convertedNum.append("8");
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
convertedNum.append("9");
break;
default:
convertedNum.append("*");
break;
}
if (convertedNum.size() == 4)
{
convertedNum.append(" ");
}
}
//Output to console.
cout << convertedNum << "." << endl;
//Send to outfile stream.
outStream << convertedNum << endl;
}
return 0;
}
//Function to remove the spaces
string cleanLine(string line)
{
string newLine;
for(int i=0; i<line.size(); i++)
{
if (line[i] != ' ')
newLine += line[i];
}
return newLine;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment