Using a string

This is a discussion on Using a string within the C++ Programming forums, part of the General Programming Boards category; Im doing c++ now in school, so I decided I should start doing some stuff at home. Im just messing ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Lightbulb 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;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    void main():
    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.
    Woop?

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    ifstream b_file (a.c_str());
    Kurt

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    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

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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 ){

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21