file opening issue

This is a discussion on file opening issue within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I am terribly sorry for asking you guys to syntax check my code, but I am seriously stuck. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    32

    file opening issue

    Hi guys, I am terribly sorry for asking you guys to syntax check my code, but I am seriously stuck. I keep getting this error.
    Code:
    C:\>dm\bin\dmc l:\school\ipr\crypter\copy_of_cipher -I\dm\stlport\stlport
            information.open(&argv,std::ifstream::in);
                                                    ^
    l:\school\ipr\crypter\copy_of_cipher.cpp(110) : Error: need explicit cast for function parameter 1 to get
    from: char **
    to  : char const *
                            save();
                                 ^
    l:\school\ipr\crypter\copy_of_cipher.cpp(140) : Error: function expected
                            save();
                                  ^
    l:\school\ipr\crypter\copy_of_cipher.cpp(140) : Warning 6: value of expression is not used
    and here are my codes:
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    int i, i1;
    
    std::string PKey;	// the privact keys
    std::string Key;	// the key for power cipher
    int len;                // length of the text
    int Klen;               // length of the key
    int n;
    int e;			// the two privact keys
    
    std::string PlainText;	// the plain text
    int* Text;		// the cipher text
    
    void encrypt(void);
    void menu(void);
    void info(void);			// constructor
    void read(char*);			// constructor 2
    void getinfo(void);			// function to recieve input from user
    bool errorcheck(void);			// function to provide an error check for data entered
    void savefunc(char*);			// function to save the results onto a txt file
    void save(void);
    
    int main(int argc, char *argv[]) {
            std::cout << "Welcome to the first unBetaed version of Crypter." << std::endl << std::endl;
    
    	if(argc>2) 					// check number of arguements
    		std::cout << "Please enter a file to work with at a time" << std::endl;
    	else {
    		if(argc) {
    			read(argv[0]);
    			encrypt();
    		}
    		else {
    			menu();
    		}
    	}
    
            return 0;
    }
    
    void menu(void) {
            char choice;
    
            std::cout << "To encrypt a plain text, type 'e'." << std::endl;
            std::cout << "To quit the program, type 'q'." << std::endl;
            std::cin >> choice;
    	std::cin.ignore();
    	std::cout << "\n";
    
            switch(choice) {
    		case 'e':
    			info();
    	        	encrypt();
    			break;
    		case 'q':
    			return;
    		default:
    			std::cout << "Please enter a valid command.\n";
    			std::cin >> choice;
    			std::cin.ignore();
    			menu();
    			break;
            }
    
    	menu();
    }
    
    void encrypt(void) {
    	std::cout << "The encrypted message is: ";
    	for(i=0;i<len;++i) {
    		Text[i] = PlainText[i] ^ Key[i];		// raise the code to the power of the key
                    Text[i] = (Text[i] ^ e) % n;	// encrypt the message with RSA
    		std::cout << Text[i];
    	}
    	std::cout << std::endl;
    
    	save();
    }
    
    void info(void) {
    	getinfo();
    
            Klen = Key.length();					// write in length of key
    
    	i = PKey.find(',',0);					// find the comma
    	int temp = i;							// declare variable temp, inisialized as i
    	n = e = 0;
    	for(i=0;i<PKey.length();++i) PKey[i] -= 48;		// turn the ascii codes into the actual number
            for(i=0;i<temp;++i) n = n * 10 + PKey[i];	// write in n
            for(i=temp+1;i<PKey.length();++i) e = e * 10 + PKey[i];// write in e
    
    	len = PlainText.length();				// write in length of text
    	Text = new int[len];					// create text
    }
    
    void getinfo(void) {
    	std::cout << "Enter the plain text.\n";			// gathering the information
    	std::getline(std::cin,PlainText);
    	std::cout << "\nEnter the key.\n";
            std::cin >> Key;
    	std::cin.ignore();
    	std::cout << "\nEnter the person's public key in the form n,e\n";
    	std::cin.ignore();
    	std::getline(std::cin,PKey);
    
    	if(errorcheck())
    		getinfo();
    }
    
    bool errorcheck(void) {
    	i = PKey.find(",",0);					// check if there is a found or a space
    	if(i == std::string::npos || PKey[i+1] == ' ') {
    		std::cout << "Please enter the privact key in the form n,e without space." << std::endl;
    		return true;
    	}
    	for(i=0;i<Key.length();++i) 
    		if(Key[i]<65||(Key[i]>90&&Key[i]<97)||Key[i]>122) {
                           	std::cout << "Bits of the information is not registered." << std::endl;
    			std::cout << "Please enter the information again." << std::endl << std::endl;
    			return true;
    		}
            return false;
    }
    
    void read(char* argv) {
    	std::ifstream information;
    	information.open(&argv,std::ifstream::in);
    
    	getline(information,PlainText);
    	getline(information,Key);
    	getline(information,PKey);
    }	
    
    void save(void) {
    	std::cout << "\nIf you wish to save the file, type save followed by the filename you wish for." << std::endl;
    	char save[5], *file = new char[32];
    	std::cin >> save >> file;
    	i = strcmp(save,"save");
    	if(!i) {
    		savefunc(file);
    		std::cout << "Your file had been saved." << std::endl;
    	}
    	else {
    		std::cout << "The command you entered cannot be registered." << std::endl;
    		std::cout << "Try again? (Y/N) ";
    		char confirm;
    		std::cin >> confirm;
    		std::cin.ignore();
    
    		if((int)confirm == 78||(int)confirm == 110) {
    			return;
    		}
    		else {
    			if(!(int)confirm == 89&&(int)confirm == 121) {
    							std::cout << "Sorry, but your command cannot be registered." << std::endl;
    			}
    			save();
    		}
    	}
    }
    
    void savefunc(char *file) {
    	char filename[32];
    	strcat(filename, file);
    	strcat(filename, ".txt");
    
    	std::ofstream record;
    	record.open(filename,std::ofstream::out);
    
    	record << "THE ORIGINAL MESSAGE IS: " << PlainText << std::endl;
    	record << "THE SQUARE CIPHER KEY IS: " << Key << std::endl;
    	record << "THE PUBLIC KEY OF THE RECIEVER IS: " << n << "," << e << std::endl;
    	record << "THE ENCRYPTED MESSAGE IS: " << &Text << std::endl;
    
    	record.close();			// close the file
    }
    thanks alot, guys

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > information.open(&argv,std::ifstream::in)
    Remove the &

    > Error: function expected
    You have a function and variable with the same name.
    Code:
    void save(void) {
    	std::cout << "\nIf you wish to save the file, type save followed by the filename you wish for." << std::endl;
    	char save[5], *file = new char[32];
    > char filename[32];
    > strcat(filename, file);
    > strcat(filename, ".txt");
    You're already using std::string for some things, so go the whole way and use them for everything.
    Then at least you won't have bugs like trying to append to an uninitialised char array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    thanks mate, that helped alot, but the other error is still there...
    Code:
    l:\school\ipr\crypter\copy_of_cipher.cpp(110) : Error: need explicit cast for function parameter 1 to get
    from: char **
    to  : char const *

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you remove the & before argv?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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