structures and functions woohoo!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 16
Reputation: Alfy is an unknown quantity at this point 
Solved Threads: 0
Alfy Alfy is offline Offline
Newbie Poster

structures and functions woohoo!

 
0
  #1
Nov 4th, 2004
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,841
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: structures and functions woohoo!

 
0
  #2
Nov 4th, 2004
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC