To make a function in C which does not except "\n" & EOF as input.
-
I have been practicing structures and function and while making a program that does not accept '\n' and EOF as input. Below is the program, it takes input but the compiler does not shows any error.
#include
#includetypedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
int log;
match team;scanf("%d", &log); printf("Enter goals:\\n"); scanf("%d", team.goals); fflush(stdin); team = check(); printf("Name: %s\\t\\tGoals: %d\\n", team.name, team.goals); return 0;
}
match check()
{
int c;
match temp;gets(temp.name); if((c=temp.name\[0\])==EOF && c == '\\n') { printf("Invalid entry!"); check(temp); } return(temp);
}
-
I have been practicing structures and function and while making a program that does not accept '\n' and EOF as input. Below is the program, it takes input but the compiler does not shows any error.
#include
#includetypedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
int log;
match team;scanf("%d", &log); printf("Enter goals:\\n"); scanf("%d", team.goals); fflush(stdin); team = check(); printf("Name: %s\\t\\tGoals: %d\\n", team.name, team.goals); return 0;
}
match check()
{
int c;
match temp;gets(temp.name); if((c=temp.name\[0\])==EOF && c == '\\n') { printf("Invalid entry!"); check(temp); } return(temp);
}
Tarun Jha wrote:
if((c=temp.name[0])==EOF && c == '\n')
I'm not sure how a character retrieved from
gets()
could ever be equal to -1, but that issue aside, I do not believe that the variablec
can be equal to bothEOF
(or any other value) and\n
at the same time. Another issue withgets()
is buffer overflow."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I have been practicing structures and function and while making a program that does not accept '\n' and EOF as input. Below is the program, it takes input but the compiler does not shows any error.
#include
#includetypedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
int log;
match team;scanf("%d", &log); printf("Enter goals:\\n"); scanf("%d", team.goals); fflush(stdin); team = check(); printf("Name: %s\\t\\tGoals: %d\\n", team.name, team.goals); return 0;
}
match check()
{
int c;
match temp;gets(temp.name); if((c=temp.name\[0\])==EOF && c == '\\n') { printf("Invalid entry!"); check(temp); } return(temp);
}
-
I have been practicing structures and function and while making a program that does not accept '\n' and EOF as input. Below is the program, it takes input but the compiler does not shows any error.
#include
#includetypedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
int log;
match team;scanf("%d", &log); printf("Enter goals:\\n"); scanf("%d", team.goals); fflush(stdin); team = check(); printf("Name: %s\\t\\tGoals: %d\\n", team.name, team.goals); return 0;
}
match check()
{
int c;
match temp;gets(temp.name); if((c=temp.name\[0\])==EOF && c == '\\n') { printf("Invalid entry!"); check(temp); } return(temp);
}
Read what David Crow said and then realize name is a char array .. that is 8 bits You move it to an int with c=temp.name[0] but that will be a truncated 8 bit value. That is comparing it to EOF which is an int value will always fail. That is what Richard is trying to tell you. You probably want name as an array of int or wide chars. So you have 2 problems and which have both been identified and you need to deal with both. I am just making sure you realize BOTH comments are valid and address different issues.
In vino veritas
-
Read what David Crow said and then realize name is a char array .. that is 8 bits You move it to an int with c=temp.name[0] but that will be a truncated 8 bit value. That is comparing it to EOF which is an int value will always fail. That is what Richard is trying to tell you. You probably want name as an array of int or wide chars. So you have 2 problems and which have both been identified and you need to deal with both. I am just making sure you realize BOTH comments are valid and address different issues.
In vino veritas
-
Tarun Jha wrote:
if((c=temp.name[0])==EOF && c == '\n')
I'm not sure how a character retrieved from
gets()
could ever be equal to -1, but that issue aside, I do not believe that the variablec
can be equal to bothEOF
(or any other value) and\n
at the same time. Another issue withgets()
is buffer overflow."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
i searched on net but didn't got the concept of how it should be corrected. can you tell me how to correct it ?
Tarun Jha wrote:
i searched on net but didn't got the concept...
So what if the "net" ceased to exist one day? Some things you just have to learn by doing, not by searching.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Tarun Jha wrote:
i searched on net but didn't got the concept...
So what if the "net" ceased to exist one day? Some things you just have to learn by doing, not by searching.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
yes, i tried it many times but am unable to do it correctly. That's why i'am asking for correct answer.
Tarun Jha wrote:
That's why i'am asking for correct answer.
Did you try replacing logical AND with OR? Did you try changing
c
to be achar
instead of anint
? Did you set a breakpoint on theif()
test to see what the value ofname[0]
is? As the documentation states, it is neitherEOF
nor\n
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Tarun Jha wrote:
That's why i'am asking for correct answer.
Did you try replacing logical AND with OR? Did you try changing
c
to be achar
instead of anint
? Did you set a breakpoint on theif()
test to see what the value ofname[0]
is? As the documentation states, it is neitherEOF
nor\n
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
yes i did, and it still does'nt works.
#include #include typedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
match team, temp;printf("Enter goals:\\n"); scanf("%d", &team.goals); fflush(stdin); temp = check(); printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals); return 0;
}
match check()
{
char c;
match temp;printf("Name: \\n"); gets(temp.name); if((c=temp.name\[0\])==EOF || c == '\\n') { printf("Invalid entry!"); check(temp); } return(temp);
}
-
yes i did, and it still does'nt works.
#include #include typedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
match team, temp;printf("Enter goals:\\n"); scanf("%d", &team.goals); fflush(stdin); temp = check(); printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals); return 0;
}
match check()
{
char c;
match temp;printf("Name: \\n"); gets(temp.name); if((c=temp.name\[0\])==EOF || c == '\\n') { printf("Invalid entry!"); check(temp); } return(temp);
}
Tarun Jha wrote:
check(temp);
How does this compile?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Tarun Jha wrote:
check(temp);
How does this compile?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
i corrected that it still doesn't work.
#include #include typedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
match team, temp;printf("Enter goals:\\n"); scanf("%d", &team.goals); fflush(stdin); temp = check(); printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals); return 0;
}
match check()
{
char c;
match temp;printf("Name: \\n"); gets(temp.name); if((c=temp.name\[0\])==EOF || c == '\\n') { printf("Invalid entry!"); check(); } return(temp);
}
-
i corrected that it still doesn't work.
#include #include typedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
match team, temp;printf("Enter goals:\\n"); scanf("%d", &team.goals); fflush(stdin); temp = check(); printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals); return 0;
}
match check()
{
char c;
match temp;printf("Name: \\n"); gets(temp.name); if((c=temp.name\[0\])==EOF || c == '\\n') { printf("Invalid entry!"); check(); } return(temp);
}
Tarun Jha wrote:
i corrected that it still doesn't work.
My question was not meant to fix your logic error. It was a compiler error. As it stands now, if you were somehow able to detect an invalid entry, a recursive call to
check()
is made but it would not return anything to the original caller. You'll also need to address this issue."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
i corrected that it still doesn't work.
#include #include typedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
match team, temp;printf("Enter goals:\\n"); scanf("%d", &team.goals); fflush(stdin); temp = check(); printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals); return 0;
}
match check()
{
char c;
match temp;printf("Name: \\n"); gets(temp.name); if((c=temp.name\[0\])==EOF || c == '\\n') { printf("Invalid entry!"); check(); } return(temp);
}
Richard and I made sure you realized you that you cant use 'gets' ... it's a drop dead. 'gets' only returns chars .. EOF is an int (-1 usually) End-of-file - Wikipedia[^] please look at the difference between 'getc' (returns an int .. yipeee EOF safe) and 'gets' (returns char boo) Then it's not real hard
int c;
int namelen = 0; // initialize array position index to zero
do {
c = getc(stdin); // fetch the integer keyboard read value .. EOF safe
temp.name[namelen++] = (char) c; // convert the integer to a char and place in array .. increment position
} while (c !=EOF && c != '\n'); // repeat until EOF or new line
temp.name[namelen-1] = '\0'; // Make C null terminated string (overwrite last EOF or '\n')That should do what you want and I commented what each line does.
In vino veritas
-
i corrected that it still doesn't work.
#include #include typedef struct
{
char name[100];
int goals;}match;
match check();
int main()
{
match team, temp;printf("Enter goals:\\n"); scanf("%d", &team.goals); fflush(stdin); temp = check(); printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals); return 0;
}
match check()
{
char c;
match temp;printf("Name: \\n"); gets(temp.name); if((c=temp.name\[0\])==EOF || c == '\\n') { printf("Invalid entry!"); check(); } return(temp);
}