Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in the code...of course I can't find it!

Status
Not open for further replies.

Taxidriver

Programmer
Jan 15, 2002
79
IT
I've written a code that should accomplish this task:
There are 10 sellers, everyone has a code number (1-10).
The program accepts in input the code of the seller and how much the seller has earned, then it calculates the bonus that the company will pay to each seller according to their earnings. I used a pointer matrix to keep these information:
seller number, earnings, bonus.

The code works but for one thing, the compiler signalls errors in the first and second "else if". If I put the first "else if" between comments brackets the code works. But I really can't see the mistake! I'm sure it is something stupid but I can't figure out by myself.

Code:
#define maxnumber 10

#include <stdio.h>
#include <alloc.h>
#include <string.h>

void main()
{
   int *seller[maxnumber], i, j, vendita, codice;
   char numero[50];

   /* initialization and pointer allocation  */
   vendita=0;
   for (i=0; i <= maxnumber; ++i)
     { seller[i]=malloc(6); 
       *seller[i]=0; 
       *(seller[i]+1)=0;
       *(seller[i]+2)=0; }

   /* data introduction */
   printf(&quot;\nIntroduce seller code\n&quot;);
   while (strcmp(gets(numero),&quot;z&quot;) !=0) 
     {  
        codice=atoi(numero);
        *seller[codice]=codice; 
        printf(&quot;\nIntroduce earning, dollars:\n&quot;);
	scanf(&quot;%10d&quot;, &vendita);
        *(seller[codice]+1)+=vendita; 

        /* bonus calculating */
	if (*(seller[codice]+1) < 100 )
	  *(seller[codice]+2)= *(seller[codice]+1)*5/100;

	 else if ((100 <= *(seller[codice]+1) && 
          (*(seller[codice]+1) <= 200))
	  *(seller[codice]+2)= *(seller[codice]+1)*7.5/100;

	 else if ( *(seller[codice]+1) > 200)
	   *(seller[codice]+2) = *(seller[codice]+1)*10/100;
	    
	 printf(&quot;\nIntroduce seller code\n&quot;);
	 gets(numero);

      }

      /* results */

      printf(&quot;\n Seller       earning          bonus&quot;);
      for (i=0 ; i  < maxnumber; ++i)
	   printf(&quot;\n  %3d       %10d      %10d&quot;, i+1, *(seller[i]+1), *(seller[i]+2));

 }
 
Ugh, another problem has arisen, folks.
Again about the code I have posted above. It appears that when I introduce data regarding the salesman nr 10, I get &quot;strange numbers&quot; in *(seller[10]+1) and *(seller[10]+2).
I tried to change the first declarations with
*(seller[i+1])=0
*(seller[i+1]+1)=0
*(seller[i+1]+2)=0
so that *seller[10] can be defined properly but this doesn't work. *(seller[10]+1) remains 0 until the program reads the value of the earnings (I called it &quot;vendita&quot;), then it changes suddenly. The problem can be solved by defining the constant maxnumber=11 but I would like to know how to deal with this problem without using this &quot;trick&quot;.
Thaaaanks in advance.
 
Never mind, just one minute after I posted the last post the solution came to my mind. I redeclared
Code:
*seller[i]=0
and so on and simply changed the line

codice=atoi(number)

into

codice=atoi(number)-1

The question in the first post is still open though.
 
hi,

Compile this code, i have pointed out where you made a
bit of silly mistake in the else if.


#define maxnumber 10

#include <stdio.h>
#include <malloc.h>
#include <string.h>

void main()
{
int *seller[maxnumber], i, j, vendita, codice;
char numero[50];

/* initialization and pointer allocation */
vendita=0;
for (i=0; i <= maxnumber; ++i)
{
seller=(int*)malloc(6);
*seller=0;
*(seller+1)=0;
*(seller+2)=0;
}

/* data introduction */
printf(&quot;\nIntroduce seller code\n&quot;);
while (strcmp(gets(numero),&quot;z&quot;) !=0)
{
codice=atoi(numero)-1;//this is the other change
*seller[codice]=codice;
printf(&quot;\nIntroduce earning, dollars:\n&quot;);
scanf(&quot;%10d&quot;, &vendita);
*(seller[codice]+1)+=vendita;

/* bonus calculating */
if (*(seller[codice]+1) < 100 )
*(seller[codice]+2)= *(seller[codice]+1)*5/100;

else if (100 <= *(seller[codice]+1) && *(seller[codice]+1) <= 200)
//i made some changes here figure it out
*(seller[codice]+2)= *(seller[codice]+1)*7.5/100;

else if ( *(seller[codice]+1) > 200)
*(seller[codice]+2) = *(seller[codice]+1)*10/100;

printf(&quot;\nIntroduce seller code\n&quot;);
gets(numero);

}

/* results */

printf(&quot;\n Seller earning bonus&quot;);
for (i=0 ; i < maxnumber; ++i)
printf(&quot;\n %3d %10d %10d&quot;, i+1, *(seller+1), *(seller+2));

}

bye
yogesh
 
Hello Hawapani...
wait a moment, you eliminated the double (( in the &quot;else if&quot;? I tried it but it won't work. I think that you should put the double brackets.
 
hi,

i have compiled and run the above code and in the previous code i dont know why did the copy paste business did some
unknown truncation.

anyways the logic remains the same, compile it and
acknowlege the same
yogesh



#define maxnumber 10

#include <stdio.h>
#include <malloc.h>
#include <string.h>

void main()
{
int *seller[maxnumber], i, j, vendita, codice;
char numero[50];

/* initialization and pointer allocation */
vendita=0;
for (i=0; i <= maxnumber; ++i)
{
seller=(int*)malloc(6);
*seller=0;
*(seller+1)=0;
*(seller+2)=0;
}

/* data introduction */
printf(&quot;\nIntroduce seller code\n&quot;);
while (strcmp(gets(numero),&quot;z&quot;) !=0)
{
codice=atoi(numero)-1;//this is the other change
*seller[codice]=codice;
printf(&quot;\nIntroduce earning, dollars:\n&quot;);
scanf(&quot;%10d&quot;, &vendita);
*(seller[codice]+1)+=vendita;

/* bonus calculating */
if (*(seller[codice]+1) < 100 )
*(seller[codice]+2)= *(seller[codice]+1)*5/100;

else if (100 <= *(seller[codice]+1) && *(seller[codice]+1) <= 200)
//i made some changes here figure it out
*(seller[codice]+2)= *(seller[codice]+1)*7.5/100;

else if ( *(seller[codice]+1) > 200)
*(seller[codice]+2) = *(seller[codice]+1)*10/100;

printf(&quot;\nIntroduce seller code\n&quot;);
gets(numero);

}

/* results */

printf(&quot;\n Seller earning bonus&quot;);
for (i=0 ; i < maxnumber; ++i)
printf(&quot;\n %3d %10d %10d&quot;, i+1, *(seller+1), *(seller+2));

}
 
Hello,
it happened the same to me.
When writing a code you have to include the proper tags
I cannot write them to you, just look at the preview when posting the code , look at the notes below and you'll see.

I compiled like you wrote but I have always the same mistakes. If I understand well, you say that I should write:

Code:
 else if (100 <= *(seller[codice]+1) &&  *(seller[codice]+1) <= 200)

instead of

Code:
 else if ((100 <= *(seller[codice]+1) &&  *(seller[codice]+1) <= 200))

??
I tried but it doesn't work. In both cases the complier gives 3 errors:
1) Illegal use of pointer
2) If statement missing )
3) misplaced else.


 
sorry i didnot see the tags and hence everytime i send
you the code a thing or the other gets truncated.

this time i will use the suggestions lets see, this code
is getting compiled and is running as it should be

hope this helps
yogesh

[ignore]

#define maxnumber 10

#include <stdio.h>
#include <malloc.h>
#include <string.h>

void main()
{
int *seller[maxnumber], i, j, vendita, codice;
char numero[50];

/* initialization and pointer allocation */
vendita=0;
for (i=0; i <= maxnumber; ++i)
{ seller=(int*)malloc(6);
*seller=0;
*(seller+1)=0;
*(seller+2)=0; }

/* data introduction */
printf(&quot;\nIntroduce seller code\n&quot;);
while (strcmp(gets(numero),&quot;z&quot;) !=0)
{
codice=atoi(numero)-1;
*seller[codice]=codice;
printf(&quot;\nIntroduce earning, dollars:\n&quot;);
scanf(&quot;%10d&quot;, &vendita);
*(seller[codice]+1)+=vendita;

/* bonus calculating */
if (*(seller[codice]+1) < 100 )
*(seller[codice]+2)= *(seller[codice]+1)*5/100;

else if (100 <= *(seller[codice]+1) && *(seller[codice]+1) <= 200)
*(seller[codice]+2)= *(seller[codice]+1)*7.5/100;

else if ( *(seller[codice]+1) > 200)
*(seller[codice]+2) = *(seller[codice]+1)*10/100;

printf(&quot;\nIntroduce seller code\n&quot;);
gets(numero);

}

/* results */

printf(&quot;\n Seller earning bonus&quot;);
for (i=0 ; i < maxnumber; ++i)
printf(&quot;\n %3d %10d %10d&quot;, i+1, *(seller+1), *(seller+2));

}

[/ignore]
 
Hello Hawapani...you were right, your code works and I've also found out that my code works if I write:

Code:
else if ((100 <= *(seller[codice]+1)
)
Code:
 &&  *(seller[codice]+1) <= 200))

I missed the bracket.
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top