Caesar Cipher reading text

I get the error of not declaring "text" "exit" but I how do I declared those variables,
even if they cause problem to file...
how can I output the decipher file in a new text...?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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;

    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);

        exit(0);


    }

    // This section reads and echo's the file one character (byte) at a time.

    while (inputFile.peek() != EOF) {

        inputFile.get(character);

        cout << character;

    char cipher[sizeof(character)];



    int shift;
    do {
        cout << "enter a value between 1-26 to encrypt the text: ";
        cin >> shift;
       } 
    while ((shift <1) || (shift >26));

    int size = strlen(text);
    int i=0;

    for(i=0; i<size; i++)
    {
        cipher[i] = text[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;   

}
closed account (Dy7SLyTq)
line 60 cipher is of type char[]. c-style arrays cant have a variable size. i recommend looking into vectors and other containers in the stl. look at line 71. what is text? you need to define that somewhere. same with 76
line 60 cipher is of type char[]. c-style arrays cant have a variable size.

The array doesn't have a variable size. The size is constant and is guaranteed to be 1. character is of type char and sizeof(char) is always 1.

Unfortunately, the OP is treating it as if the array were larger than one character.
closed account (Dy7SLyTq)
sorry cire my mistake. why doesnt the compiler flag character when i use it for my own uses? like in rpgs?
It isn't a keyword. It is declared in his code:

char character;
closed account (Dy7SLyTq)
oh yeah thats right... every character is 8 bytes right?
A variable of type char is required to have at least 8 bits, yes.

(I don't see how that's relevant to anything that's been mentioned so far.)
Last edited on
Topic archived. No new replies allowed.