Thread: help again :)

  1. #1
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36

    Unhappy help again :)

    I'm getting this error message
    Info :Compiling G:\Encryption\version21.cpp

    Error: version21.cpp(119,11):Case bypasses initialization of a local variable


    How can i fix it

    Heres the code:Not finished tho)

    Code:
    //By:Nick Johnson
    //version 1.1
    
    #include <fstream.h>
    #include <conio.h>
    #include <iomanip.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdio.h>
    
    char user[256];
    char input_file[60];
    int choice;
    
    void input(int&choice);
    void process(int&string_length, int&key,int&user_choice,int&key2);
    
    ifstream infile;
    
    int main()
    {
       clrscr();
    	int user_choice,string_length;
      	int key, key2;
    
    
    		cout<<"\n"<<setw(40)<<"\tEncryption\n\n";
    
       input(choice);
       process(string_length,key,user_choice,key2);
    
    	return 0;
    }
    
    void input()
    {
    
    	cout<<"\t\t\t ************************** "<<endl;
       cout<<"\t\t\t * 1. Encrypt a nonfile   * "<<endl;
       cout<<"\t\t\t * 2. Encrypt a file      * "<<endl;
       cout<<"\t\t\t * 3. Decrypt a file      * "<<endl;
       cout<<"\t\t\t * 4. Exit                * "<<endl;
       cout<<"\t\t\t ************************** "<<endl;
    
       cout<<"\nEnter your choice: ";
       cin >>choice;
    }
    
    
    
    void process(int&string_length,int&key,int&user_choice,int&key2)
    {
    
       switch(choice)
       {
       	case 1:
    
          	clrscr();
    			cout<<"Enter something you want to encrypt: "<<endl;
             cin.ignore(80, '\n');
    			cin.get(user, 256);
    			cin.ignore(80, '\n');
       		string_length=strlen(user);
       		cout<<"\nEnter key: ";
       		cin>>key;
     				for(int x=0; x<string_length; x++)
                	{
       					user[x]=user[x]+key;
    					}
    
       				key2=123456789;
    
       			for(int f=0; f<string_length; f++)
       				{
       					user[f]=user[f]+key2;
       				}
    
    						cout<<"\nEncrypted: "<<endl;
    						cout<<user<<endl;
                      clrscr();
    
             cout<<"Would you like to decrypt it?"<<endl;
          	cout<<"1 = yes / 0 = no"<<endl;
             cin >>user_choice;
    
    
    
       		if (user_choice==1)
       		{
             	key=0;
    
       			cout<<endl;
                cout<<"Enter the key: ";
       			cin>>key;
    
           		for(int x=0; x<string_length;x++)
                	{
                   user[x]=user[x]-key;
             		}
    
             	for(int f=0; f<string_length; f++)
       				{
             		user[f]=user[f]-key2;
       				}
    
                   cout<<"\nDecrypted: \n";
          			cout<<user;
          			cout<<endl;
    
    			}
    
       		else
       		{
             clrscr();
             cout<<"Thanks for trying my program" <<endl;
             }
             break;
    
          case 2:
    
              clrscr()
    
             cout<<"Enter The Input File(C:\example.txt): ";gets(input_file);fflush(stdin);
             cout<<input_file<<endl;
            
       }
    
    }
    Also any suggestions on how to improve the code would cool

    Thanks alot
    Last edited by quiksilver9531; 02-17-2002 at 11:04 AM.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Move your variable declaration out of the switch. I always declare all of my vvariables at the top of each function, then you know where they are. Some compilers allow you to do other things, when you run up against one that doesn't, and everything is at the top, no problem, as you've found out, do it your way - bang.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    cout<<"Enter The Input File(C:\example.txt): ";gets(input_file);fflush(stdin);

    Should be either

    cout<<"Enter The Input File(C:/example.txt): ";gets(input_file);fflush(stdin);

    or

    cout<<"Enter The Input File(C:\\example.txt): ";gets(input_file);fflush(stdin);

  4. #4
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    I fixed the prob....but since I am fairly new at this, im not sure how I can input the file and then encrypt whats in the file, help please


    This is what I doin now...It doesnt work but I think I'm on the right track::

    I think this only gets one character at a time...how can I make it get all the characters then from there encrypt

    Code:
       if (infile)
             {
             	infile.unsetf(ios::skipws);
    
                while(!infile.eof())
                	{
                   	infile >> file_ch;
                   }
    
                    string_length=strlen(file_ch)
    
                      	for(x=0; x<string_length; x++)
                			{
       						file_ch[x]=file_ch[x]+key;
    							}
                   infile <<file_ch;
    
             }
    
            infile.close();
    Last edited by quiksilver9531; 02-17-2002 at 03:27 PM.

Popular pages Recent additions subscribe to a feed