944,156 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3379
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 13th, 2006
0

Please help....

Expand Post »
can you please help me or give some advice of how will i make this problem:

Student database: the user will be asked to input information about students(1-20 students) and the inputs are age,year level,name,course....
the program also will be able to sort the informations ethier by descending or ascending by age or year level... did you understand it? please give me idea how to make this program...
Last edited by cezarjont; Aug 13th, 2006 at 10:34 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cezarjont is offline Offline
9 posts
since Aug 2006
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 13th, 2006
0

Re: Please help....

I use class structure for your problem.

  1. class student
  2. {
  3. int age;
  4. string year;
  5. string level
  6. string name
  7. string course
  8. };

Then you could have a sort method which sorts by either year or level.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 14th, 2006
0

Re: Please help....

thnx... but how can i make the structures elements to be sorted?

is this possible???
 
for(x = 0; x < ARRAY_SIZE; x++)
    for(y = 0; y < ARRAY_SIZE-1; y++)
      if(student.age[y] > student.age[y+1]) {
        holder = student.age[y+1];
        student.age[y+1] = student.age[y];
        student.age[y] = holder;
      }


if this is possible... how will i put it in a function...??? please help....
Last edited by cezarjont; Aug 14th, 2006 at 9:47 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cezarjont is offline Offline
9 posts
since Aug 2006
Aug 15th, 2006
0

Re: Please help....

yey... im almost done... please help me check my program.... please help me....
it shows and error of Lvalue required... i don't know how to fix it....

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define size 20
  5. struct data{
  6. char name[20];
  7. char course[6];
  8. int age;
  9. int year;
  10. } students[20];
  11. union temporary{
  12. int age;
  13. int year;
  14. char name[20];
  15. char course[6];
  16. } holder;
  17.  
  18. main()
  19. {
  20. int x,limit;
  21. int input;
  22. do
  23. {
  24. clrscr();
  25. puts("1..insert data");
  26. puts("2..show data");
  27. puts("3..show data in ascending order by age");
  28. puts("4..show data in ascending order by year");
  29. puts("5..show data in descending order by age");
  30. puts("6..show data in descending order by year");
  31. puts("9..exit");
  32. printf("input choice:"); scanf("%d",&input);
  33. switch(input)
  34. {
  35. case 1:
  36. inputdata();
  37. break;
  38. case 2:
  39. showdata();
  40. break;
  41. }
  42. }while(input!=9);
  43.  
  44. getch();
  45. return 0;
  46. }
  47.  
  48.  
  49. inputdata()
  50. {
  51. int limit,x;
  52. printf("Size of structure to make? (1-20)\t");
  53. scanf("%d",&limit);
  54. if(limit<1 || limit>size)
  55. {
  56. printf("Input Invalid!");
  57. }
  58. else
  59. {
  60. limit=limit-1;
  61. printf("\n\n\nAge Year level Course Name\n");
  62. for(x=0;x<=limit;x++)
  63. {
  64. scanf("%d",&students[x].age);
  65. flushall();
  66. scanf("%d",&students[x].year);
  67. flushall();
  68. gets(students[x].course);
  69. flushall();
  70. gets(students[x].name);
  71. flushall();
  72. }
  73. }
  74. }
  75.  
  76. showdata()
  77. {
  78. int limit;
  79. int x;
  80. printf("This are the items you inputted!");
  81. printf("\nAge Year level Course Name\n");
  82. for(x=0;x<size;x++)
  83. {
  84. printf("%d",students[x].age);
  85. printf("\t%s",students[x].year);
  86. printf("\t%s",students[x].course);
  87. printf("\t%s\n",students[x].name);
  88. }
  89. getch();
  90. }
  91. sort_asc_by_age()
  92. {
  93.  
  94. int x,y;
  95. for(x = 0; x < size; x++)
  96. for(y = 0; y < size; y++)
  97. if(students[y].age > students[y+1].age)
  98. {
  99. holder.age=students[y+1].age;
  100. holder.year=students[y+1].year;
  101. holder.name=students[y+1].name;
  102. holder.course=students[y+1].course;
  103. students[y+1].age=students[y].age;
  104. students[y+1].year=students[y].year;
  105. students[y+1].name=students[y].name;
  106. students[y+1].course=students[y].course;
  107. students[y].age=holder.age;
  108. students[y].name=holder.name;
  109. students[y].year=holder.year;
  110. students[y].course=holder.course;
  111. }
  112. for(x=0;x<size;x++)
  113. {
  114. printf("%d",students[x].age);
  115. printf("\t%s",students[x].year);
  116. printf("\t%s",students[x].course);
  117. printf("\t%s\n",students[x].name);
  118. }
  119. getch();
  120. }
  121. sort_asc_by_year()
  122. {
  123. int x,y;
  124. for(x = 0; x < size; x++)
  125. for(y = 0; y < size-1; y++)
  126. if(students[y].year > students[y+1].age)
  127. {
  128. holder.age=students[y+1].age;
  129. holder.year=students[y+1].year;
  130. holder.name=students[y+1].name;
  131. holder.course=students[y+1].course;
  132. students[y+1].age= students[y].age;
  133. students[y+1].year=students[y].year;
  134. students[y+1].name=students[y].name;
  135. students[y+1].course=students[y].course;
  136. students[y].age=holder.age;
  137. students[y].name=holder.year;
  138. students[y].year=holder.name;
  139. students[y].course=holder.course;
  140. }
  141. for(x=0;x<size;x++)
  142. {
  143. printf("%d",students[x].age);
  144. printf("\t%s",students[x].year);
  145. printf("\t%s",students[x].course);
  146. printf("\t%s\n",students[x].name);
  147. }
  148. getch();
  149. }
  150. sort__desc_by_age()
  151. {
  152.  
  153. int x,y;
  154. for(x = 0; x < size; x++)
  155. for(y = 0; y < size; y++)
  156. if(students[y].age < students[y+1].age)
  157. {
  158. holder.age=students[y+1].age;
  159. holder.year=students[y+1].year;
  160. holder.name=students[y+1].name;
  161. holder.course=students[y+1].course;
  162. students[y+1].age= students[y].age;
  163. students[y+1].year= students[y].year;
  164. students[y+1].name= students[y].name;
  165. students[y+1].course= students[y].course;
  166. students[y].age =holder.age;
  167. students[y].name =holder.year;
  168. students[y].year =holder.name;
  169. students[y].course=holder.course;
  170. }
  171. for(x=0;x<size;x++)
  172. {
  173. printf("%d",students[x].age);
  174. printf("\t%s",students[x].year);
  175. printf("\t%s",students[x].course);
  176. printf("\t%s\n",students[x].name);
  177. }
  178. getch();
  179. }
  180. sort_desc_by_year()
  181. {
  182.  
  183. int x,y;
  184. for(x = 0; x < size; x++)
  185. for(y = 0; y < size-1; y++)
  186. if(students[y].year < students[y+1].age)
  187. {
  188. holder.age=students[y+1].age;
  189. holder.year=students[y+1].year;
  190. holder.name=students[y+1].name;
  191. holder.course=students[y+1].course;
  192. students[y+1].age= students[y].age;
  193. students[y+1].year= students[y].year;
  194. students[y+1].name= students[y].name;
  195. students[y+1].course= students[y].course;
  196. students[y].age =holder.age;
  197. students[y].name =holder.year;
  198. students[y].year =holder.name;
  199. students[y].course=holder.course;
  200. }
  201. for(x=0;x<size;x++)
  202. {
  203. printf("%d",students[x].age);
  204. printf("\t%s",students[x].year);
  205. printf("\t%s",students[x].course);
  206. printf("\t%s\n",students[x].name);
  207. }
  208. getch();
  209. }
Last edited by cezarjont; Aug 15th, 2006 at 12:08 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cezarjont is offline Offline
9 posts
since Aug 2006
Aug 15th, 2006
0

Re: Please help....

Here's your lint:
  1. PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software 1985-2006
  2.  
  3. --- Module: test.c (C)
  4. test.c 25 error [Info 718] Symbol 'clrscr' undeclared, assumed to return int
  5. test.c 25 error [Info 746] call to function 'clrscr()' not made in the presence of a prototype
  6. test.c 37 error [Info 718] Symbol 'inputdata' undeclared, assumed to return int
  7. test.c 37 error [Info 746] call to function 'inputdata()' not made in the presence of a prototype
  8. test.c 40 error [Info 718] Symbol 'showdata' undeclared, assumed to return int
  9. test.c 40 error [Info 746] call to function 'showdata()' not made in the presence of a prototype
  10. test.c 42 error [Info 744] switch statement has no default
  11. test.c 45 error [Info 718] Symbol 'getch' undeclared, assumed to return int
  12. test.c 45 error [Info 746] call to function 'getch()' not made in the presence of a prototype
  13. test.c 47 error [Warning 529] Symbol 'x' (line 21) not subsequently referenced
  14. test.c 47 error [Warning 529] Symbol 'limit' (line 21) not subsequently referenced
  15. test.c 51 error [Info 745] function 'inputdata()' has no explicit type or class, int assumed
  16. test.c 51 error [Warning 532] Return mode of function 'inputdata(void)' inconsistent with line 37
  17. test.c 66 error [Warning 534] Ignoring return value of function 'flushall(void)' (compare with line 299, file c:\progra~1\borland\bcc55\include\stdio.h)
  18. test.c 68 error [Warning 534] Ignoring return value of function 'flushall(void)' (compare with line 299, file c:\progra~1\borland\bcc55\include\stdio.h)
  19. test.c 69 error [Warning 534] Ignoring return value of function 'gets(char *)' (compare with line 179, file c:\progra~1\borland\bcc55\include\stdio.h)
  20. test.c 69 error [Warning 421] Caution -- function 'gets(char *)' is considered dangerous
  21. test.c 70 error [Warning 534] Ignoring return value of function 'flushall(void)' (compare with line 299, file c:\progra~1\borland\bcc55\include\stdio.h)
  22. test.c 71 error [Warning 534] Ignoring return value of function 'gets(char *)' (compare with line 179, file c:\progra~1\borland\bcc55\include\stdio.h)
  23. test.c 71 error [Warning 421] Caution -- function 'gets(char *)' is considered dangerous
  24. test.c 72 error [Warning 534] Ignoring return value of function 'flushall(void)' (compare with line 299, file c:\progra~1\borland\bcc55\include\stdio.h)
  25. test.c 75 error [Warning 533] function 'inputdata(void)' should return a value (see line 50)
  26. test.c 78 error [Info 745] function 'showdata()' has no explicit type or class, int assumed
  27. test.c 78 error [Warning 532] Return mode of function 'showdata(void)' inconsistent with line 40
  28. test.c 86 error [Warning 560] argument no. 2 should be a pointer
  29. test.c 91 error [Warning 533] function 'showdata(void)' should return a value (see line 77)
  30. test.c 91 error [Warning 529] Symbol 'limit' (line 79) not subsequently referenced
  31. test.c 93 error [Info 745] function 'sort_asc_by_age' has no explicit type or class, int assumed
  32. test.c 98 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 98]
  33. test.c 100 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 100]
  34. test.c 101 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 101]
  35. test.c 102 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  36. test.c 102 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 102]
  37. test.c 103 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  38. test.c 103 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 103]
  39. test.c 104 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 104]
  40. test.c 105 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 105]
  41. test.c 106 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  42. test.c 106 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 106]
  43. test.c 107 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  44. test.c 107 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 97, 107]
  45. test.c 109 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  46. test.c 111 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  47. test.c 116 error [Warning 560] argument no. 2 should be a pointer
  48. test.c 121 error [Warning 533] function 'sort_asc_by_age(void)' should return a value (see line 92)
  49. test.c 123 error [Info 745] function 'sort_asc_by_year' has no explicit type or class, int assumed
  50. test.c 131 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  51. test.c 132 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  52. test.c 135 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  53. test.c 136 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  54. test.c 138 error [Error 64] Type mismatch (assignment) (char [20] = int)
  55. test.c 139 error [Error 64] Type mismatch (assignment) (int = pointer)
  56. test.c 140 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  57. test.c 145 error [Warning 560] argument no. 2 should be a pointer
  58. test.c 150 error [Warning 533] function 'sort_asc_by_year(void)' should return a value (see line 122)
  59. test.c 152 error [Info 745] function 'sort__desc_by_age' has no explicit type or class, int assumed
  60. test.c 157 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 157]
  61. test.c 159 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 159]
  62. test.c 160 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 160]
  63. test.c 161 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  64. test.c 161 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 161]
  65. test.c 162 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  66. test.c 162 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 162]
  67. test.c 163 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 163]
  68. test.c 164 error [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 164]
  69. test.c 165 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  70. test.c 165 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 165]
  71. test.c 166 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  72. test.c 166 error [Warning 662] Possible creation of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 156, 166]
  73. test.c 168 error [Error 64] Type mismatch (assignment) (char [20] = int)
  74. test.c 169 error [Error 64] Type mismatch (assignment) (int = pointer)
  75. test.c 170 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  76. test.c 175 error [Warning 560] argument no. 2 should be a pointer
  77. test.c 180 error [Warning 533] function 'sort__desc_by_age(void)' should return a value (see line 151)
  78. test.c 182 error [Info 745] function 'sort_desc_by_year' has no explicit type or class, int assumed
  79. test.c 191 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  80. test.c 192 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  81. test.c 195 error [Error 64] Type mismatch (assignment) (char [20] = char *)
  82. test.c 196 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  83. test.c 198 error [Error 64] Type mismatch (assignment) (char [20] = int)
  84. test.c 199 error [Error 64] Type mismatch (assignment) (int = pointer)
  85. test.c 200 error [Error 64] Type mismatch (assignment) (char [6] = char *)
  86. test.c 205 error [Warning 560] argument no. 2 should be a pointer
  87. test.c 210 error [Info 783] Line does not end with new-line
  88. test.c 210 error [Warning 533] function 'sort_desc_by_year(void)' should return a value (see line 181)
  89. test.c 211 error [Info 766] Header file 'c:\progra~1\borland\bcc55\include\string.h' not used in module 'test.c'
  90.  
  91. --- Global Wrap-up
  92.  
  93. test.c 45 error [Warning 526] Symbol 'getch()' (line 45, file test.c) not defined
  94. test.c 45 error [Warning 628] no argument information provided for function 'getch()' (line 45, file test.c)
  95. test.c 25 error [Warning 526] Symbol 'clrscr()' (line 25, file test.c) not defined
  96. test.c 25 error [Warning 628] no argument information provided for function 'clrscr()' (line 25, file test.c)
  97. test.c 151 error [Info 714] Symbol 'sort__desc_by_age(void)' (line 151, file test.c) not referenced
  98. test.c 92 error [Info 714] Symbol 'sort_asc_by_age(void)' (line 92, file test.c) not referenced
  99. test.c 122 error [Info 714] Symbol 'sort_asc_by_year(void)' (line 122, file test.c) not referenced
  100. test.c 181 error [Info 714] Symbol 'sort_desc_by_year(void)' (line 181, file test.c) not referenced
A little raw, but if you fix all of that, most of your troubles will go away.

[edit]
Quote ...
it shows and error of Lvalue required... i don't know how to fix it...
Use strcpy to copy strings.
Last edited by Dave Sinkula; Aug 15th, 2006 at 12:31 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 15th, 2006
0

Re: Please help....

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

#define size 20

struct data
{
 int age;
 int year;
 char course[6];
 char name[20];


} students[20];

struct temporary  //changed to struct?
{
 int age;
 int year;
 char course[6];
 char name[20]; 

} holder;
 
int main()
{
int x,limit;
int input;
 do
 {
  //clrscr(); non portable avoid if possible
  puts("1..insert data");
  puts("2..show data");
  puts("3..show data in ascending order by age");
  puts("4..show data in ascending order by year");
  puts("5..show data in descending order by age");
  puts("6..show data in descending order by year");
  puts("9..exit");
  printf("input choice:"); scanf("%d",&input);
  switch(input)
  {
   case 1:
     inputdata();
     break;
   case 2:
     showdata();
     break;
   case 3:
    sort_asc_by_age(); //added this case
    break;             //added this case
        
  }
 }while(input!=9);
 
  getchar(); //getchar better than getch()
             //getch is non-portable
 return 0;
}
 
 
inputdata()
{
int limit,x;
printf("Size of structure to make? (1-20)\t");
scanf("%d",&limit);
if(limit<1 || limit>size)
 {
 printf("Input Invalid!");
 }
else
 {
   limit=limit-1;
   printf("\n\n\nAge Year Course  Name\n");
   
   for(x=0; x<=limit; x++)
   {
    printf("Age:");
    scanf("%d",&students[x].age);
    printf("Year:");
    scanf("%d",&students[x].year);
    printf("Course:");
    scanf("%s",students[x].course); //gets = bad
    printf("Name:");
    scanf("%s",students[x].name); //gets = bad 
    //although scanf is a lesser evil
    //for better a better input tutorial in c
    //check out dave's tutorial
    printf("\nNext entry\n");
   }
  
  }
}
 
showdata()
{
 int limit;
 int x;
 printf("This are the items you inputted!");
 printf("\nAge Year level Course  Name\n");
 for(x=0;x<size;x++)
  {
  printf("%d",students[x].age);
  printf("\t%d",students[x].year);//changed to %d (it is an integer!)
  printf("\t%s",students[x].course);
  printf("\t%s\n",students[x].name);
  }
 getchar(); //getchar better than getch()
}

sort_asc_by_age()
{
 
int x,y;
for(x = 0; x < size; x++)
    for(y = 0; y < size; y++)
      if(students[y].age > students[y+1].age)
      {
 holder.age=students[y+1].age;
 holder.year=students[y+1].year;
 strcpy(holder.course,students[y+1].course); //changed string copy
 strcpy(holder.name,students[y+1].name);// changed
 
 students[y+1].age=students[y].age;
 students[y+1].year=students[y].year;
 strcpy(students[y+1].name,students[y].name);//changed
 strcpy(students[y+1].course,students[y].course);//changed
 
 students[y].age=holder.age;
 students[y].year=holder.year;
 strcpy(students[y].name,holder.name); //changed
 strcpy(students[y].course,holder.course);//changed
       }
  for(x=0;x<size;x++)
  {
  printf("%d",students[x].age);
  printf("\t%d",students[x].year); //%d
  printf("\t%s",students[x].course);
  printf("\t%s\n",students[x].name);
  }
 getch();
}


sort_asc_by_year()
{
int x,y;
for(x = 0; x < size; x++)
    for(y = 0; y < size-1; y++)
      if(students[y].year > students[y+1].age)
 {
 holder.age=students[y+1].age;
 holder.year=students[y+1].year;
 strcpy(holder.course,students[y+1].course); //changed
 strcpy(holder.name,students[y+1].name);//changed
 
 students[y+1].age=students[y].age;
 students[y+1].year=students[y].year;
 strcpy(students[y+1].name,students[y].name);//changed
 strcpy(students[y+1].course,students[y].course);//changed
 
 students[y].age=holder.age;
 students[y].year=holder.year;
 strcpy(students[y].name,holder.name); //changed
 strcpy(students[y].course,holder.course);//changed
 
       }
for(x=0;x<size;x++)
  {
  printf("%d",students[x].age);
  printf("\t%d",students[x].year);  //%d
  printf("\t%s",students[x].course);
  printf("\t%s\n",students[x].name);
  }
 getchar(); //getchar
}


sort__desc_by_age()
{
 
int x,y;
for(x = 0; x < size; x++)
    for(y = 0; y < size; y++)
      if(students[y].age < students[y+1].age)
      {
 holder.age=students[y+1].age;
 holder.year=students[y+1].year;
 strcpy(holder.course,students[y+1].course); //changed
 strcpy(holder.name,students[y+1].name);//changed
 
 students[y+1].age=students[y].age;
 students[y+1].year=students[y].year;
 strcpy(students[y+1].name,students[y].name);//changed
 strcpy(students[y+1].course,students[y].course);//changed
 
 students[y].age=holder.age;
 students[y].year=holder.year;
 strcpy(students[y].name,holder.name); //changed
 strcpy(students[y].course,holder.course);//changed
       }
for(x=0;x<size;x++)
  {
  printf("%d",students[x].age);
  printf("\t%d",students[x].year);  //%d
  printf("\t%s",students[x].course);
  printf("\t%s\n",students[x].name);
  }
 getch();
}
sort_desc_by_year()
{
 
int x,y;
for(x = 0; x < size; x++)
    for(y = 0; y < size-1; y++)
      if(students[y].year < students[y+1].age)
 {
        holder.age=students[y+1].age;
 holder.year=students[y+1].year;
 strcpy(holder.course,students[y+1].course); //changed
 strcpy(holder.name,students[y+1].name);//changed
 
 students[y+1].age=students[y].age;
 students[y+1].year=students[y].year;
 strcpy(students[y+1].name,students[y].name);//changed
 strcpy(students[y+1].course,students[y].course);//changed
 
 students[y].age=holder.age;
 students[y].year=holder.year;
 strcpy(students[y].name,holder.name); //changed
 strcpy(students[y].course,holder.course);//changed
       }
for(x=0;x<size;x++)
  {
  printf("%d",students[x].age);
  printf("\t%d",students[x].year);  //%d
  printf("\t%s",students[x].course);
  printf("\t%s\n",students[x].name);
  }
 getchar(); //getchar()
}

I've made a few changes and added comments I hope this helps.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 16th, 2006
0

Re: Please help....

please help... i have made it more nicer to output... but the sorting does not sort the structure into ascending or descending... no sorting happens..
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #define size 20
  5.  
  6. struct data{
  7. int age;
  8. int year;
  9. char course[6];
  10. char name[20];
  11. } students[20];
  12. struct temporary{
  13. int age;
  14. int year;
  15. char course[6];
  16. char name[20];
  17. } holder;
  18. int main()
  19. {
  20. int x,limit;
  21. int input;
  22. clrscr();
  23. do
  24. {
  25. printf("this will not appear!");
  26. clrscr();
  27. puts("1..insert data");
  28. puts("2..show data in ascending order by age");
  29. puts("3..show data in ascending order by year");
  30. puts("4..show data in descending order by age");
  31. puts("5..show data in descending order by year");
  32. puts("6..exit");
  33. printf("input choice:"); scanf("%d",&input);
  34. switch(input)
  35. {
  36. case 1: inputdata(input);
  37. break;
  38. case 2: sort_asc_by_age(input);
  39. break;
  40. case 3: sort_asc_by_year(input);
  41. break;
  42.  
  43. case 4: sort_desc_by_age(input);
  44. break;
  45. case 5: sort_desc_by_year(input);
  46. break;
  47.  
  48. }
  49. }while(input!=6);
  50. printf("Jeffrey polpol!");
  51. getchar();
  52. return 0;
  53. }
  54.  
  55. inputdata(int input)
  56. {
  57. int limit,x,a,b,c;
  58. clrscr();
  59. printf("Size of data to make? (1-20):\t");
  60. scanf("%d",&limit);
  61. input=limit;
  62. if(limit<1 || limit>size)
  63. {
  64. printf("Input Invalid!");
  65. }
  66. else
  67. {
  68. clrscr();
  69. limit=limit-1;
  70. gotoxy(1,1);
  71. printf("Age Year Course Name");
  72. b=2;
  73. for(x=0; x<=limit; x++)
  74. {
  75. c=1;
  76. a=c;
  77. flushall();
  78. gotoxy(a,b); scanf("%d",&students[x].age);
  79. a=c+6;
  80. c=a;
  81. clreol();
  82. gotoxy(a,b); scanf("%d",&students[x].year);
  83. a=c+8;
  84. c=a;
  85. clreol();
  86. gotoxy(a,b); scanf("%s",students[x].course);
  87. a=c+12;
  88. c=a;
  89. clreol();
  90. gotoxy(a,b); scanf("%s",students[x].name);
  91. b++;
  92. clreol();
  93. gotoxy(a,b); printf("\n Next entry \n");
  94. clreol();
  95. }
  96. }
  97. return input;
  98. printf("press any key to show menu");
  99. }
  100.  
  101. sort_asc_by_age(int input)
  102. {
  103. int x,y,limit=input;
  104. clrscr();
  105. for(x = 0; x <limit; x++)
  106. for(y = 0; y <limit-1; y++)
  107. if(students[y].age > students[y+1].age)
  108. {
  109. holder.age=students[y+1].age;
  110. holder.year=students[y+1].year;
  111. strcpy(holder.course,students[y+1].course);
  112. strcpy(holder.name,students[y+1].name);
  113. students[y+1].age=students[y].age;
  114. students[y+1].year=students[y].year;
  115. strcpy(students[y+1].name,students[y].name);
  116. strcpy(students[y+1].course,students[y].course);
  117. students[y].age=holder.age;
  118. students[y].year=holder.year;
  119. strcpy(students[y].name,holder.name);
  120. strcpy(students[y].course,holder.course);
  121. }
  122. for(x=0;x<size;x++)
  123. {
  124. printf("%d",students[x].age);
  125. printf("\t%d",students[x].year);
  126. printf("\t%s",students[x].course);
  127. printf("\t%s\n",students[x].name);
  128. }
  129. printf("press any key to show menu");
  130. printf("ayay..");
  131. getch();
  132. }
  133.  
  134. sort_asc_by_year(int input)
  135. {
  136. int x,y,limit=input;
  137. clrscr();
  138. for(x=0;x<limit; x++)
  139. for(y=0;y<limit-1;y++)
  140. if(students[y].year>students[y+1].age)
  141. {
  142. holder.age=students[y+1].age;
  143. holder.year=students[y+1].year;
  144. strcpy(holder.course,students[y+1].course);
  145. strcpy(holder.name,students[y+1].name);
  146. students[y+1].age=students[y].age;
  147. students[y+1].year=students[y].year;
  148. strcpy(students[y+1].name,students[y].name);
  149. strcpy(students[y+1].course,students[y].course);
  150. students[y].age=holder.age;
  151. students[y].year=holder.year;
  152. strcpy(students[y].name,holder.name);
  153. strcpy(students[y].course,holder.course);
  154. }
  155. for(x=0;x<size;x++)
  156. {
  157. printf("%d",students[x].age);
  158. printf("\t%d",students[x].year); /*%d*/
  159. printf("\t%s",students[x].course);
  160. printf("\t%s\n",students[x].name);
  161. }
  162. printf("press any to key to menu");
  163. printf("hikap samad");
  164. getch();
  165. }
  166.  
  167. sort_desc_by_age(int input)
  168. {
  169. int x,y,limit=input;
  170. clrscr();
  171. for(x = 0; x < limit; x++)
  172. for(y = 0; y < limit-1; y++)
  173. if(students[y].age < students[y+1].age)
  174. {
  175. holder.age=students[y+1].age;
  176. holder.year=students[y+1].year;
  177. strcpy(holder.course,students[y+1].course);
  178. strcpy(holder.name,students[y+1].name);
  179. students[y+1].age=students[y].age;
  180. students[y+1].year=students[y].year;
  181. strcpy(students[y+1].name,students[y].name);
  182. strcpy(students[y+1].course,students[y].course);
  183. students[y].age=holder.age;
  184. students[y].year=holder.year;
  185. strcpy(students[y].name,holder.name);
  186. strcpy(students[y].course,holder.course);
  187. }
  188. for(x=0;x<size;x++)
  189. {
  190. printf("%d",students[x].age);
  191. printf("\t%d",students[x].year);
  192. printf("\t%s",students[x].course);
  193. printf("\t%s\n",students[x].name);
  194. }
  195. printf("press any key to show menu");
  196. printf("jeffrey asa");
  197. getch();
  198. }
  199.  
  200. sort_desc_by_year(int input)
  201. {
  202. int x,y,limit=input;
  203. clrscr();
  204. for(x = 0; x < limit; x++)
  205. for(y = 0; y < limit-1; y++)
  206. if(students[y].year < students[y+1].age)
  207. {
  208. holder.age=students[y+1].age;
  209. holder.year=students[y+1].year;
  210. strcpy(holder.course,students[y+1].course);
  211. strcpy(holder.name,students[y+1].name);
  212. students[y+1].age=students[y].age;
  213. students[y+1].year=students[y].year;
  214. strcpy(students[y+1].name,students[y].name);
  215. strcpy(students[y+1].course,students[y].course);
  216. students[y].age=holder.age;
  217. students[y].year=holder.year;
  218. strcpy(students[y].name,holder.name);
  219. strcpy(students[y].course,holder.course);
  220. }
  221. for(x=0;x<size;x++)
  222. {
  223. printf("%d",students[x].age);
  224. printf("\t%d",students[x].year);
  225. printf("\t%s",students[x].course);
  226. printf("\t%s\n",students[x].name);
  227. }
  228. printf("press any key to show menu");
  229. printf("polpol jeffrey");
  230. getch();
  231. }

please help....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cezarjont is offline Offline
9 posts
since Aug 2006
Aug 16th, 2006
0

Re: Please help....

Why I'm not offering much input:
Quote ...
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software 1985-2006

--- Module: test.c (C)
test.c 36 error [Info 718] Symbol 'inputdata' undeclared, assumed to return int
test.c 36 error [Info 746] call to function 'inputdata()' not made in the presence of a prototype
test.c 38 error [Info 718] Symbol 'sort_asc_by_age' undeclared, assumed to return int
test.c 38 error [Info 746] call to function 'sort_asc_by_age()' not made in the presence of a prototype
test.c 40 error [Info 718] Symbol 'sort_asc_by_year' undeclared, assumed to return int
test.c 40 error [Info 746] call to function 'sort_asc_by_year()' not made in the presence of a prototype
test.c 41 error [Info 725] Expected positive indentation from line 34
test.c 43 error [Info 718] Symbol 'sort_desc_by_age' undeclared, assumed to return int
test.c 43 error [Info 746] call to function 'sort_desc_by_age()' not made in the presence of a prototype
test.c 44 error [Info 725] Expected positive indentation from line 34
test.c 45 error [Info 718] Symbol 'sort_desc_by_year' undeclared, assumed to return int
test.c 45 error [Info 746] call to function 'sort_desc_by_year()' not made in the presence of a prototype
test.c 46 error [Info 725] Expected positive indentation from line 34
test.c 48 error [Info 744] switch statement has no default
test.c 51 error [Warning 534] Ignoring return value of function '_fgetc(FILE *)' (compare with line 294, file c:\progra~1\borland\bcc55\include\stdio.h)
test.c 53 error [Warning 529] Symbol 'x' (line 20) not subsequently referenced
test.c 53 error [Warning 529] Symbol 'limit' (line 20) not subsequently referenced
test.c 56 error [Info 745] function 'inputdata()' has no explicit type or class, int assumed
test.c 56 error [Warning 532] Return mode of function 'inputdata(int)' inconsistent with line 36
test.c 75 error [Warning 525] Negative indentation from line 73
test.c 76 error [Warning 525] Negative indentation from line 73
test.c 77 error [Warning 525] Negative indentation from line 73
test.c 77 error [Warning 534] Ignoring return value of function 'flushall(void)' (compare with line 299, file c:\progra~1\borland\bcc55\include\stdio.h)
test.c 79 error [Warning 525] Negative indentation from line 73
test.c 80 error [Warning 525] Negative indentation from line 73
test.c 81 error [Warning 525] Negative indentation from line 73
test.c 83 error [Warning 525] Negative indentation from line 73
test.c 84 error [Warning 525] Negative indentation from line 73
test.c 85 error [Warning 525] Negative indentation from line 73
test.c 87 error [Warning 525] Negative indentation from line 73
test.c 88 error [Warning 525] Negative indentation from line 73
test.c 89 error [Warning 525] Negative indentation from line 73
test.c 91 error [Warning 525] Negative indentation from line 73
test.c 92 error [Warning 525] Negative indentation from line 73
test.c 95 error [Warning 525] Negative indentation from line 73
test.c 98 error [Warning 527] Unreachable code at token 'printf'
test.c 99 error [Warning 533] function 'inputdata(int)' should return a value (see line 55)
test.c 102 error [Info 745] function 'sort_asc_by_age()' has no explicit type or class, int assumed
test.c 102 error [Warning 532] Return mode of function 'sort_asc_by_age(int)' inconsistent with line 38
test.c 109 error [Warning 525] Negative indentation from line 107
test.c 110 error [Warning 525] Negative indentation from line 107
test.c 111 error [Warning 525] Negative indentation from line 107
test.c 112 error [Warning 525] Negative indentation from line 107
test.c 113 error [Warning 525] Negative indentation from line 107
test.c 114 error [Warning 525] Negative indentation from line 107
test.c 115 error [Warning 525] Negative indentation from line 107
test.c 116 error [Warning 525] Negative indentation from line 107
test.c 117 error [Warning 525] Negative indentation from line 107
test.c 118 error [Warning 525] Negative indentation from line 107
test.c 119 error [Warning 525] Negative indentation from line 107
test.c 120 error [Warning 525] Negative indentation from line 107
test.c 122 error [Warning 539] Did not expect positive indentation from line 105
test.c 124 error [Info 725] Expected positive indentation from line 122
test.c 125 error [Info 725] Expected positive indentation from line 122
test.c 126 error [Info 725] Expected positive indentation from line 122
test.c 127 error [Info 725] Expected positive indentation from line 122
test.c 131 error [Warning 534] Ignoring return value of function 'getch(void)' (compare with line 174, file c:\progra~1\borland\bcc55\include\conio.h)
test.c 132 error [Warning 533] function 'sort_asc_by_age(int)' should return a value (see line 101)
test.c 135 error [Info 745] function 'sort_asc_by_year()' has no explicit type or class, int assumed
test.c 135 error [Warning 532] Return mode of function 'sort_asc_by_year(int)' inconsistent with line 40
test.c 141 error [Warning 525] Negative indentation from line 140
test.c 142 error [Warning 525] Negative indentation from line 140
test.c 143 error [Warning 525] Negative indentation from line 140
test.c 144 error [Warning 525] Negative indentation from line 140
test.c 145 error [Warning 525] Negative indentation from line 140
test.c 146 error [Warning 525] Negative indentation from line 140
test.c 147 error [Warning 525] Negative indentation from line 140
test.c 148 error [Warning 525] Negative indentation from line 140
test.c 149 error [Warning 525] Negative indentation from line 140
test.c 150 error [Warning 525] Negative indentation from line 140
test.c 151 error [Warning 525] Negative indentation from line 140
test.c 152 error [Warning 525] Negative indentation from line 140
test.c 153 error [Warning 525] Negative indentation from line 140
test.c 154 error [Warning 525] Negative indentation from line 140
test.c 162 error [Warning 539] Did not expect positive indentation from line 155
test.c 164 error [Warning 534] Ignoring return value of function 'getch(void)' (compare with line 174, file c:\progra~1\borland\bcc55\include\conio.h)
test.c 165 error [Warning 533] function 'sort_asc_by_year(int)' should return a value (see line 134)
test.c 168 error [Info 745] function 'sort_desc_by_age()' has no explicit type or class, int assumed
test.c 168 error [Warning 532] Return mode of function 'sort_desc_by_age(int)' inconsistent with line 43
test.c 175 error [Warning 525] Negative indentation from line 173
test.c 176 error [Warning 525] Negative indentation from line 173
test.c 177 error [Warning 525] Negative indentation from line 173
test.c 178 error [Warning 525] Negative indentation from line 173
test.c 179 error [Warning 525] Negative indentation from line 173
test.c 180 error [Warning 525] Negative indentation from line 173
test.c 181 error [Warning 525] Negative indentation from line 173
test.c 182 error [Warning 525] Negative indentation from line 173
test.c 183 error [Warning 525] Negative indentation from line 173
test.c 184 error [Warning 525] Negative indentation from line 173
test.c 185 error [Warning 525] Negative indentation from line 173
test.c 186 error [Warning 525] Negative indentation from line 173
test.c 195 error [Warning 539] Did not expect positive indentation from line 188
test.c 197 error [Warning 534] Ignoring return value of function 'getch(void)' (compare with line 174, file c:\progra~1\borland\bcc55\include\conio.h)
test.c 198 error [Warning 533] function 'sort_desc_by_age(int)' should return a value (see line 167)
test.c 201 error [Info 745] function 'sort_desc_by_year()' has no explicit type or class, int assumed
test.c 201 error [Warning 532] Return mode of function 'sort_desc_by_year(int)' inconsistent with line 45
test.c 207 error [Warning 525] Negative indentation from line 206
test.c 209 error [Warning 525] Negative indentation from line 206
test.c 210 error [Warning 525] Negative indentation from line 206
test.c 211 error [Warning 525] Negative indentation from line 206
test.c 212 error [Warning 525] Negative indentation from line 206
test.c 213 error [Warning 525] Negative indentation from line 206
test.c 214 error [Warning 525] Negative indentation from line 206
test.c 215 error [Warning 525] Negative indentation from line 206
test.c 216 error [Warning 525] Negative indentation from line 206
test.c 217 error [Warning 525] Negative indentation from line 206
test.c 218 error [Warning 525] Negative indentation from line 206
test.c 219 error [Warning 525] Negative indentation from line 206
test.c 220 error [Warning 525] Negative indentation from line 206
test.c 228 error [Warning 539] Did not expect positive indentation from line 221
test.c 230 error [Warning 534] Ignoring return value of function 'getch(void)' (compare with line 174, file c:\progra~1\borland\bcc55\include\conio.h)
test.c 231 error [Warning 533] function 'sort_desc_by_year(int)' should return a value (see line 200)
Outputting to file test.lob
c:\progra~1\borland\bcc55\bin\bcc32 -P- -c @MAKE0000.@@@
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test.c:
Warning W8065 test.c 36: Call to function 'inputdata' with no prototype in function main
Warning W8065 test.c 38: Call to function 'sort_asc_by_age' with no prototype in function main
Warning W8065 test.c 40: Call to function 'sort_asc_by_year' with no prototype in function main
Warning W8065 test.c 43: Call to function 'sort_desc_by_age' with no prototype in function main
Warning W8065 test.c 45: Call to function 'sort_desc_by_year' with no prototype in function main
Warning W8080 test.c 53: 'limit' is declared but never used in function main
Warning W8080 test.c 53: 'x' is declared but never used in function main
Warning W8066 test.c 98: Unreachable code in function inputdata
Warning W8070 test.c 99: Function should return a value in function inputdata
Warning W8004 test.c 88: 'c' is assigned a value that is never used in function inputdata
Warning W8070 test.c 132: Function should return a value in function sort_asc_by_age
Warning W8070 test.c 165: Function should return a value in function sort_asc_by_year
Warning W8070 test.c 198: Function should return a value in function sort_desc_by_age
Warning W8070 test.c 231: Function should return a value in function sort_desc_by_year
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 16th, 2006
0

Re: Please help....

:-( im still new to turbo c... i can't understand your post....
can you please make it easier to understand??????



please...
Last edited by cezarjont; Aug 16th, 2006 at 8:53 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cezarjont is offline Offline
9 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Correct me If I'm wrong
Next Thread in C Forum Timeline: Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC