how to read whitespace using infile from a file in c++?

i have to make a program which reads each chaacter from a file and using the caesar cipher if the character is an alpha it should be shifted by the user specified number, if it is not an alpha it should be left alone, including spaces. i am so close, everyhting works except for the program not reading the spaces and putting in spaces where they need to be in the outfile

thanks in advance!!!

here is my program

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;

char encode(char&, int);
char decode(char&, int);

int main()
{
    cout << "Enter E to encode a file or D to decode a file: ";
    char choice;
    cin >> choice;
    
    while(choice != 'E' && choice != 'D')
    {
        cout << "Enter E to encode a file or D to decode a file: ";
        cin >> choice;
    }
    
    if(choice == 'E')
    cout << "Enter the name of the file to be encoded: ";
    
    if(choice == 'D')
    cout << "Enter the name of the file to be decoded: ";
    
    ifstream inFile;
    string inputFilename;
    cin >> inputFilename;
    
    inFile.open(inputFilename.c_str());
    
    while(inFile.fail())
    {
        cout << inputFilename << " does not exist, enter new file name: ";
        cin >> inputFilename;
        inFile.open(inputFilename.c_str());
    }
    
    if(choice == 'E')
    cout << inputFilename << " will be encoded and written to a new file.\n";
    
    if(choice == 'D')
    cout << inputFilename << " will be decoded and written to a new file.\n";
    
    cout << "What would you like to name this file? ";
    string outputFilename;
    cin >> outputFilename;
    
    ifstream checkFile;
    checkFile.open(outputFilename.c_str());
    
    if(!checkFile.fail())
    {
        cout << outputFilename << " already exists.\n"
             << "Do you want to overwrite the file?\n"
             << "Enter Y to overwrite, or any other character to quit: ";
             
        char decision;
        cin >> decision;
        
        if(decision != 'Y')
            return 0;
    }
    
    ofstream outFile;
    outFile.open(outputFilename.c_str());

    cout << "Enter the key (an integer): ";
    int key;
    cin >> key;
    
    char letter;
    
    if(choice == 'E')
    {
        while(inFile.good())
        {
            if(letter == 32)
            outFile << " "; // i thought this would work but it doesnt
            inFile >> letter;
            encode(letter, key);
            outFile << letter;
        }
    }
    
    if(choice == 'D')
    {
        while(inFile.good())
        {
            if(letter == 32)
            outFile << " "; // i thought this would work but it doesnt
            inFile >> letter;
            decode(letter, key);
            outFile << letter;
        }
    }
    
    return 0;
}

char encode(char& t, int k)
{
    if(t >= 'A' && t <= 'Z')
    t = (t - 'A' + k) % 26 + 'A';
   
    else if (t >= 'a' && t <= 'z')
    t = (t - 'a' + k) % 26 + 'a';
    
    return t;
}

char decode(char& t, int k)
{
    if(t >= 'A' && t <= 'Z')
    t = (t - 'A' - k + 26) % 26 + 'A';
   
    else if (t >= 'a' && t <= 'z')
    t = (t - 'a' - k + 26) % 26 + 'a';
    
    return t;
}


here is an example file:

a b c d e f g h ijklmn o p q r s t u v w x y z
A B C D E F G H I J K LMNOP Q R S T U V W X Y Z
1 2 34 5

this is what the program should do if i encode with a key of 5:

f g h i j k l m nopqrs t u v w x y z a b c d e
F G H I J K L M N O P QRSTU V W X Y Z A B C D E
1 2 34 5

here is what i get with my program:

fghijklmnopqrstuvwxyzabcdeFGHIJKLMNOPQRSTUVWXYZABCDE123455

all i have to do is figure out how to get it to read the white spaces and put white spaces in the right place and why does my program put an extra 5 in the file at the end?
1
2
if(letter == 32)
outFile << " "; // i thought this would work but it doesnt 


Look at these two lines. You are saying if the letter == 32, write a space. If letter != 32, it skips writing a space.
Last edited on
i realized that, so then how do i tell the program to read whitespaces? i tried isspace(letter), but again realized that the 'letter' only represents characters in the file and not spaces? what in c++ allows me to read whitespace?
http://www.cplusplus.com/reference/istream/istream/get/

I will help more if you do not understand after that =)
i tried to use inFile.get(letter) to get the each character from the file then an if statement to add a space if the character equals 32 on the ascii table but all it did was display a bunch of spaces and a 5 at the end.

1
2
3
4
5
6
7
8
9
10
11
12
    if(choice == 'E')
    {
        while(inFile.good())
        {
            inFile >> letter;
            inFile.get(letter);
            if(letter == 32)
            cout << " ";
            encode(letter, key);
            outFile << letter;
        }
    }


am i getting closer to figuring it out? how do i fix this?
You are getting close! Okay, so you need to replace inFile.get(letter) with something else, because the "letter" variable is already used. So, you are now getting the whitespace, and replacing the letter with it. So if you had infile >> letter; followed by infile.get(whitespace);
See where I'm going with this?
1
2
3
4
5
6
7
8
9
10
11
    if(choice == 'E')
    {
        while(inFile.good())
        {
            inFile.get(letter);
            if(letter == 32)
            cout << " ";
            encode(letter, key);
            outFile << letter;
        }
    }


thank you

this works but for some reason it adds an extra character thats different eveytime i change the file, just like in the provious example you can see it adds an extra 5 at the end for some reason. do you know why this happens?
change while(inFile.good()) to while(inFile.get(letter)) and remove line 5. I don't like to use the 'good' function for this purpose, or using EOF (for this purpose) because they act quirky sometimes. If you do some research, I imagine you could find a detailed answer why.

The way I just told you is best because it checks if the the 'get' function fails.
Last edited on
thank you!! the program works perfectly now.
Topic archived. No new replies allowed.