Results 1 to 4 of 4

Thread: C++ Asignment help

  1. #1

    Default C++ Asignment help

    Hello, everyone. I'm a student and I was assigned a coursework and I haven't had any issues with my last coursework/programming over all but this one gets me. I have to make a program for a group of students with a name, faculty number and marks in 3 subjects. The program should:
    1. Create an empty file (done)
    2. Add data for the students to the file (done)
    3. Change the files by an inserted faculty number (done)
    4. Seperately calculate the arithmetic average of the 3 subjects and output them in a text file
    Qt Code:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4.  
    5. struct Student {
    6.  
    7. char Name[50];
    8. char FN[7];
    9. int marks[3];
    10. float SRU;
    11. };
    12. char fileName[31];
    13. FILE *fs;
    14. Student st;
    15.  
    16. void createFile(){ //1
    17.  
    18. fs=fopen(fileName,"wb");
    19. fclose(fs);
    20. }
    21.  
    22. void ReadStudent (Student *st) {
    23.  
    24. int i;
    25. printf ("Name: "); scanf("%s",st->Name); getchar();
    26. printf ("Faculty number: "); scanf("%s",st->FN);
    27. printf ("Insert the marks of Physics(1), Mathematics(2) and Programming (3): \n");
    28. for (i=0;i<3;i++) {
    29. printf ("Subject %d-a: ",i+1);
    30. scanf ("%d",&st->marks[i]);
    31. }
    32. }
    33. void addStudents(){ //2
    34. fs=fopen(fileName, "a+b");
    35. char c;
    36. do {
    37. ReadStudent(&st);
    38. fwrite(&st, sizeof(Student),1,fs);
    39. printf("More (y/n)?"); c=getchar(); getchar();
    40.  
    41. }while(c=='y');c=getchar(); getchar();
    42. fclose(fs);
    43. }
    44. void changeData(){ // 3
    45.  
    46. char j[11];
    47. int k=0;
    48. // if(!exist(fileName)){
    49. // printf("The file's not created\n\n"); return;
    50.  
    51. printf("Insert the faculty number of the student whose data you'd like you change "); gets(j);
    52. fs=fopen (fileName, "r+b");
    53. fread(&st,sizeof(Student),1,fs);
    54. while (!feof(fs)) {
    55. if(strcmp(st.FN,j)==0) {
    56. ReadStudent(&st);
    57. fseek(fs,-(long)sizeof(Student), SEEK_CUR);
    58. fwrite(&st, sizeof(Student),1,fs);
    59. printf("The data has been changed\n");
    60. k++;
    61. break;
    62. }
    63. fread(&st, sizeof(Student),1,fs);
    64. }if (k==0) printf("No data for the student with that FN\n");
    65. fclose(fs);
    66. }
    67.  
    68. void arithmAv()
    69. {
    70. fs=fopen(fileName,"rb");
    71. FILE *txt=fopen("Students.txt","wt");
    72. int count=0;
    73. //Here I should write something but idk what...
    74. fprintf(txt, "There are %d student(s)\n",count);
    75. fclose(fs);
    76. fclose(txt);
    77. }
    78.  
    79. void main() {
    80. system("chcp 1251");
    81. printf("File name: ");
    82. gets(fileName);
    83. int answer;
    84. do{
    85. printf("\n\nMenu:\n");
    86. printf("1-Create an empty file\n");
    87. printf("2-Add data for a student to the file\n");
    88. printf("3-Change data for a student by FN\n");
    89. printf("4-Arithmetic average of the 3 subjects seperately and output in text file.\n");
    90. printf("0-Exit\n");
    91. char c;
    92. printf("Choose: "); scanf("%d", &answer);c=getchar(); getchar();
    93. switch (answer){
    94. case 1:createFile(); break;
    95. case 2:addStudents(); break;
    96. case 3:changeData(); break;
    97. case 4:arithmAv(); break;
    98. }
    99. }while(answer !=0);
    100. }
    To copy to clipboard, switch view to plain text mode 

    Please, help me! I'm totaly bumped I have no idea how to proceed...

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Asignment help

    Well, you need

    - three variables to hold the sum of marks for each subject
    - one variable to count the number of students

    and then do the read loop you already have, but this time add each student's marks to the three variable and increase the student counter.
    When you have processed the whole file you have accumulated all information for calculating the averages.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Asignment help

    This apparently has nothing to do with Qt. Are you certain that this is a C++ course? Your program is a C program, and uses the C standard library exclusively; I cannot imagine that a C++ teacher would approve that. On top of that, your code is riddled with buffer overflow vulnerabilities (just think of what scanf "%s" and gets do).

    Perhaps you should start over, C++-style, and use C++ iostreams. You will see that they are much easier to deal with than C stdio primitives, in particular when parsing strings. Consider that
    Qt Code:
    1. std::string s;
    2. std::cin >> s;
    To copy to clipboard, switch view to plain text mode 
    takes care of (re)allocating memory for the string as needed.

    Now, about your question. In order to compute the arithmetic average for each subject you have to inspect every mark in the database. One solution would be for your program to always keep the database loaded in memory: the file would be loaded and parsed on startup, then the program would exclusively update and inspect this database in memory, and you would only write the database back to a file upon exiting the program. Another solution consists in reopening and reparsing the file on each operation.

  4. #4

    Default Re: C++ Asignment help

    Actually, without reading your answers , I managed to get what I had to do. And we are studying, for a fact, C++ but we aren't using cin and cout like all other normal people. Thanks for the attention, though. You can close the topic.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.