Thread: Basic Program but I need Help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    Question Basic Program but I need Help

    I have to write this program that does the following:

    You are to implement a C++ program that meets the following criteria:
    1) Retrieves the number of records from the beginning of a file
    2) Perform a counted loop (while/for) that will iterate for each of the records.
    3) These records will consist of the FORM:
    numberofgrades name grade1 …. GradeN
    example: 5 Mike 100 100 100 100 100
    4) Calculates an average as well as the concepts of the min/max
    grade entered for that particular student.
    5) Perform error checking on each entered grade received to ensure
    it is valid, since we are getting this input from a file, error
    messages should be supressed when they occur but during
    output should be noted.


    I am in an introductory course and this is our second project. Myself along with the rest of the class is completely confused... I'm not asking for a complete code from someone, just some guidance. I really do not know much about the C/C++ language due to lack of proper teaching and the book is just as confusing.

    The input should look like this:
    2 // total number of students
    5 Terry 100 100 100 100 100 // the first student “record”
    2 Susan 100 100 // the next student “record”

    The output should look like this:
    Student name: Student Average: Min: Max:
    Name1 average1 min1 max1
    Name2 average2 min2 max2

    People keep asking if know how to use arrays and stuff but we haven't learned any of that. We know the very very basics and thats all. Somehow the prof wants us to open another file that I believe will have the code w/ formulas but I dont understand why we need another file....something to do with header files too, I just don't know what these all are and how to use them. I read the book so I dont want to hear read the book and go to class because I do every week...I just need help getting started and understanding why we need header files and to open a file and as far as the input.... is it even possible to input the numbers and name like that? i didnt think that the comp would recognize that. anyways can someone help me out? oh also we're studying swapping... i kind of understand that but not exactly how to write it. thanks for your time

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I see no need for arrays. Just follow the instructions and implement one at a time.
    Here is some sudo-code
    Code:
    Open the file
    Read the number of student records that are in the file
    for each student
        read number of grades this student
        read this student's name
        for each grade
            read the grade
            keep track of minimum and maximum grade, and the sum of all grades for this student
          Note:  the above will require three variables for max, min, and sum
        end 
        calculate the average grade (sum of all grades / number of grades)
        display results for this student and reinitialize all variables to 0
    end
    Last edited by Ancient Dragon; 10-23-2005 at 02:06 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, and while you're at it...

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

    It appears that you are posting a homework assignment or other project.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.


    On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

    Feel free to PM me with any questions.

    Good Luck,

    Kermi3

    Read and follow the above instructions, and try your post (in the same thread) again.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    when you say "open the file" does that relate to header files? or is that just the basic program file? or is that the whole infile where we call on a file to be inserted into the project at a specific time...but i dont understand why a file would need to be called on to be opened at a certain time. what is that purpose?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    I would post some code if I had something....but I literally don't know what is going on. I did not ask for someone to write the program either - I'm just looking for guidance like someone already posted... someone said what i need to make codes for and the proper order with a few hints and that was great. i just didnt understand one thing he said and i reposted a response. the only thing i do know how to do for this project is output the line and the table values.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    apparently you need to study your textbook a little harder -- you DO have one don't you???
    when you say "open the file" does that relate to header files
    No. study about fstream c++ class. The file is the data file that resides on your hard drive that contains the student information.

    If you really don't know where to start, here is an example
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       // put your code here
    
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    i get that part....thats just the very very basic. but thanks for all the help.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    when you say "open the file" does that relate to header files? or is that just the basic program file? or is that the whole infile where we call on a file to be inserted into the project at a specific time
    None of the above. You need to study about "file input and output". It allows you to open a file on your computer and read from it or write to it. To write to a file, you have to open it a certain way. Those are the rules. To read from a file, you have to open it a different way. Those are the rules. There has to be some way to specify a file you want to read or write, afterall the program can't read your mind.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    I came up with all these different codes from talking with my prof today, I just don't know what to put in the main .cpp file and what to put in the header file. We must use a header file and I don't know what goes in there. Any suggestions or help or corrections??? Thanks

    Code:
     
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "header.h"
    
    using namespace std;
    
    
    void GetGrades();
    string name;
    int students, grade, gradesum, gradecount;
    float gradeavg;
    int min, max;
    
    ifstream infile;
    inflie.open ("header.h");
    
    
    int main (); 
    cout.width(10);
    cout<<"Student Name";
    cout.width(10);
    cout<<"Average Grade";
    cout.width(10);
    cout<<"Min Grade";
    cout.width(10);
    cout<<"Max Grade";
    
    for intj==0; <students; j++>{
    	if min=0, min=grade;
    	if max=100, max=grade;
    	gradesum=gradesum+grade;
    	students=students+1;
    		if (grade<min, swap(min,grade);
    		else if (grade>max, swap(max,grade);
    }
    
    
    
    
    Void
    	if (min==0) min=grade;
    	if (max==0) max=grade;
    	if (inrange (grade, 0, 100);
    	if (grade<min) swap (min, grade);
    		else (grade>max) swap (max, grade);
    	else;
    else;
    
    
    infile >> num_grades >> name;
    	
    for (int b=0);
    	        b<num_grades, b++;
    infile >> score;
    total_score = total_score + score
    ave + total_score/ num_grades

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    get that part....thats just the very very basic
    From the code you posed apparently you did NOT get it. Forget the header file for now, just concentrate on writing a workable and compileable program. Start with that program "shell" that I posted, then use the sudo-code (or something like it) and fill in the blanks with c++ code. For example, the first thing the sudo-code says is to "open the file". So now your program should look like this (Note: the program is supposed to open a data file, not the header file that is at the top of the program. The data file contains the student information. You will probably have to create that file manually using Notepad or some other program).

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       // put your code here
      ifstream infile;
      inflie.open ("MyDataFile.txt");
      // verify that the file was opened ok
      if( !infile.is_open())
      {
         // oops!
         cout << "could not open the file" << endl;
         return 1;
       }
      return 0;
    }
    Read the number of student records that are in the file
    That ^^^ implies that you will have to have a variable to hold the number of students that are in the data file.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       // put your code here
      ifstream infile;
      inflie.open ("MyDataFile.txt");
      // verify that the file was opened ok
      if( !infile.is_open())
      {
         // oops!
         cout << "could not open the file" << endl;
         return 1;
       }
     int NumStudents;
       // read the number of student records that are in the file
       infile >> NumStudents; 
      return 0;
    }
    for each student
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       // put your code here
      ifstream infile;
      inflie.open ("MyDataFile.txt");
      // verify that the file was opened ok
      if( !infile.is_open())
      {
         // oops!
         cout << "could not open the file" << endl;
         return 1;
       }
       int NumStudents;
       // read the number of student records that are in the file
       infile >> NumStudents; 
      // For each student record
       for( int i = 0; i < NumStudents; ++i) 
       {
           // do other stuff here
    
       }
      return 0;
    }
    I'm not going to write the rest of the program for you. Just do it one instruction at a time.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    thanks for everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:44 PM