-
Using a string
Im doing c++ now in school, so I decided I should start doing some stuff at home. Im just messing around making a small program that will encrypt and decrypt text files, the only problem is Im getting an error for using the string a as the file path under the string file_content(string a); function.
Code:
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
string encrypt(string a);
string decrypt(string a);
string file_content(string a);
void main(){
string content;
string file;
string input;
cout<<"Welcome to ascii encrypter and decrypter v1.0\n please enter the file you wish to encrypt or decrypt : ";
cin>>file;
cin.get();
cout<<"\n getting file content";
content = file_content(file);
}
string encrypt(string a){
for (int z =0;z<a.size();z++){
a[z] += 5;
}
return a;
}
string decrypt(string a){
for (int z =0;z<a.size();z++){
a[z] -= 5;
}
return a;
}
string file_content(string a){
string file;
string buffer;
ifstream b_file (a);
while ( !b_file.eof() ){
b_file>>buffer;
buffer += " ";
file += buffer;
}
b_file.close();
return file;
}
-
void main(): :eek: :eek: :eek: :eek: :eek: :eek: :eek: :eek:
It is int main() not void main. Always has maybe always will.
Oh and it should be a.c_str() filestreams take char * not strings as an arg don't ask me why.
-
Code:
ifstream b_file (a.c_str());
Kurt
-
ah thank you. oh ya, I forgot about int main :P I havent done any c++ for almost 2 years. Been doing mainly flash programming :D
-
>> while ( !b_file.eof() ){
You should not use eof() to control your while loop. In most cases this causes the loop to run one extra time at the end. The solution is to move the b_file>>buffer code directly into the loop control:
Code:
while ( b_file>>buffer ){