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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strcmp 2

Status
Not open for further replies.

TFfan

Programmer
Jan 18, 2002
192
0
0
CA
Hi, here's the problem.

I have these arrays:

char name[2][25];
int salary[2][12];
int rowmax[1];
char months[11][9]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "December"}

User inputs name and salary. I need to find the month in which employee x got the maximum salary.

I know I'll need strcmp, but the logic of the problem escapes me. I've set up a variable match and char name2 and messed around a little, but without success.

while(!match){
gets (name2[25]);
if (strcmp(name2, name)==0){
...?

Am I on the right track? Any help is appreciated.
 
hi ..

your variable name is a multi dimensional array.
name[2][25].

i will give you a hint here.

if you had 10 employees, then you will declare
name[10]25]..you have also declared char name2[25] {to compare .. and that is okay}

so, to compare you will have a loop similar to the one shown below :

Code:
while (your_condition) {
  gets(name2); 
      /* dont say gets(name2[25]) ->wrong syntax */
  if (strcmp(name2,name[i]) == 0) {
     /* note name[i] -> i is an index ranging 0 to 9 */
     /* you will have a for loop or some sort for this */
     ...
     ...
  }
} /* end of while */

does this help ?

br: nagi
 
Okay, now I'm really lost. It's basically just the menu item 1 (after that Ithink I'll be able to figure out the rest). I'm unsure of how to apply the logic in C. I think this is hopeless.

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

main()
{

char name[2][25];
int salary[2][12];
char months[11][9]={&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;November&quot;, &quot;December&quot;};

//int rowtotals[2];
//int coltotals[12];
int rowmax[1];
//char colmax[25];

char name2[25];
int i,j, menu, match=0;

printf(&quot;Please enter the information for employees (type 'exit' when done).\n&quot;);

for (i=0; i<2; i++){

printf (&quot;Name :&quot;);
gets (name);

printf(&quot;\nJan :&quot;);
scanf (&quot;%d&quot;,&salary[0]);
printf(&quot;\nFeb :&quot;);
scanf (&quot;%d&quot;,&salary[1]);
printf(&quot;\nMar :&quot;);
scanf (&quot;%d&quot;,&salary[2]);
printf(&quot;\nApr :&quot;);
scanf (&quot;%d&quot;,&salary[3]);
printf(&quot;\nMay :&quot;);
scanf (&quot;%d&quot;,&salary[4]);
printf(&quot;\nJun :&quot;);
scanf (&quot;%d&quot;,&salary[5]);
printf(&quot;\nJul :&quot;);
scanf (&quot;%d&quot;,&salary[6]);
printf(&quot;\nAug :&quot;);
scanf (&quot;%d&quot;,&salary[7]);
printf(&quot;\nSep :&quot;);
scanf (&quot;%d&quot;,&salary[8]);
printf(&quot;\nNov :&quot;);
scanf (&quot;%d&quot;,&salary[9]);
printf(&quot;\nOct :&quot;);
scanf (&quot;%d&quot;,&salary[10]);
printf(&quot;\nDec :&quot;);
scanf (&quot;%d&quot;, &salary[11]);
getchar();
}

printf (&quot;\nNAME Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n&quot;);
printf (&quot;___________________________________________________________________________\n&quot;);

for (i=0; i<2; i++){
printf (&quot;\n&quot;);
printf (&quot;%s &quot;, name);

for (j=0; j<12; j++){
printf (&quot; %d &quot;, salary[j]);
}}



while (menu!=5){
printf(&quot;\n&quot;);
printf (&quot;MENU\n\n&quot;);

printf (&quot;1. Print the month in which employee x got the maximum salary.\n&quot;);
printf (&quot;2. Print the Employee name who got the maximum salary in any month.\n&quot;);
printf (&quot;3. Print the total salary earned by employee x in whole year.\n&quot;);
printf (&quot;4. Print the total salary paid to employees in month x.\n&quot;);
printf (&quot;5. Exit\n\n&quot;);

printf (&quot;Enter Choice :&quot;);
scanf (&quot;%d&quot;, &menu);

if (menu==1)
{

printf(&quot;\n\n Enter the Employee name :&quot; );

while (!match)
{
gets(name2);

for (i=0; i<2; i++)
{

if (strcmp(name2,name) == 0)
{
for (j=0; j<12; j++)
{

if (salary[k]>salary[k+1])
{
rowmax[1]=salary[k];

printf (&quot;\n\nIn the month of %s, %d got the maximum salary ($%d)&quot;, months[j], name, rowmax[1]);
}

} /*end of for*/
} /*end of if*/
} /* end of for */
} /* end of while (!match)*/
} /* end of if (menu==1) */

else if (menu==2){printf(&quot;2\n&quot;);}
else if (menu==3){printf(&quot;3\n&quot;);}
else if (menu==4){printf(&quot;4\n&quot;);}
else {/*Do nothing*/}
}


}
 
I've figured it out. Took out a piece of paper and thought about it one step at a time. Thanks just the same.
 
There are quite a few probs in ur prog
1. scanf - U hav declared a 2 dim array so scanf shud be
scanf(&quot;%d&quot;,&salary[0][0]);
scanf(&quot;%d&quot;,&salary[0][1]);
scanf(&quot;%d&quot;,&salary[0][2]); ......
scanf(&quot;%d&quot;,&salary[1][11]);

2. Also the while (!match) loop is never ending...if the name is properly compared the set the flag match to 1.

3. Also the logic used for calculating the month of maximum salary seems to be faulty.
int temp=salary[0];
for(j=0;j<11;j++)
{
if(salary[j+1]>salary[j])
temp=salary[j+1];
}
now max salary of employee i is temp

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top