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

Searching and comparing in a structure

Status
Not open for further replies.

talidyce

Technical User
Mar 16, 2001
10
AU
Hello Folks!
I am going NUTS!!!
I just can't get the last bit of my code to do it right.
Please somebody have a look and see what I am missing?
Thank you very much in advance.

/* Assignment 4 Sorting and Searching Students Records */
// Date: 15.10.01 by Nearly there!!!!!!!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define COUNT 2
typedef struct RECORD {
int num;
char name[10];
float score;
}student;

int index, option, item;
void showmenu(void);

void dataentry(void);
void sortrec(void);
void calcavg(RECORD list[COUNT],int);
void searchncomp(RECORD list[COUNT],int);
float scoresum, avg, target;
struct RECORD list[COUNT];
void bubble(RECORD list[COUNT],int);

int main()
{
clrscr();
for(;;)
{
showmenu();

}
}

void showmenu(void)
{
printf(&quot;\tMENU:\n&quot;);
printf(&quot;\t-----\n&quot;);
printf(&quot;1. Enter 5 student records.\n&quot;);
printf(&quot;2. Display the 5 records in ascending order by record number.\n&quot;);
printf(&quot;3. Calculate class average.\n&quot;);
printf(&quot;4. Search and compare student score to average.\n&quot;);
printf(&quot;5. Exit the program.\n&quot;);

printf(&quot;Please enter a selection: \n&quot;);
scanf(&quot;%d&quot;, &option);

switch(option)
{
case 1: dataentry();
break;
case 2: sortrec();
break;
case 3: calcavg(list, COUNT);
break;
case 4: searchncomp(list, COUNT);
break;
case 5: exit(0);
break;
default: printf(&quot;Error! Try again.\n\n&quot;);
}
}

void dataentry(void)
{

for (index = 0; index < COUNT; ++index)
{
printf(&quot;Enter student number: &quot;);
scanf(&quot;%d&quot;,&list[index].num);
printf(&quot;Enter name: &quot;);
scanf(&quot;%s&quot;,&list[index].name);
printf(&quot;Enter student score: &quot;);
scanf(&quot;%f&quot;,&list[index].score);
}
printf(&quot;Press a key to continue.\n\n&quot;);
getch();
}

void sortrec(void)
{
bubble(list, COUNT);
printf(&quot;Sorted list by ascending student number is : \n&quot;);
{
for (index=0; index < COUNT; ++index)
printf(&quot;%10d&quot;, list[index].num);
printf(&quot;\n&quot;);
for (index=0; index < COUNT; ++index)
printf(&quot;%10s&quot;, list[index].name);
printf(&quot;\n&quot;);
for (index=0; index < COUNT; ++index)
printf(&quot;%10.2f&quot;, list[index].score);
}
printf(&quot;\nPress a key to continue.\n\n&quot;);
getch();
}

void bubble(RECORD list[], int n)
{
int i, pass, sorted;
RECORD temp;

clrscr();
pass = 1;
do{
sorted = 1;
for(i=0; i<n - pass; ++i)
{
if (list.num > list[i+1].num)
{
temp = list;
list = list[i+1];
list[i+1] = temp;
sorted = 0;
}
}
++pass;
} while(!sorted);
}

void calcavg(RECORD list[COUNT], int)
{
scoresum = avg = 0;

for (index=0; index < COUNT; ++index)
scoresum = scoresum + list[index].score;
avg = scoresum / COUNT;
printf(&quot;\nAverage is %.2f\n&quot;, avg);
printf(&quot;Press a key to continue.\n\n&quot;);
getch();
}

void searchncomp(RECORD list[COUNT], int)
{
item = 0;
index = 0;
printf(&quot;Please enter student number for search 'n compare: \n&quot;);
scanf(&quot;%d&quot;, &item);

do
{
if(item == (list[index].num))
{
printf(&quot;Score is %.2f. &quot;, list[index].score);

if(list[index].score > avg)
printf(&quot;Above average. Press a key to continue\n&quot;);
else
printf(&quot;Below average. Press a key to continue\n&quot;);
}
else
printf(&quot;Error! Record not found. Press a key to continue.\n&quot;);
index++;
}
while (index > COUNT);
getch();
}
 
Hello RPet!
Thanks for your quick reply. The following last bit can only find the first record. When I enter any other one it returns the error message. I think it's got to do with the braces but I just can't get around it.
Everything else runs OK.
*************************************************
void searchncomp(RECORD list[COUNT], int)
{
item = 0;
index = 0;
printf(&quot;Please enter student number for search 'n compare: \n&quot;);
scanf(&quot;%d&quot;, &item);

do
{
if(item == (list[index].num))
{
printf(&quot;Score is %.2f. &quot;, list[index].score);

if(list[index].score > avg)
printf(&quot;Above average. Press a key to continue\n&quot;);
else
printf(&quot;Below average. Press a key to continue\n&quot;);
}
else
printf(&quot;Error! Record not found. Press a key to continue.\n&quot;);
index++;
}
while (index > COUNT);
getch();
}

 
I think your &quot;if else&quot; logic is not correct. Whenever the &quot;item&quot; is not equal to list[index].num, your code will go to the &quot;else&quot; statement. Your should print out the error message after you search the whole list and does not find the one.
 
hi
atleast the following probs are easily visible ...
1. function : dataEntry
line: scanf(&quot;%s&quot;,&list[index].name);
shd be : scanf(&quot;%s&quot;,list[index].name);
or : scanf(&quot;%s&quot;,&(list[index].name[0]));
scanf expects an address. list[].name is the base address and so is &list[].name[0].
however &list[].name is incorrect in the current context.

2. No error but ...
typedef struct RECORD {
since u define a typedef a better practice wud be
typedef struct record_tag
{
...
} RECORD ;
RECORD list[10] ;
if even after a typedef u r using a struct RECORD , then the whole purpose of abstraction provided by typedef is lost !

3. in function bubble
ur definition
RECORD temp ;
semantically wrong. I suggest that some reading of C grammer be persued b4 writing more programs!
wat u require to do is ...
either
student temp ;
or
struct RECORD temp ;
as the typedef has name student, and not RECORD. RECORD is still a tag name !
this must be giving u a syntax error with some syggestive error mesg i think ! :)

4. fn : bubble ...
if (list.num > list[i+1].num)
{
temp = list;
list = list[i+1];
list[i+1] = temp;
sorted = 0;
}
list is an array ! usage list.num shd be replaced by list[IndeX].num
so bubble needs to be modified here ! only a small change is reqd though !

5. ur fn prototype ...
void bubble(RECORD list[], int n)
again struct RECORD or student, in place of RECORD.
and this holds for all the other such occurences.
however if u want to make the least changes. change ur struct definition like
typedef struct student {...
} RECORD ;
actually this is wat u intended i think !

6. fn : searchncomp
while (index > COUNT);
shd it not be :
(index < COUNT ) ;

well, this shd curb a lot of errors :) !
however I wud suggest a limited use of global vars if its possible ! do revert in case of any further query ... and lemme know if it helped at all :) !

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top