• C

Problem with my functions

Hi

I have a program with 6 functions doing various things.  I am having problems with three of them.

Function 1 i want it to display 20 lines at a time from file DT249.txt when the 20 lines have been displayed the person is asked if they wish to continue to display the next 20 lines c for continue and q for quit and this should continue until all line have been displayed or if q is pressed.  Also the person is asked at the beginning if they want to displat the line number.  I have attempted to do this but it doesnt work

Function 4 A person is suppose to enter a file name and the function is suppose to encrypt this file name using Caesar Cypher.

Function 5 this function should decrypt the file encrypted in function 4 the person should input a file name and the function will decrypt (plaintext) the file name.

Please help i have written the functions but they dont work and my compiler wont tell me whats wrong.  
Thanks
Natasha
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARACTERS 81
#include <string.h>
void encrypt(const char plain_text[],
             const int shift,char encrypted_text[]);
/* Purpose:    encrypt a string using shift cypher
   Parameters: The original plain text,
               The shift (1-25),
               The resulting encrypted text
*/
int get_encryption_key();
/* Purpose: Read the key (shift) from the keyboard.
            The key must be in the rqange 1-25.
   Return : The shif(key) - an integer value in range 1-25
*/
#define SIZE 20
int main();
{
  char plain_text[20+1];
  char encrypted_text[20+1];
  int shift;

  printf("Enter alphabetic string: ");
  scanf("%s",plain_text);

  shift=get_encryption_key();

  encrypt(plain_text,shift,encrypted_text);

  printf("Plain Text:%s\n", plain_text);
  printf("Encrypted Text:%s\n", encrypted_text);

int option;
void display_file ();
void show_one_line();
void last_n_lines();
void encrypt();
void decrypt();
void text_analysis();


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:
				encrypt();
				break;
            case 5:
				decrypt();
				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, cont_line, 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");
	    scanf("%c ,%c",Y,N );
            if (show_no = N)
            {
            for (line_number=1; line_number<=20; line_number++)

			printf( "%s",  one_line ) ;

            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=21; line_number<=40; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=41; line_number<=60; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=61; line_number<=80; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=81; line_number<=100; line_number++)

			printf( "%s",  one_line ) ;
            }
            }
            else
            {
            for (line_number=1; line_number<=20; line_number++)

			printf( "%s",  one_line ) ;
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
            if (line_number==0) getchar();
            line_number ++;

            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=21; line_number<=40; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=41; line_number<=60; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=61; line_number<=80; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line = C)
                    for (line_number=81; line_number<=100; line_number++)

			printf( "%s",  one_line ) ;
            }
            }


		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(const char plain_text[],
             const int shift,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'+shift )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      encrypted_text[i]  = (ch -'a'+ shift )%26+'a' ;
    else  /* all other characters are not encrypted */
      encrypted_text[i] =ch ;
  }
  encrypted_text[i] = '\0';
}

int get_encryption_key()
{
  int shift;
  printf("\nEnter encryption key (1-25)");
  do
    scanf("%d",&shift);
  while (shift<1 || shift>25);

  return shift;
}

//Function 5 to decrypt a text file using the Caesar Cypher//
void decrypt(void);
{
  char ch;
  int i=0;
  int len=strlen(encrypted_text);
  for (i=0;i<len;i++)
  {
    ch=plain_text[i];
    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      plain_text[i]  = (ch +'A'-shift )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      plain_text[i]  = (ch +'a'- shift )%26+'a' ;
    else  /* all other characters are not encrypted */
      plain_text[i] =ch ;
  }
  plain_text[i] = '\0';
}

int get_encryption_key()
{
  int shift;
  printf("\nEnter encryption key (1-25)");
  do
    scanf("%d",&shift);
  while (shift<1 || shift>25);

  return shift;
}


//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.

ZoppoCommented:
Hi NatashaMac,

I guess some 'scanf' like in line 118 is wrong:
>           scanf("%c ,%c",Y,N );
This tries to scan to ,-seperated chars, but you only want the user to enter one - I would do it somehow like this:

> printf( "do you want to display the line number? type Y for yes or N for No: " );
> while ( show_no != 'N' && show_no != 'Y' )
> {
>  scanf( "%c", &show_no );
> }

This ensures 'show_no' is either 'N' or 'Y' - later on you can ask if numbers should be shown using:
> if ( show_no == 'N' )

Further I found lot of 'if's are wrong, i.e.:

> if (cont_line = C)

This doesn't compare 'cont_line' with 'C' but assigns 'C' to 'cont_line'. To do what you intend you need to use a '==' instead of a '=', i.e.

> if (cont_line == C)

Maybe you should first solve these problems and then tell us what problems still are left.

ZOPPO
 
 
NatashaMacAuthor Commented:
Hi
Thanks for answering i made those changes and ran it and  when i enter Y i get rubbish printed and then all the printf questions when i enter n nothing happens?
ZoppoCommented:
Well, as I wrote it the 'scanf' only accepts upper-case 'Y' and 'N'.

You can modify this to accept lower case too like this:

> printf( "do you want to display the line number? type Y for yes or N for No: " );
> while ( show_no != 'N' && show_no != 'Y' )
> {
>  scanf( "%c", &show_no );
>  show_no = toupper( show_no );
> }

>> i get rubbish printed and then all the printf questions
What do you mean with this?

ZOPPO
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

NatashaMacAuthor Commented:
Hi

I did that and am getting an error saying toupper cant be used as a function but im not trying to use it as a function i have added the edited code

 get rubbish printed and then all the printf questions
What do you mean with this?
i get this


do you want to display the line number? type Y for yes or N for No: Y
AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAA
AAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAA
AAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#AAAAAAAAF¦#do you want to conti
nue? type C for yes or Q for Nodo you want to continue? type C for yes or Q for
Nodo you want to continue? type C for yes or Q for Nodo you want to continue? ty
pe C for yes or Q for No
Process returned 0 (0x0)   execution time : 2.687 s
Press any key to continue.


tHANKS
nATASHA
#include <stdio.h>
#define MAX_CHARACTERS 81
main()
{
	FILE* fp_in ;
	char one_line[ MAX_CHARACTERS ] ;
	char show_no, cont_line, Y, N, C, Q;/* Array used to store line */
	int line_number=1;
	int toupper;


	/* 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: " );
        while ( show_no != 'N' && show_no != 'Y' )
        {
        scanf( "%c", &show_no );
        show_no = toupper( show_no );
        }


            if (show_no == N)
            {
            for (line_number=1; line_number<=20; line_number++)

			printf( "%s",  one_line ) ;

            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=21; line_number<=40; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=41; line_number<=60; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=61; line_number<=80; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=81; line_number<=100; line_number++)

			printf( "%s",  one_line ) ;
            }
            }
            else
            {
            for (line_number=1; line_number<=20; line_number++)

			printf( "%s",  one_line ) ;
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
            if (line_number==0) getchar();
            line_number ++;

            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=21; line_number<=40; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=41; line_number<=60; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=61; line_number<=80; line_number++)

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == C)
                    for (line_number=81; line_number<=100; line_number++)

			printf( "%s",  one_line ) ;
            }
            }


		fclose( fp_in ) ;
	}
}

Open in new window

pkadianCommented:
Hi ,
Beacuse you are displaying un initialized variable printf( "%s",  one_line ) ;
You need to read the file in 'one_line' and then need to print.
There are lot's of problems in your original code.



pkadianCommented:
As you have used in following line.....
while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

The same way you need to read the line before any printf( "%s",  one_line ) ;

I was trying to clean the whole code , however there are so many issue that needs to understand what exactly you were trying.
itsmeandnobodyelseCommented:
>>>>      int toupper;

It didn't compile on my system because you defined a variable 'toupper' at line 9.

>>>>         while ( show_no != 'N' && show_no != 'Y' )

Here you were using 'show_no' though you didn't initialize it to a proper initial value.

>>>>         if (show_no == N)
Should be

   if (show_no == 'N')

>>>>                 if (cont_line == C)

You check for 'cont_line' but have not made a scanf before to read it .

>>>>                 if (cont_line == C)

should be

   if (cont_line == 'C')



ZoppoCommented:
pkadian is right.

Further you don't wait for user input with all following questions like 'printf("do you want to continue? type C for yes or Q for No");', so the user cannot enter anything and therefor your progam terminates.

BTW, about user input. Now you use 'scanf' which is a little bit problematic since the user can enter anything, it's not neccesary a single character - and the user needs to press ENTER too.

Would it be suitable for you to use 'Getch()'? This function returns after the next key press. With this I would suggest to write a function like the attached to retrieve the user input like this:

> show_no = GetOption( "do you want to display the line number? type Y for yes or N for No", "YN" );
> if ( show_no == 'Y' ) { ... } else { ... }
ot
> cont_line = GetOption( "do you want to continue? type C for yes or Q for No", "CQ" );
> if ( cont_line == 'C' ) { ... } else { ... }

ZOPPO
ZoppoCommented:
Sorry, I forgot the code ...
int IsValidChar( char c, const char* pszValidChars )
{
	int nLen, i;

	nLen = strlen( pszValidChars );

	for ( i = 0; i < nLen; i++ )
	{
		if ( c == pszValidChars[i] )
		{
			return 1;
		}
	}

	return 0;
}

char GetOption( const char* pszMessage, const char* pszValidChars )
{
	char c = 0;

	do
	{
		printf( "%s: ", pszMessage );

		c = toupper( getch() );

		printf( "\n" );
	} while ( !IsValidChar( c, pszValidChars ) );

	return c;
}

Open in new window

NatashaMacAuthor Commented:
OK
I have added the  while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
 before all printf( "%s",  one_line ) ;

What is the proper initial value for 'show_no' ?

And ZOPPO i havn't added you code yet as i dont understand it as im sure you are aware of now my c is very basic i am just learning so i need to understand what im putting in thats why there are lots of printf and scanf's and i would prefer to stick with this for now until i get more familiar with c.

So i have added the edited code now when i put in Y it prints everything am i getting any closer?

Thanks for all you help so far
Natasha
#include <stdio.h>
#define MAX_CHARACTERS 81
main()
{
	FILE* fp_in ;
	char cont_line, Y, N, C, Q;/* Array used to store line */
	int line_number=1;
	char show_no=0;
	char one_line[ MAX_CHARACTERS ] ;


	/* 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: " );
        while ( show_no != 'N' && show_no != 'Y' )
        {
        scanf( "%c", &show_no );
        }

            if (show_no == 'N')
            {
            for (line_number=1; line_number<=20; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;

            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=21; line_number<=40; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=41; line_number<=60; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=61; line_number<=80; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=81; line_number<=100; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;
            }
            }
            else
            {
            for (line_number=1; line_number<=20; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
            if (line_number==0) getchar();
            line_number ++;

            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=21; line_number<=40; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )

			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=41; line_number<=60; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=61; line_number<=80; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
			printf( "%s",  one_line ) ;
            }
            {
            printf("do you want to continue? type C for yes or Q for No");
                if (cont_line == 'C')
                    for (line_number=81; line_number<=100; line_number++)
            while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL )
			printf( "%s",  one_line ) ;
            }
            }


		fclose( fp_in ) ;
	}
}

Open in new window

ZoppoCommented:
Proper initial value for 'show_no = 0' is ok.

You still don't check for user input (i.e. with 'scanf') after those lines:
> printf("do you want to continue? type C for yes or Q for No");


NatashaMacAuthor Commented:
how do i check for this?
itsmeandnobodyelseCommented:
>>>>             printf("do you want to continue? type C for yes or Q for No");
>>>>                if (cont_line == 'C')

The above is your current code.

There is a printf which prints

   do you want to continue? type C for yes or Q for No

But you have no statement which reads the input from user (e. g. a scanf or a fgets). So the statement "if (cont_line == 'C')" is never true.

You need something like

       printf("do you want to continue? type C for yes or Q for No");
       scanf("%c", &cont_line);
       if (cont_line == 'C')  // now the check makes sense


Why do you repeat the statements beginning with 'do you want to continue ...' three times??

And what is the loop with fgets for?






NatashaMacAuthor Commented:
Hi

I am trying to get back to the very beginning and do it one by one so i have added the code to just print the first 20 lines in the file, which still doesnt work can someone have a look?

Thanks
Natasha
/* Program to read 20 line2 at a time from a file,
    assuming no line is more than 80 characters long. */
#include <stdio.h>
#define MAX_CHARACTERS 81
main()
{
 	FILE* fp_in ;
	char one_line[ MAX_CHARACTERS ] ;/* Array used to store line */
	int line_count;


	/* Open the file for reading */
	if ( (fp_in=fopen("DT249.txt", "r") ) == NULL )
		puts( "Error in opening DT249.txt" ) ;
	else
	{
		//printf("enter the line no 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_count=0; line_count<20; line_count++)
				printf( "%d.%s", line_count, one_line ) ;
			//if (line_number==line_count)
			//line_count ++;
		}
		fclose( fp_in ) ;
	}
}

Open in new window

itsmeandnobodyelseCommented:
>>>> if (line_count=0; line_count<20; line_count++)
You mixed up a for statement with an if statement. Do


int line_count = 0;
while( fgets( one_line, MAX_CHARACTERS, fp_in )!=NULL  && ++line_count <= 20)
{
    printf( "%d.%s", line_count, one_line ) ;
}
pkadianCommented:
Hey I am not sure what exactly you might be trying in above code. Hoever the right version is following.
I have just modified it from your yesterday version.
Please feel free to ask , if somthing is not working.
//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 ) ;
	}
}

Open in new window

pkadianCommented:
The code which I have published is of considering you as newbie.So it is in your version and simplest way which you can understand better.
NatashaMacAuthor Commented:
Hi

Thanks for that i have added it to the rest of the function but i am getting a error saying
error: expected unqualified-id before '{' token|
||=== Build finished: 1 errors, 0 warnings ===|
I cant figure out what is wrong, can you?

This is at the first curly bracket

Natasha
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARACTERS 81
#include <string.h>
#define SIZE 20
int main();

{
int option;
void display_file ();
void show_one_line();
void last_n_lines();
void encrypt();
void decrypt();
void text_analysis();


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:
				encrypt();
				break;
            case 5:
				decrypt();
				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 line no 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 ) ;
	}


}

Open in new window

NatashaMacAuthor Commented:
Line 8
pkadianCommented:
Take Following....Full Code ,Compilation error free.

#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARACTERS 81
#include <string.h>
void encrypt(const char plain_text[],const int shift,char encrypted_text[]);
/* Purpose:    encrypt a string using shift cypher
   Parameters: The original plain text,
               The shift (1-25),
               The resulting encrypted text
*/
int get_encryption_key();
/* Purpose: Read the key (shift) from the keyboard.
            The key must be in the rqange 1-25.
   Return : The shif(key) - an integer value in range 1-25
*/

void display_file();
void show_one_line();
void last_n_lines();
void decrypt();
void text_analysis();
int menu();

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


int option;


int main()
{
	  /*printf("Enter alphabetic string: ");
      scanf("%s",plain_text);

		shift=get_encryption_key();

		encrypt(plain_text,shift,encrypted_text);

		printf("Plain Text:%s\n", plain_text);
		printf("Encrypted Text:%s\n", encrypted_text);*/

	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:
				encrypt(plain_text,5,encrypted_text);
				break;
            case 5:
				decrypt();
				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(const char plain_text[],const int shift,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'+shift )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      encrypted_text[i]  = (ch -'a'+ shift )%26+'a' ;
    else  /* all other characters are not encrypted */
      encrypted_text[i] =ch ;
  }
  encrypted_text[i] = '\0';
}

int get_encryption_key()
{
  int shift;
  printf("\nEnter encryption key (1-25)");
  do
  {
    scanf("%d",&shift);
  }while (shift<1 || shift>25);

  return shift;
}

//Function 5 to decrypt a text file using the Caesar Cypher//
void decrypt(void)
{
  char ch;
  int i=0;
  int len=strlen(encrypted_text);
  for (i=0;i<len;i++)
  {
    ch=plain_text[i];
    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      plain_text[i]  = (ch +'A'-shift )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      plain_text[i]  = (ch +'a'- shift )%26+'a' ;
    else  /* all other characters are not encrypted */
      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

pkadianCommented:
The Above code is all error free with latest display_file()..as I have given earlier.
Please go , read , compare and understand what all was wrong in your coding.
Still may be some issues in few methods , As I ahve not tested all. I hope you will learn and able to solve further issues , if Won't please feel free to ask.
pkadianCommented:
Encrypt & Dycrypt were also not doing anything...Rectified , please take following as full code.

#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARACTERS 81
#include <string.h>
void encrypt(const char plain_text[],const int shift,char encrypted_text[]);
/* Purpose:    encrypt a string using shift cypher
   Parameters: The original plain text,
               The shift (1-25),
               The resulting encrypted text
*/
int get_encryption_key();
/* Purpose: Read the key (shift) from the keyboard.
            The key must be in the rqange 1-25.
   Return : The shif(key) - an integer value in range 1-25
*/

void display_file();
void show_one_line();
void last_n_lines();
void decrypt();
void text_analysis();
int menu();

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


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 alphabetic string: ");
				scanf("%s",plain_text);
				shift=get_encryption_key();
				encrypt(plain_text,shift,encrypted_text);
				printf("Plain Text:%s\n", plain_text);
				printf("Encrypted Text:%s\n", encrypted_text);
				break;
				}
				
            case 5:
				{
					decrypt();
					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(const char plain_text[],const int shift,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'+shift )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      encrypted_text[i]  = (ch -'a'+ shift )%26+'a' ;
    else  /* all other characters are not encrypted */
      encrypted_text[i] =ch ;
  }
  encrypted_text[i] = '\0';
}

int get_encryption_key()
{
  int shift;
  printf("\nEnter encryption key (1-25)");
  do
  {
    scanf("%d",&shift);
  }while (shift<1 || shift>25);

  return shift;
}

//Function 5 to decrypt a text file using the Caesar Cypher//
void decrypt(void)
{
  char ch;
  int i=0;
  int len=strlen(encrypted_text);
  for (i=0;i<len;i++)
  {
    ch=plain_text[i];
    if (ch<='Z' && ch>='A')  /* shift upper case letters */
      plain_text[i]  = (ch +'A'-shift )%26+'A' ;
    else if (ch<='z' && ch>='a')  /* shift lower case letters */
      plain_text[i]  = (ch +'a'- shift )%26+'a' ;
    else  /* all other characters are not encrypted */
      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

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
pkadianCommented:
In Compilation errors , you were having 2 main() , one with ending bracket one without...
C can not have 2 main method.
few methods were redefine and few were  in complete in calls.
and other small small issues like ; and all.
I hope now you mus be able to reun and understand your code as I ahve not done anything very advance level or not even chnaged the basic structure of code and methods.
itsmeandnobodyelseCommented:
>>>> Take Following....Full Code ,Compilation error free.

@pkadian: the above question most surely is an academic assignment where we experts can help but cannot make the homework.

Though it is more work, you therefore must explain each single error in detail rather than posting full code.

Such doing would help Natasha more than that what you do now and what is against the EE rules which you have agreed to.
pkadianCommented:
Thanks itsmeandnobodyelse,
I will keep it in mind , However I have tried just basic problems solution.
As Most of the errors were because of either copy-paste or some typo mistakes.
Anyway I am really sorry as it was not intentional to go against some rules.

Rgrds,
Pardeep Kadian
NatashaMacAuthor Commented:
Hi

Thank you very much for all your help.  I have looked through the code that you posted pkadian and i can see where i went wrong when comparing it to my own so i have learnt a lot for your posting and also from others.  

Thanks
itsmeandnobodyelseCommented:
>>>>  I have looked through the code that you posted pkadian and i can see where i went wrong when comparing it to my own

I much appreciate you doing so cause copy-paste leads to errors like the duplicate main which were unrelated to that you actually want to achieve.

Regards, Alex
NatashaMacAuthor Commented:
Thanks for all you help
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.