Home > Back-end >  No newline at end of file Error appears when i run the program
No newline at end of file Error appears when i run the program

Time:11-20

I am using gcc compiler through the terminal of linux run a program. The University that i in, gave us a file with tests. We should run these tests on our programs and the tests should pass. I compile my program with gcc through terminal and it doesnt come back with any errors.I run the tests, the test results are correct but it says that i failed because there is No newline at end of file

For example. The test gibes out the result: Secret The result is Secret but it says that i failed because of the error mentioned. How can i fix it?

#include <string.h>
#include <ctype.h>

int main(void)
{
        int i,j;
        char k='a', arr[5][5];
        for (i=0; i<=4; i  )
    {
         for (j=0; j<=4; j  )
           {
                  arr[i][j]= k;
                        k=    k;
                 if(k=='j')
                   k=   k;
            }
   }
   char str[74],str2[74], *p;
        fgets(str,75,stdin);
        for(i=0; i<75; i  )
          str2[i]=str[i];
        p=strtok(str,"-");
        while(p!=NULL)
        {        
          if(atoi(p)/10>4||atoi(p)>4)
           {
            printf("Out of bounds\n");
                return 0;
           }
          else if (isalpha(*p))
       {
                printf("Unable to decode\n");
            return 0;
       }
       p=strtok(NULL,"-");  
    }
        p=strtok(str2,"-");
        printf("< ");
        while(p!=NULL)
          {
          printf("%c", arr[atoi(p)/10][atoi(p)]);    
          p=strtok(NULL, "-");
      }
printf("\n");
return 0
}

enter image description here

CodePudding user response:

Not entirely sure what you're trying to do but try this fixed up version of your code. Looks like it expects input something like 1-2-3.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    int i,j;
    char k='a', arr[5][5];
    for (i=0; i<=4; i  ) {
        for (j=0; j<=4; j  ) {
            arr[i][j] = k  ;
            if(k=='j')
                k  ;
         }
    }
    char str[75],str2[75], *p;
    fgets(str,75,stdin);
    for(i=0; i<75; i  )
        str2[i]=str[i];
    while(p!=NULL) {        
        printf("p=%c\n", *p);
        if(atoi(p)/10>4||atoi(p)>4) {
            printf("Out of bounds\n");
            return 0;
        }
        else if (isalpha(*p)) {
            printf("Unable to decode\n");
            return 0;
        }
        p=strtok(NULL,"-");  
    }
    p=strtok(str2,"-");
    printf("< ");
    while(p!=NULL) {
        printf("%c", arr[atoi(p)/10][atoi(p)]);    
        p=strtok(NULL, "-");
    }
    printf("\n");
    return 0;
}

The main changes are:

  1. Adding a couple of missing #includes
  2. Error in initial population of arr
  3. Sizing of str and str2 should be 75 not 74. The setting of str2 was overwriting p

CodePudding user response:

I found the answer people.The problem wasnt the fact that i didn't put \n . I put the \n and the error still appeared.The problem was that i put the \n. Basically the error appeared because the \n was there, not because \n was missing. Thank you so much for the help and your time

  • Related