Functions and pointers

This is a discussion on Functions and pointers within the C++ Programming forums, part of the General Programming Boards category; Dear friends, I have previosly posted part of this code. I am improving on it to take its input from ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Functions and pointers

    Dear friends,

    I have previosly posted part of this code. I am improving on it to take its input from a file and output to another file. I am also trying to make it more user interactive.

    I achieved writing the program output to a file using:

    ofstream file_op("c:\\file.txt",ios::app);
    file_op<<n;
    file_op.close();



    I am using CV++ 6.0 to compile this code. The following errors are reported. Can someone please help me
    learn how to remove these errors.

    Compiling...
    encrypt2Function.cpp
    C:\encrypt2Function.cpp(36) : error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    C:\encrypt2Function.cpp(80) : error C2181: illegal else without matching if
    C:\encrypt2Function.cpp(100) : error C2601: 'Interface' : local function definitions are illegal
    C:\encrypt2Function.cpp(131) : error C2601: 'EncDec' : local function definitions are illegal
    Error executing cl.exe.

    encrypt2Function.obj - 4 error(s), 0 warning(s)

    My code is

    Code:
    #include <iostream>
    #include <string>
    #include <time.h>
    #include <conio.h>
    #include <fstream>
    
    #define DATA	 0x378  //Data Register base address
    #define STATUS   DATA+1 // Status Register address
    #define CONTROL  DATA+2  //Control Register address
    
    
    using namespace std;
    
    //Function prototyping
    
    void Interface (ifstream &fin, char (*Secure)(char c));
    
    char EncDec (char c);
    
    //main (): manage file stream extraction
    
    int main ()
    
    {
    
    	const int MaxFileNameSize=256;
    
    	// prompt and extract the name of the file
    
    	cout<<"Enter the name of the file:"<<flush;
    
    	char FileName;
    
    	//validate the file name provided
    
    	ifstream fin (FileName);
    
    	if (fin)
    
    	{
    		//we have a valid file, so determine the action
    
    		cout<<"The file contents to be made secure through"
    
    			<<"bit level transformation (Y,N):"<<flush;
    
    		char reply;
    
    		cin>>reply;
    
    		//process the file according to the user request
    
    		switch (reply)
    
    		{
    
    		case 'Y':
    
    			Interface(fin, EncDec);
    
    			break;
    
    		case 'N':
    
    			cout<<"File is left in plain version";
    
    			break;
    
    		default:
    
    			{
    
    				cerr<<"Bad request"<<endl;
    
    				return 1;
    			}
    
    		}
    
    	else 
    
    		{
    
    			cerr<<"Invalid file name:"<<FileName<<endl;
    
    			return 1;
    
    		}
    
    return 0;
    
    }
    
    	//Interface (): Manage the interface to the stream cipher 
    
    	//using the EncDec () function.
    
    	void Interface (ifstream &fin, char (*secure)(char c))
    
    	{
    
    		//Extract, modify and save one-character-at-a-time 
    
    		//from the file.
    
    		char CurrentChar;  //currenrly extracted character
    
    		while (fin.get(CurrentChar))
    
    		{	//modify and save the current character using
    			//Secure()
    
    			CurrentChar=(*Secure)(CurrentChar);
    
    			ofstream   file_op("C:\\CodeOutput file_out.txt"), ios::out);
    
    			file_op<<CurrentChar;
    
    		}
    
    		return;
    
    	}
    
    	//EncDec(): manage writing to and reading from the 
    
    	//standard parallel port.
    
    	char EncDec (char c )
    
    	{
    		char s, n;
    
    		_outp(CONTROL,_inp(CONTROL)&0xF0|0x04); /*initialise the
    																CONTROL port for data input*/
    
    		_outp(DATA, s);   /*write c to the DATA register*/
    
    		clock_t start_time;
    
    		start_time=clock();
    
    		while ((clock()-start_time)<0.25*CLK_TCK);//propagation delay time
    
    		n=(_inp(STATUS)&0xF0);  /* read MSnibble*/
    
    		n=n|(_inp(CONTROL)&0x0F);/*Read LSnibble*/
    
    		n=n^0x84; /*toggle bit 2 and 7 */
    		/*pins 11(bit 7)-BUSY- is hardware inverted. Pin 16 has been inverted by the external 		hardware*/
    					
    		return s;
    	
    
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    char FileName;
    This is a single char. Either declare an array like this
    Code:
    char FileName[MAX_PATH];
    or use a string object
    Code:
    string FileName;

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Quantum1024
    Code:
    char FileName;
    This is a single char. Either declare an array like this
    Code:
    char FileName[MAX_PATH];
    or use a string object
    Code:
    string FileName;
    Looking at these lines:
    Code:
    const int MaxFileNameSize=256;
    
    // prompt and extract the name of the file
    
    cout<<"Enter the name of the file:"<<flush;
    
    char FileName;
    it looks like you meant to do this:

    char FileName[MaxFileNameSize];

    In addition, you never even read in the file name to begin with:
    Code:
    cout<<"Enter the name of the file:"<<flush;
    
    char FileName;
    
    //validate the file name provided
    
    ifstream fin (FileName);
    You used the variable FileName to create your input stream, but you never assigned a value to FileName, i.e. you never read in the file name from the user.

    Quote Originally Posted by Quantum1024
    Code:
    or use a string object
    string FileName;
    You should probably stick to char arrays, since the argument for this function:

    ifstream fin(FileName)

    has to be a char array. If you do use a string type instead of a char array, then you need to do this:
    Code:
    string FileName;
    
    //read in FileName from user
    
    ifstream fin(FileName.c_str());
    which converts the string type to a char array type for the function.

    C:\encrypt2Function.cpp(80) : error C2181: illegal else without matching if
    Get rid of the parentheses(look up the format for a switch statement):
    Code:
    default:
    
    		{
    
    			cerr<<"Bad request"<<endl;
    
    			return 1;
    		}
    C:\encrypt2Function.cpp(100) : error C2601: 'Interface' : local function definitions are illegal
    Because of your parentheses problem above, the compiler thinks your function definitions are inside main().
    Last edited by 7stud; 04-02-2005 at 02:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

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