|
What is the value of Time[i] when the code runs?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
All your for loops are iterating from 0 to (including) sizeof(array)/sizeof(array[0]) :
for(i=0; i<=sizeof(Time)/sizeof(Time[0]); i++)
So they will access one item behind the array size with the last iteration because the max. allowed index is the number of items minus one.
Change all your loops to use < rather than <= (here for the last one):
for(i=0; i<sizeof(Time)/sizeof(Time[0]); i++)
|
|
|
|
|
@Jochenn Thanks, l made the corrections and it worked.
|
|
|
|
|
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
int main(int *op[]) {
char n;
int total;
int ct = 0, oper1 = 0, oper2 = 0, oper3 = 0, i = 0;
FILE *arq1;
arq1 = fopen("Maq1.log", "r");
while (!feof(arq1)) {
n = fgetc(arq1);
if (n == "\n") {
ct++;
}
}
op = (int*) calloc(ct, sizeof(int));
if(op==0) {
printf("Não houve memoria alocada!\n");
return 0;
}
for(i=0;!feof(arq1);i++) {
fscanf(",,,%d,", &op[ct]);
}
for(i=0;i<=ct;i++) {
if(op[ct] == 1){
oper1++;
}
if(op[ct] == 2){
oper2++;
}
if(op[ct] == 3){
oper3++;
}
}
total = oper1 + oper2 + oper3;
printf("Operacao 1: %d \n Operacao 2: %d \n Operacao 3: %d \n Total: %d \n", oper1, oper2, oper3, total);
fclose(arq1);
return 0;
}
Can you guys help me on this Code? I have no idea of what is happening, but when I run the code he prints
Operacao 1: 0
Operacao 2: 0
Operacao 3: 0
Total: 0 Sorry for the bad english!
modified 13-Jan-16 15:05pm.
|
|
|
|
|
Have you tried stepping through the code using a debugger?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
gcc -c "trabalho.c" -o "trabalho.o"
trabalho.c: In function ‘main’:
trabalho.c:22:11: warning: comparison between pointer and integer
if (n == "\n") {
^
trabalho.c:35:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
fscanf(arq1, ",,,%d,", op[ct]);
^
g++ -o "trabalho.o"
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 2 warning(s) (0 minute(s), 0 second(s))
Checking for existence: /home
Executing: xterm -T 'dest" (in origem)
Process terminated with status 0 (0 minute(s), 2 second(s))
When I compile the .c archive, appears just those 2 warnings
|
|
|
|
|
The three if() conditions in your for() loop are always looking at op[ct] rather than op[i] . I do not think that is your intent.
A date with the debugger, as jeron1 suggested, would have unveiled that.
"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
|
|
|
|
|
Thank you! I didn't see that error of logic... --'
But still giving me the same answer :/
I think the error is when he count the lines on
char n;
while (!feof(arq1)) {
n = fgetc(arq1);
if (n == "\n") { ct++;
}
}
warning: assignment makes pointer from integer without a cast
and...
for(i=0;!feof(arq1);i++) {
fscanf(arq1, ",,,%d,", op[i]); }
format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]|
|
|
|
|
|
honor3us wrote: if (n == "\n") { Why are you not using:
if (n == '\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
|
|
|
|
|
Another error, sorry :/
I changed to 'n' and &op[i] and all the warnings are gonne!
The programs runs perfectly but all the values of vector op[] are == 0.
ex:
printf("%d\n%d", op[4], op[5]);
The answer still
0
0
I'm really thankful! 
|
|
|
|
|
See here.
"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
|
|
|
|
|
This has nothing to do with the solution to your problem, as DavidCrow has already given you a clue. I'm just wondering if your compiler isn't giving you any warnings about your definition of main().
int main(int *op[]) definitely isn't one of the normal ways to define main() Normally it would be one of
int main()
int main(void)
int main(int argc, char *argv[])
int main(int argc, char **argv)
On some systems you might also be able to use int main(int argc, char *arg[], char *envp[])
Note that as a function parameter char *arg[] and char **arg are equivalent.
You also have a type issues with op . It's declared as int *op[] , which is an array of pointer to int, but you are using it as a pointer to int. What you probably want to do is:
int main(void)
{
...
int *op;
op = (int*)calloc(ct, sizeof *op);
...
}
If you're wondering why I used sizeof *op as the second argument to calloc() , consider what might happen if you decide you want to change op from an int to a long , and what might happen if you forget that you need change the call to calloc() too.
|
|
|
|
|
Thank you! You help me a lot!
But the compiler still having problem to count lines and fscanf the data I need, I don't have a clue what is going on--'
|
|
|
|
|
You might want to look at this section of the code ... there's definitely an issue here:
for( i=0; i<=ct; i++) { if(op[ct] == 1){
oper1++;
}
Also, you are accessing past the end of the array op[] -- what should the end condition for the loop be?
|
|
|
|
|
The program is working fine now, just needed to close the file and open again to read the data.
Ty for the help everyone!
|
|
|
|
|
honor3us wrote: ...just needed to close the file and open again... Or call rewind() .
"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
|
|
|
|
|
can i typdef one class into two classes . I should segregate one class into two classes.
if i do following its giving error.
typedef cxclass1 cxclass;
typedef cxclass2 cxclass;
Please let me know.
|
|
|
|
|
|
Are Foundation Classes supported in the latest version of Visual Studio 2016 (or whatever version of Visual Studio is currently being published?
Thanks,
Barry
|
|
|
|
|
|
I have added a menu with submenus to my MFC frame program. I also have a child view window instantiated within a main frame.
In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar.
However, when i want to disable some submenu I cannot do it.
I already have the following functions:
bool CMViewOption1Enabled;
.
.
.
.
.
ON_COMMAND(CM_VIEW_OPTION1, CmViewOption1)
ON_COMMAND(CM_VIEW_OPTION2, CmViewOption2)
ON_UPDATE_COMMAND_UI(CM_VIEW_OPTION1, CmEnableViewOption1)
.
.
.
.
.
.
.
void ChildView::CmViewOption1()
{
MessageBox("View Option1", "MENU", MB_OK | MB_ICONEXCLAMATION);
CMViewOption1Enabled = false;
}
void ChildView::CmEnableViewOption1(CCmdUI *ptrenabler)
{
ptrenabler->Enable(CMViewOption1Enabled);
}
void ChildView::CmViewOption2()
{
MessageBox("View Option2", "MENU", MB_OK | MB_ICONEXCLAMATION);
}
So, i right click inside my child view i see Option1, Option2, and when i click on either i can see message window popping up, meaning that they work.
However, if you noticed, when i click Option 1, it must disable this same option in menu (it must be grayed out) but that does not happen.
What is the proper way to get it to work?
to give additonal details, when i right click and select Option 1, i do see message about it, and as i mentioned above I do not see it grayed out, however when i click it again, no message appears, which means (i assume) that menu is already disabled, but not grayed out..so how to permanently disable/gray it out?
modified 11-Jan-16 10:44am.
|
|
|
|
|
You should use your debugger to trap when the call to CmEnableViewOption1 occurs. Or move the message box there; that is the code you need to validate.
|
|
|
|
|
ok so here is a thing,
the CmEnableViewOption1 occurs only when i do View-> from top bar menu, there i see Option 1 and Option2 popping down.
However, when i mouse right click from inside ChildView window i do see |Option1,2 menu popping up but inside debugger it does not enter the CmEnableViewOption1, meaning that this function does not run.
In that case my question would be, why when i click directly inside top frame menu that function is called, but when i right click from child view window and properly getting that menu and can even run its commands, but CmEnableViewOption1 is not executed?
|
|
|
|
|
Have you checked that your ON_UPDATE_COMMAND_UI in the FrameWnd class gets called? It's a long time since I used MFC but I do not recall ever seeing this problem. Take a look at the area where you create and display the popup menu.
|
|
|
|
|
Member 11203277 wrote: In the child window i can successfully do a MouseRight click and popup of some menu with its submenus from the main menu bar.
So you are using a popup (context) menu here and not the menu of the main frame (only the menu items / resources are re-used).
Then you should handle enabling/disabling when opening the popup menu and pass AfxGetMainWnd() as the owner window (see code comments):
CMenu *pMenu = new CMenu;
pMenu->LoadMenu(IDR_OF_MENU);
CMenu *pPopupMenu = pMenu->GetSubMenu(0);
pPopupMenu->EnableMenuItem(CM_VIEW_OPTION1, CMViewOption1Enabled ? 0 : MF_GRAYED);
pPopupMenu->TrackPopupMenuEx(flags, x, y, AfxGetMainWnd(), NULL);
delete pMenu;
A dynamic update is not necessary here when the popup menu closes with the first click. Otherwise make pPopupMenu a member of your child view so that it can be used to change items and/or their state dynamically.
|
|
|
|