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