• C

error in my code

Hi I

I have a c program and am getting an error in line 62 saying |error: `encrypted_text' was not declared in this scope|
 i have tried to fix it but cant can some show me how to fix it.

Thanks Natasha
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARACTERS 81
#include <string.h>
void encrypt(char *plain_text);
/* Purpose:    encrypt a string using shift cypher
   Parameters: The original plain text,

*/


void display_file();
void show_one_line();
void last_n_lines();
void decrypt(char *encrypted_text);
void text_analysis();
int menu();

#define SIZE 20
  char plain_text[20+1];
  //char encrypted_text[20+1];



int option;


int main()
{


	do
	{
		option=menu();

		switch (option)
		{
			case 0:
				break;
			case 1:
				display_file();
				break;
			case 2:
				show_one_line();
				break;
			case 3:
				last_n_lines();
				break;
            case 4:
				{
				printf("Enter your file name: ");
				scanf("%s",plain_text);
				//shift=get_encryption_key();
				encrypt(plain_text);
				printf("File name:%s\n", plain_text);
				//printf("Encrypted file name:%s\n", encrypted_text);
				break;
				}

            case 5:
				{
					decrypt(*encrypted_text);
					printf("Dcrypted back Plain Text:%s\n", plain_text);
				}

				break;
            case 6:
				text_analysis();
				break;
		}
	}
    while (option!=0);
	return 0;
}

int menu()
{
    int opt;
    printf("\n");
    printf ("1. Display the file \n");
    printf ("2. Display any line in the file \n");
    printf ("3. Display the last n lines in the file \n");
    printf ("4. Encrypt the file using the Caesar Cypher \n");
    printf ("5. Decrypt the fle using the Caesar Cypher \n");
    printf ("6. Display a text analysis of the file \n");
    printf ("0. Exit\n\n");
    scanf  ("%d",&opt);
    printf("\n");
    return opt;
}

//Function 1 to display the File DT249.txt//
void display_file(void)
{
	FILE* fp_in ;
	char one_line[ MAX_CHARACTERS ] ;
	char show_no = 'N', cont_line = 'C', Y, N, C, Q;/* Array used to store line */
	int line_number=1;


	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		printf("do you want to display the line number? type Y for yes or N for No");
	    // using getchar()
		fflush(stdin);
		Y = getchar();
		if (show_no == Y || 'n' == Y)
            {
				while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
					if(feof(fp_in))
					{
						printf( "%s",  one_line ) ;
						printf( "%s",  "No more contents to read") ;
						return;
					}
					else
						printf( "%s",  one_line ) ;

			}
            else if (show_no == Y || 'y' == Y)
			{
				while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
					if(feof(fp_in))
					{
						printf( "Line Number : %d \n %s",  line_number,one_line ) ;
						printf( "%s",  "No more contents to read \n") ;
						line_number++;
						return;
					}
					else
					{
						printf( "Line Number : %d \n %s",  line_number,one_line ) ;
						line_number++;
					}

			}


		fclose( fp_in ) ;
	}
}

//Function 2 to display one line from the text//

void show_one_line(void)
{
	FILE* fp_in ;
	char one_line[ MAX_CHARACTERS ] ;/* Array used to store line */
	int line_number=1;
	int line_count=1;


	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		printf("enter the no of lines from the end you want to see ");
		scanf("%d", &line_number);
		/* Read at most MAX_CHARACTERS-1 characters from the file
		 or until a new line character (\n) is read
		 or the end of file occurs. */
		while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
		{
			if (line_number==line_count)
				printf( "%d.%s", line_count, one_line ) ;
			//if (line_number==line_count)
			line_count ++;
		}
		fclose( fp_in ) ;
	}

}

//Function 3 to display n lines from the end from the text file//
void last_n_lines(void)
{
	FILE* fp_in ;
	char n_line[ MAX_CHARACTERS ] ;/* Array used to store line */
	int line_number=0;
	int line_count=0;
	int line_print = 0;

	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		printf("enter the no of lines you want to see from the end");
		scanf("%d", &line_number);

		while( fgets( n_line, MAX_CHARACTERS, fp_in )!=NULL )
		{
			line_count ++;
		}

		if (line_count > line_number)
			line_print = line_count - line_number;

		 fseek(fp_in, 0, SEEK_SET);
		line_count = 0;
		while( fgets( n_line, MAX_CHARACTERS, fp_in )!=NULL )
		{
			if (line_count >= line_print)
				printf( "%d.%s", line_count, n_line ) ;
			line_count ++;
		}
		fclose( fp_in ) ;
	}
}


//Function 4 to encrypt a text file using the Caesar Cypher//

void encrypt( char *plain_text,char *encrypted_text)
{
  char ch;
  int i=0;
  int len=strlen(plain_text);
  for (i=0;i<len;i++)
  {
    ch=plain_text[i];
    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      encrypted_text[i]  = (ch -'A'+ 3 )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      encrypted_text[i]  = (ch -'a'+ 3 )%26+'a' ;
    else  /* all other characters are not encrypted */
      encrypted_text[i] =ch ;
  }
  encrypted_text[i] = '\0';
}



//Function 5 to decrypt a text file using the Caesar Cypher//
void decrypt(char *encrypted_text)
{
  char ch;
  int i=0;
  int len;  /* number of characters*/

  len = strlen(encrypted_text);

  for (i=0;i<len;i++)
  {

    ch=encrypted_text[i];

    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      plain_text[i]  = (ch +'A'-3 )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      plain_text[i]  = (ch +'a'-3 )%26+'a' ;
    else  /* all other characters are not decrypted */
      plain_text[i] =ch ;
  }
  plain_text[i] = '\0';
}

//Function 6 to carry out text analysis on file DT249.txt//

void text_analysis()
{

}

Open in new window

NatashaMacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Line 21 -- is that meant to be commented out?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffric\Commented:
There is a mismatch in your declaration and your definition of encrypt function. The definition (last line) is probably what you want.
void encrypt(char *plain_text);
encrypt(plain_text);
void encrypt( char *plain_text,char *encrypted_text)

Open in new window

phoffric\Commented:
You usage
       encrypt(plain_text);
probably should be
       encrypt(plain_text, encrypted_text )

Also,
    decrypt(*encrypted_text);
should be
   decrypt(encrypted_text);
since encrypted_text (after you uncomment it) is actually a constant pointer to the starting address of the encrypted_text array.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

NatashaMacAuthor Commented:
Hi

I have done as you suggested but still get the same error
c|8|error: expected initializer before "void"|
c||In function `int main()':|
c|37|error: `display_file' was not declared in this scope|
.c|50|error: `encrypted_text' was not declared in this scope|
.c|50|error: `encrypt' was not declared in this scope|
.c|58|error: `encrypted_text' was not declared in this scope|
||=== Build finished: 5 errors, 0 warnings ===|

#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARACTERS 81
#include <string.h>
void encrypt( char *plain_text,char *encrypted_text)


void display_file();
void show_one_line();
void last_n_lines();
void decrypt(char *encrypted_text);
void text_analysis();
int menu();

#define SIZE 20
  char plain_text[20+1];
  //char encrypted_text[20+1];



int option;


int main()
{


	do
	{
		option=menu();

		switch (option)
		{
			case 0:
				break;
			case 1:
				display_file();
				break;
			case 2:
				show_one_line();
				break;
			case 3:
				last_n_lines();
				break;
            case 4:
				{
				printf("Enter your file name: ");
				scanf("%s",plain_text);
				//shift=get_encryption_key();
				encrypt(plain_text, encrypted_text );
				printf("File name:%s\n", plain_text);
				printf("Encrypted file name:%s\n", encrypted_text);
				break;
				}

            case 5:
				{
					decrypt(encrypted_text);
					printf("Decrypted back Plain Text:%s\n", plain_text);
				}

				break;
            case 6:
				text_analysis();
				break;
		}
	}
    while (option!=0);
	return 0;
}

int menu()
{
    int opt;
    printf("\n");
    printf ("1. Display the file \n");
    printf ("2. Display any line in the file \n");
    printf ("3. Display the last n lines in the file \n");
    printf ("4. Encrypt the file using the Caesar Cypher \n");
    printf ("5. Decrypt the fle using the Caesar Cypher \n");
    printf ("6. Display a text analysis of the file \n");
    printf ("0. Exit\n\n");
    scanf  ("%d",&opt);
    printf("\n");
    return opt;
}

//Function 1 to display the File DT249.txt//
void display_file(void)
{
	FILE* fp_in ;
	char one_line[ MAX_CHARACTERS ] ;
	char show_no = 'N', cont_line = 'C', Y, N, C, Q;/* Array used to store line */
	int line_number=1;


	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		printf("do you want to display the line number? type Y for yes or N for No");
	    // using getchar()
		fflush(stdin);
		Y = getchar();
		if (show_no == Y || 'n' == Y)
            {
				while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
					if(feof(fp_in))
					{
						printf( "%s",  one_line ) ;
						printf( "%s",  "No more contents to read") ;
						return;
					}
					else
						printf( "%s",  one_line ) ;

			}
            else if (show_no == Y || 'y' == Y)
			{
				while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
					if(feof(fp_in))
					{
						printf( "Line Number : %d \n %s",  line_number,one_line ) ;
						printf( "%s",  "No more contents to read \n") ;
						line_number++;
						return;
					}
					else
					{
						printf( "Line Number : %d \n %s",  line_number,one_line ) ;
						line_number++;
					}

			}


		fclose( fp_in ) ;
	}
}

//Function 2 to display one line from the text//

void show_one_line(void)
{
	FILE* fp_in ;
	char one_line[ MAX_CHARACTERS ] ;/* Array used to store line */
	int line_number=1;
	int line_count=1;


	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		printf("enter the no of lines from the end you want to see ");
		scanf("%d", &line_number);
		/* Read at most MAX_CHARACTERS-1 characters from the file
		 or until a new line character (\n) is read
		 or the end of file occurs. */
		while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
		{
			if (line_number==line_count)
				printf( "%d.%s", line_count, one_line ) ;
			//if (line_number==line_count)
			line_count ++;
		}
		fclose( fp_in ) ;
	}

}

//Function 3 to display n lines from the end from the text file//
void last_n_lines(void)
{
	FILE* fp_in ;
	char n_line[ MAX_CHARACTERS ] ;/* Array used to store line */
	int line_number=0;
	int line_count=0;
	int line_print = 0;

	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		printf("enter the no of lines you want to see from the end");
		scanf("%d", &line_number);

		while( fgets( n_line, MAX_CHARACTERS, fp_in )!=NULL )
		{
			line_count ++;
		}

		if (line_count > line_number)
			line_print = line_count - line_number;

		 fseek(fp_in, 0, SEEK_SET);
		line_count = 0;
		while( fgets( n_line, MAX_CHARACTERS, fp_in )!=NULL )
		{
			if (line_count >= line_print)
				printf( "%d.%s", line_count, n_line ) ;
			line_count ++;
		}
		fclose( fp_in ) ;
	}
}


//Function 4 to encrypt a text file using the Caesar Cypher//

void encrypt( char *plain_text,char *encrypted_text)
{
  char ch;
  int i=0;
  int len=strlen(plain_text);
  for (i=0;i<len;i++)
  {
    ch=plain_text[i];
    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      encrypted_text[i]  = (ch -'A'+ 3 )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      encrypted_text[i]  = (ch -'a'+ 3 )%26+'a' ;
    else  /* all other characters are not encrypted */
      encrypted_text[i] =ch ;
  }
  encrypted_text[i] = '\0';
}



//Function 5 to decrypt a text file using the Caesar Cypher//
void decrypt(char *encrypted_text)
{
  char ch;
  int i=0;
  int len;  /* number of characters*/

  len = strlen(encrypted_text);

  for (i=0;i<len;i++)
  {

    ch=encrypted_text[i];

    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      plain_text[i]  = (ch +'A'-3 )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      plain_text[i]  = (ch +'a'-3 )%26+'a' ;
    else  /* all other characters are not decrypted */
      plain_text[i] =ch ;
  }
  plain_text[i] = '\0';
}

//Function 6 to carry out text analysis on file DT249.txt//

void text_analysis()
{

}

Open in new window

phoffric\Commented:
As evilrix mentioned, is the comment here intentional?
>>     //char encrypted_text[20+1];
phoffric\Commented:
>> void encrypt( char *plain_text,char *encrypted_text)
   need a ;
NatashaMacAuthor Commented:
thanks
evilrixSenior Software Engineer (Avast)Commented:
The actual problem in question was solved by the first post!

http:#32036400

error in line 62 saying |error: `encrypted_text' was not declared in this scope

"Line 21 -- is that meant to be commented out?"

//char encrypted_text[20+1]; //<--- this is commented out so the compiler cannot see it!
phoffric\Commented:
>> The actual problem in question was solved by the first post!
Yes, true. I didn't see the first post when I made my post. So, an adjustment of the accept is in order.
evilrixSenior Software Engineer (Avast)Commented:
Thanks phoffric.
NatashaMacAuthor Commented:
Hi

Apologies for that i clicked on the wrong post by accident and didnt realise it until now.
evilrixSenior Software Engineer (Avast)Commented:
No worries. Thanks.
phoffric\Commented:
As with most questions regarding compilation errors, often there are multiple errors that all get resolved before a question is closed. After removing the comment line 21 in original post, there were still compilation errors that needed to be addressed (since as seen the author did post follow-up questions with the intent of resolving all the compilation errors).

For this reason, to capture all the compilation errors, recommend that http:#32036826 be accepted, as well as http:#32037095, and http:#32037962, and http:#32036400
pkadianCommented:
Hi , Again you have simple and mistakes by ignorance.
First you should have ; at the end of method declaration. It should be

void encrypt( char *plain_text,char *encrypted_text);
Instead of
void encrypt( char *plain_text,char *encrypted_text)

Also as it has been mentioned that you need to un comment follwoing
//char encrypted_text[20+1];
it should be
char encrypted_text[20+1];
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.