Проблема в том, что файл не будет читаться ... По-видимому, есть проблема с массивом, но я действительно не знаю, как исправить эту проблему ... Я начинаю с C++ ' массивы и „строки“ ...Хранение переменной в C++
Мой файл должен прочитать код, а затем перевести файл, а затем выводить текст в новый файл ..
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
int main()
{
// Declarations
string reply;
string inputFileName;
ifstream inputFile;
ofstream outFile;
char character;
cout << "Input file name: ";
getline(cin, inputFileName);
// Open the input file.
inputFile.open(inputFileName.c_str());
// Check the file opened successfully.
if (! inputFile.is_open()) {
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
return 1;
}
// This section reads and echo's the file one character (byte) at a time.
while (inputFile.peek() != EOF) {
inputFile.get(character);
//cout << character;
//Don't display the file...
char cipher[sizeof(character)];
//Caesar Cipher code...
int shift;
do {
cout << "enter a value between 1-26 to encrypt the text: ";
cin >> shift;
}
while ((shift <1) || (shift >26));
int size = strlen(character);
int i=0;
for(i=0; i<size; i++)
{
cipher[i] = character[i];
if (islower(cipher[i])) {
cipher[i] = (cipher[i]-'a'+shift)%26+'a';
}
else if (isupper(cipher[i])) {
cipher[i] = (cipher[i]-'A'+shift)%26+'A';
}
}
cipher[size] = '\0';
cout << cipher << endl;
}
cout << "\nEnd of file reached\n" << endl;
// Close the input file stream
inputFile.close();
cout << "Press enter to continue...";
getline(cin, reply);
return 0;
}
«По-видимому, есть проблема с массивом, но я действительно не знаю, как исправить эту проблему ...» Если вы получаете сообщения об ошибках, вы должны предоставить их нам для просмотра. –
Вы передаете одиночный символ 'strlen', который ожидает тип' const char * '. Зачем вообще называть strlen? char всегда будет 1. – greatwolf
Что вы ожидаете 'char cipher [sizeof (character)];' делать точно? – greatwolf