|
Thank you for your elaborated explanation i was able to understand and learn many new things.
|
|
|
|
|
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 <stdio.h>
#include <string.h>
typedef 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 variable c can be equal to both EOF (or any other value) and \n at the same time.
Another issue with gets() 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
modified 3-Jan-18 11:11am.
|
|
|
|
|
But in K&R it's && not ||
|
|
|
|
|
|
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
|
|
|
|
|
i searched on net but didn't got the concept of how it should be corrected.
can you tell me how to correct it ?
modified 3-Jan-18 9:47am.
|
|
|
|
|
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 a char instead of an int ? Did you set a breakpoint on the if() test to see what the value of name[0] is? As the documentation states, it is neither EOF 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
modified 3-Jan-18 11:52am.
|
|
|
|
|
yes i did, and it still does'nt works.
#include <stdio.h>
#include <string.h>
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
|
|
|
|
|
i corrected that it still doesn't work.
#include <stdio.h>
#include <string.h>
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
|
|
|
|
|
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; do {
c = getc(stdin); temp.name[namelen++] = (char) c; } while (c !=EOF && c != '\n'); temp.name[namelen-1] = '\0';
That should do what you want and I commented what each line does.
In vino veritas
modified 3-Jan-18 21:00pm.
|
|
|
|
|
I showed you where to find the answer two days ago: read the documentation.
|
|
|
|
|
I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.
<pre>#include <stdio.h>
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);
int main()
{
struct TIME startTime, stopTime, diff;
printf("Enter start time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
printf("Enter stop time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
if(stop.minutes > start.minutes){
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}
Thank you
modified 31-Dec-17 18:35pm.
|
|
|
|
|
The logic does not work because it is subtracting the stop time from the start time. It should subtract the start time from the stop time to get the difference. For example a start time of 11:33:47 and a stop time of 15:21:10 gives a difference of -4:12:37. The correct answer is 3:47:23. You can work through the logic by using some random times and working through each line,changing values as necessary and doing the subtractions.
|
|
|
|
|
It solves the following equation for X.
{stop time} + {X} = {start time}
If you walk through examples by hand you will see that the code appears to solve that equation correctly.
However like the other response I too find it counter intuitive.
As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values.
It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.
|
|
|
|
|
jschell wrote: {stop time} + {X} = {start time} Really? Only if X is negative; or the interval stops before it starts.
|
|
|
|
|
Richard MacCutchan wrote: Only if X is negative;
It is negative.
|
|
|
|
|
yes i find that problem too....
|
|
|
|
|
I have noticed that CMainFrame::ActivateFrame is not called normally, when I start the app from debugger, or from Windows Explorer.
void CMainFrame::ActivateFrame(int nCmdShow)
{
MessageBox(_T("AAAA"));
CMDIFrameWnd::ActivateFrame(nCmdShow);
}
And I have read CFrameWnd::ActivateFrame
[^]here that this method are called "Call this member function to activate and restore the frame window so that it is visible and available to the user. This member function is usually called after a non-user interface event such as a DDE, OLE, or other event that may show the frame window or its contents to the user."
Can you give an example when this method is called ? I am trying to start the my app in hidden mode, passing in this method (CMainFrame::ActivateFrame) nCmdShow = SW_HIDE;
Thank you.
|
|
|
|
|
Flaviu2 wrote: I have noticed that CMainFrame::ActivateFrame is not called normally, when I start the app from debugger, or from Windows Explorer.
void CMainFrame::ActivateFrame(int nCmdShow)
{
MessageBox(_T("AAAA"));
CMDIFrameWnd::ActivateFrame(nCmdShow);
}
Did you try to replace MessageBox call with a TRACE and check whether your text is displayed in the Output window?
|
|
|
|
|
Yes, I have:
void CMainFrame::ActivateFrame(int nCmdShow)
{
TRACE(_T("======================================================AAAA\n"));
CMDIFrameWnd::ActivateFrame(nCmdShow);
}
and there is no trace in output window ...
|
|
|
|