CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: The last below 2 void function what should I write ?

  1. #1
    Join Date
    Feb 2016
    Posts
    1

    The last below 2 void function what should I write ?

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <fstream>
    #include <iomanip>

    #define MAXLEN 30

    /* structure definition */
    struct StudentRec
    {
    char name[MAXLEN];
    float grade1;
    float grade2;
    float average;
    };

    /* function prototypes */
    void ReadGrade(StudentRec *Student);
    void Display(const StudentRec Student[], int last);
    void ReadFromFile(StudentRec Student[], int *next);
    void Search(const StudentRec Student[], int last);

    using namespace std;
    /* main program */
    int main(void)
    {
    int quit = 0;
    char action;
    StudentRec Stud[MAXLEN]; //an array to store student information
    int next = 0; //counter to keep track of the number of students in the array

    while(!quit)
    {
    cout << "\n\t\t******************************************";
    cout << "\n\t\t**\tStudent Records Menu\t\t**";
    cout << "\n\t\t******************************************\n\n";
    cout << "\t\t<1> Read Grade.\n";
    cout << "\t\t<2> Display.\n";
    cout << "\t\t<3> Read File.\n";
    cout << "\t\t<4> Search.\n";
    cout << "\t\t<5> Quit.\n\n";
    cout << "\tPlease enter your option --> ";
    cin >> action;
    cout << "\n\n";

    switch(action)
    {
    case '1': ReadGrade(&Stud[next]);
    next++;
    break;

    case '2': Display(Stud, next-1);
    break;

    case '3': ReadFromFile(Stud, &next);
    break;

    case '4': Search(Stud, next-1);
    break;

    case '5': quit = 1;
    break;

    default : cout << "\tWrong selection.\n";
    break;
    }
    }
    }

    //read student information from keyboard and store in array
    void ReadGrade(StudentRec *Student)
    {
    cout << "\tPlease enter the student name : ";
    cin >> Student->name;
    cout << "\tPlease enter the student grade 1: ";
    cin >> Student->grade1;
    cout << "\tPlease enter the student grade 2: ";
    cin >> Student->grade2;
    Student->average = (Student->grade1 + Student->grade2) / 2;
    cout << "\tThe average of the student grades is " << Student->average << endl;
    }


    //display all the student information
    void Display(const StudentRec Student[], int last)
    {
    int i;

    cout << "Name\t\tGrade1\t\tGrade2\t\tAverage\n";
    for(i = 0; i <= last; i++)
    {
    cout << Student[i].name << "\t\t" << setprecision(1) << showpoint << fixed << Student[i].grade1 << "\t\t" << Student[i].grade2 << "\t\t" << Student[i].average << endl;
    }
    }


    //read record from file and store in array Student
    // next is the counter in the array to keep track of number of student
    void ReadFromFile(StudentRec Student[], int *next)
    {

    }

    //function to search for a student in an array according to name
    //last is the numebr of students store in the array Student
    void Search(const StudentRec Student[], int last)
    {

    }
    Attached Files Attached Files

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,118

    Re: The last below 2 void function what should I write ?

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    The last below 2 void function what should I write ?
    Please see http://forums.codeguru.com/showthrea...ork-assignment

    With that in mind, what is the issue you are having with the code for these two functions? For the file read, you'll need to open the required file, read/parse each record and update the student record array until all records have been processed. See http://www.cplusplus.com/reference/fstream/ifstream/

    For the search, you'll need to ask for a student name and iterate through the record array until either the name is found or the end is reached and report accordingly.

    If you produce some code for these functions and post back here we'll be able to advise/guide further.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)