Reading from text file

Working on an assignment for my algorithms class, the code is supposed to read in input from a given text document, reverse the given set of letters, and then offset them in the alphabet, with "." and "-" coming after "Z", with "0" declaring the end of input. As such:

Example Input:
1 ABCD
14 ROAD
1 SNQZDRQDUDQ
0

Example Output:
EDCB
ROAD
REVERSE_ROT


However I keep getting an error at "getline(afile, a);" (Line 23) and I can't figure out why.

/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/bits/basic_string.tcc:1068:5: note:   template argument deduction/substitution failed:
main.cpp:30:25: note:   'std::ofstream {aka std::basic_ofstream<char>}' is not derived from 'std::basic_istream<_CharT, _Traits>'


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
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

/*
 * 
 */
int main() 
{

    ofstream afile;
    afile.open("rot.txt");
    while(afile.good())
    {
        string a;
        string b;
        int c;
        
        getline(afile, a);
        int space = a.find_first_of(' ', 0);
        if(space == -1) {break;}
        b = a.substr(space+1, a.length()-space-1);
        a = a.substr(0, space);
        c = atoi(a.c_str());
        if(c == 0) {break;}
        reverse(b.begin(), b.end());
        for(int x = 0; x < b.length(); x++)
        { 
            int temp = (int)b[x] + c;
            if(temp > 90) { temp-=46; }
            b[x] = (char)temp;
        }
        cout << b;
    }
    afile.close();
    return 0;
}


Im also not sure exactly where the .txt file is supposed to go to be properly read from. Right now it's in the folder with the makefile.
Any help at all would be much appreciated. Thanks.
Last edited on
In case my code is hard to read,

string a takes in the entire line
the code then finds the first (and only) occasion of " "
two substrings are then made, one containing the number, the other "b" containing the letters

c then takes the number converted to integer

b is then reversed using the iterator reverse function in the algorithm library

then there is a for loop that goes through each character and adds the offset to its ascii value
if the ascii value ends up being over the ascii value for "Z", it subtracts enough so that the next characters are "." and "-"

then it prints the new string and goes to the next line

if c is ever zero or a space is never found, then the program stops
hiya,

if you're reading froma file shouldn't it be a ifstream and not a ofstream?

http://www.cplusplus.com/doc/tutorial/files/
I tried including the other files and it kept giving me something along the lines of, they don't exist.

I'll try it again...

"fatal error: no such file or directory"

I get fatal error for both ifstream and ofstream. fstream is the only one that doesnt give a fatal error.
Okay, I feel retarded.
I included fstream but declared my infile as ofstream.

I see what you were saying. That fixed it. Thanks a bunch.
Topic archived. No new replies allowed.