944,163 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2940
  • C++ RSS
Nov 4th, 2004
0

structures and functions woohoo!

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int i,type;
  4. int buffer[100],line[50];
  5.  
  6. struct student
  7. {
  8. char name[20];
  9. int age;
  10. int id;
  11. };
  12. struct student students[3];
  13.  
  14. int main()
  15. {
  16. for(i=0;i<3;i++)
  17. {
  18. printf("please enter your name:\n");
  19. fgets(students[i].name,sizeof(students[i].name),stdin);
  20. printf("please enter your age:\n");
  21. fgets(buffer,sizeof(buffer),stdin);
  22. sscanf(buffer,"%d",&students[i].age);
  23. printf("please enter your id:\n");
  24. fgets(buffer,sizeof(buffer),stdin);
  25. sscanf(buffer,"%d",&students[i].id);
  26. }
  27. while(1)
  28. {
  29. int *ptr=students;
  30. printf("Please enter the # for the program you want to run:\n");
  31. fgets(line,sizeof(line),stdin);
  32. sscanf(line,"%d",&type);
  33. if(type==5)
  34. break;
  35. switch(type)
  36. {
  37. int insertsort(int list[],*ptr) /*function to sort an array of integers */
  38. {
  39. int j, k, index;
  40. or (j=1;j<students[i-1];j++)
  41. {
  42. index=list[j];
  43. k=j;
  44. while ((k>0) && (list[k-1]>index))
  45. {
  46. list[k] = list[k-1];
  47. k=k-1;
  48. }
  49. list[k] = index;
  50. }
  51. }
  52. case 1:/* sort by name */
  53.  
  54. case 2:/* sort by age */
  55. insertsort(students_ptr->age);
  56. case 3:/* sort by id */
  57. insertsort(*ptr[i].id);
  58.  
  59. case 4:
  60. printf("1-sort by name\n2-sort by age\n3-sort by id\n4-help\n5-Quit\n");
  61. continue;
  62.  
  63. default:
  64. printf("enter 4 for help\n");
  65. continue;
  66. }
  67. prinf("idk\n");
  68. }
  69. return(0);
  70. }
as u can see the structure was created so that th euser inputs the students name, age and id. It seems to work fine but i get an error (using dev-c++):21[Warning] passing arg 1 of `fgets' from incompatible pointer type for like every line in that code. Next theres the function, should this be in my while loop, or outside of it? im guessing outside although its not like that in the example. Im kinda lost on the whole bossing of a structure to a function. just some tips would be nice, thanks a bunch!
Last edited by alc6379; Nov 4th, 2004 at 11:39 pm. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alfy is offline Offline
16 posts
since Oct 2004
Nov 4th, 2004
0

Re: structures and functions woohoo!

>passing arg 1 of `fgets' from incompatible pointer type
fgets expects a pointer to char. You pass it a pointer to int because buffer is defined as an array of int. Change buffer to be an array of char and the warning will go away.

>should this be in my while loop, or outside of it?
Functions can only be defined at the top lexical level of a program. This means that your function must be outside of main, which is most certainly outside of the loop.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Looking for _gcvt(double,int,string)
Next Thread in C++ Forum Timeline: When does the memory get allocated to the function





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


Follow us on Twitter


© 2011 DaniWeb® LLC