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
}
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:
- Adding a couple of missing
#include
s - Error in initial population of
arr
- Sizing of
str
andstr2
should be 75 not 74. The setting ofstr2
was overwritingp
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