私はC++にかなり慣れていません。そうです、これは宿題です。関数内のifelseステートメントの代わりにswitchステートメントを検討しています。
データが操作された後、入力ファイルストリームから読み取った情報を出力ファイルに書き込もうとしています。
prgramは、ファイルから情報を読み取り、それを処理してから、コンソールにデータを表示し、結果を出力ファイルに書き込むことになっています。プログラムは、入力ファイルと出力ファイルの両方のファイル名を入力するようにユーザーに要求する必要があります。
プログラムにファイルを作成させることができません。ただし、すでに存在するファイルで機能します。
プログラムが存在しない場合は、プログラムでファイルを作成するのを手伝ってください。
ああ、これはC++です
どんな助けでも大歓迎です。
私のコード:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
char getNumber(char l);
int main ()
{
string s1 = "D:\\Unisa\\Assignment_stuffs\\COS1512\\Assignment\\";
string inFile, outFile;
cout << " Please enter the input filename: ";
cin >> inFile;
cout << "\nPlease enter the output filename: ";
cin >> outFile;
string inFileAdd = s1 + inFile;
string inFileAdd2 = s1 + outFile;
ifstream in_stream;
ofstream out_stream;
in_stream.open(inFileAdd.c_str(), ios::in);
if (in_stream.fail())
{
cout << "Error!! Input file opening failed.";
exit(1);
}
out_stream.open(inFileAdd2.c_str(), ios::out);
if (out_stream.fail())
{
cout << "Error!! Output file opening failed.";
exit(1);
}
char next = ' ';
string letter;
while (!in_stream.eof())
{
in_stream.get(next);
while (next != '\n')
{
cout << next;
out_stream.put(next);
letter = letter + getNumber(next);
in_stream.get(next);
}
cout << " " + letter;
out_stream << " " + letter << endl;
letter = "";
cout << endl;
}
in_stream.close();
return 0;
}
char getNumber(char l)
{
if ((l == 'A') || (l == 'a') || (l == 'B') || (l == 'b') || (l == 'C') || (l == 'c'))
{
return '2';
}
else if ((l == 'D') || (l == 'd') || (l == 'E') || (l == 'e') || (l == 'F') || (l == 'f'))
{
return '3';
}
else if ((l == 'G') || (l == 'g') || (l == 'H') || (l == 'h') || (l == 'I') || (l == 'i'))
{
return '4';
}
else if ((l == 'J') || (l == 'j') || (l == 'K') || (l == 'k') || (l == 'L') || (l == 'l'))
{
return '5';
}
else if ((l == 'M') || (l == 'm') || (l == 'N') || (l == 'n') || (l == 'O') || (l == 'o'))
{
return '6';
}
else if ((l == 'P') || (l == 'p') || (l == 'Q') || (l == 'q') || (l == 'R') || (l == 'r') || (l == 'S') || (l == 's'))
{
return '7';
}
else if ((l == 'T') || (l == 't') || (l == 'U') || (l == 'u') || (l == 'V') || (l == 'v'))
{
return '8';
}
else if ((l == 'W') || (l == 'w') || (l == 'X') || (l == 'x') || (l == 'Y') || (l == 'y') || (l == 'Z') || (l == 'z'))
{
return '9';
}
}