how to get a file line to do more than just print?

Pages: 12
hello, how can you get a text file with data like this do more than just print out a line.

[text]Fred Farkle 72 69 17 21 23 14 22
Tina Moffett 81 86 23 25 19 25 28
Lois Lepler 98 91 25 23 24 21 25
Brett Lyons 61 56 21 14 18 23 20
Alex Stark 81 72 19 23 21 12 21
Sandy Reardon 78 81 22 19 25 17 29
Jordan Taglauer 66 52 17 15 19 21 18
Rhonda Bellamy 91 92 23 24 21 19 22
Connor Sellers 78 83 22 25 19 21 21
Alice Legrow 99 73 21 18 16 26 19
Paul Richards 78 56 12 16 18 13 22

heres some more info to what the file represents:
 First name
 Last name
 Test One
 Test Two
 Assignment 1
 Assignment 2
 Assignment 3
 Assignment 4
 a number representing the number of classes they attended out of 30

here is what it supposed to do if it helps:

The teacher wants to see (on the monitor) for each student, the following:
 First name and Last Name
 Test Scores and Assignment Scores
 Final average
 Letter grade corresponding to the final average (use normal grading scale)
 A message letting him know if they pass or fail or fail due to attendance

all ive been seeing is how to print or write a file only. But i need to know how you process specfic parts of data like numbers and the names in the same line.


all ive been seeing is how to print or write a file only.

Unfortunately all the homework team have been arrested for assisting plagiarists.

So, best is to apply what you have seen.

Start by reading in the file line by line, preferably storing the data as individual variables per line. ( BTW: getline() isn't the best way to go)

Post what you've done when you get that far.
I haven't been doing any plagiarism , just using a pin name to hide my privacy lol. But I do thank you for the tip.
I'm sure you haven't. That's why the best idea is to have a go and post your honest attempt here.

If you comment out the <vector> stuff from this and consider the types of data involved ... http://www.cplusplus.com/forum/beginner/282299/#msg1221824
Last edited on
Well I'd read a line into a suitable struct using stream extraction, have a vector of that struct and read the whole file into that vector. Then once you're proved that and can display a student and all students, then you can perform whatever processing on the vector that is required for the assignment.

What issues are you having with this exercise? Can you open a file, extract data from a file, use a struct, use a vector??

What have you attempted so far? Post your current code.
I'm feeling generous today, here's a crude mash-up of code to read a data file, parse each line into the individual parts of a struct. Shove the filled struct into a vector for each student.

When done reading the data file and populating the vector, close the file. Display what was read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>

// a struct used to hold file data
// on a single student
struct student
{
   std::string  fName;
   std::string  lName;
   unsigned int test_one;   // int, uint or double?  depends on what you want to do
   unsigned int test_two;   // with the data
   unsigned int assign_one; // my choice based on what's in the file
   unsigned int assign_two;
   unsigned int assign_three;
   unsigned int assign_four;
   unsigned int attendence;
};

int main()
{
   std::ifstream in("data.txt");

   if (!in.is_open())
   {
      // can't open the file, bail!
      return (std::cerr << "** Unable to open file, exiting! **\n"), 1;
   }
   
   std::vector<student> Students;

   std::string temp;

   // there are other, better ways to read repetiive blocks of data from a file
   while (std::getline(in, temp))
   {
      student temp_student;

      std::istringstream is(temp);
      
      is >> temp_student.fName;
      is >> temp_student.lName;
      is >> temp_student.test_one;
      is >> temp_student.test_two;
      is >> temp_student.assign_one;
      is >> temp_student.assign_two;
      is >> temp_student.assign_three;
      is >> temp_student.assign_four;
      is >> temp_student.attendence;

      Students.push_back(temp_student);
   }
   
   // done reading file, close it.
   // it pays to be neat
   in.close();

   // a loop to display the stored data
   // this demonstrates how to access the stored strings and numbers
   for (size_t i { }; i < Students.size(); ++i)
   {
      std::cout << Students[i].lName << ", "
                << Students[i].fName << ": "
                << Students[i].test_one << ' '
                << Students[i].test_two << ' '
                << Students[i].assign_one << ' '
                << Students[i].assign_two << ' '
                << Students[i].assign_three << ' '
                << Students[i].assign_four << ' '
                << Students[i].attendence << '\n';
   }

   // what you do with the data stored is up to the assignment
}

With the sample test data you provided, here is what you should see when run:
1
2
3
4
5
6
7
8
9
10
11
Farkle, Fred: 72 69 17 21 23 14 22
Moffett, Tina: 81 86 23 25 19 25 28
Lepler, Lois: 98 91 25 23 24 21 25
Lyons, Brett: 61 56 21 14 18 23 20
Stark, Alex: 81 72 19 23 21 12 21
Reardon, Sandy: 78 81 22 19 25 17 29
Taglauer, Jordan: 66 52 17 15 19 21 18
Bellamy, Rhonda: 91 92 23 24 21 19 22
Sellers, Connor: 78 83 22 25 19 21 21
Legrow, Alice: 99 73 21 18 16 26 19
Richards, Paul: 78 56 12 16 18 13 22

I've shown how to fulfill the first two criteria of the assignment, displaying info, though not according to the exact specifications.

Go forth and code, grasshopper!
cool thanks for this.
There is no need to store the student data read into a vector, you can process each line of data "on the fly" when parsed out.

Do you know how to sum numbers and get the average? Getting a letter grade from the final average?

Plus, can you "prettify" the output to look more professional with "headers" that describe the output?

Segmenting all the code to do the various steps can be segmented off into functions. Functions that can call other functions to manipulate, mash and mangle the data tossed back and forth as needed.

You have a good chunk of design thinking to do.
yeah the assignment is dealing with functions, been having problems getting them to work.

this is what is have now. sorry of the novel here btw.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream> //Header File for console I/O
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>

using namespace std;

void display();
bool pass_fail ();
double grade(char, double);



// begin min Function Definition
int main()
{
	ifstream inputFile("My grades.txt");
	string  first, last,  line;
	char again;
	double Test_Scores, Test_Scores2,  assignment1, assignment2, assignment3, assignment4, class_attendance, average; 
// Display Identtification on screen
	cout << "assignement 5" << endl;
	cout << "Programmed by Paul Duckworth" << endl;
	do
{
	
		display();
		while(inputFile >> first >> last >> Test_Scores >> Test_Scores2 >> assignment1 >> assignment2 >> assignment3 >> assignment4 >> class_attendance){
			cout << "Student name:  "<<first << " "<< last << endl;
			cout << "Test Scores and Assignment Scores: " <<Test_Scores << " " << Test_Scores2 << " "
			<< assignment1 << " " << assignment2 << " "<< assignment3 << " "<< assignment4 << endl;
			cout << "Class attendence: "<< class_attendance << endl;
			
			
			cout << "Final Average: "<< average <<  endl;
			cout << "Grade: " << endl;
			cout << "Pass or fail: " << endl;
			
			
	}
	
	inputFile.close();
	cout << "do you want to rocess another student?  (Y/N) ";
	cin >> again;
	}	while(again == 'Y' || again == 'y');
	
	return 0;
//*********************************************
//Displays header
//
//*********************************************
	
	void display();
	{
		cout << " Students Grade Point Averages: " << endl;
		
	}
//*************************************************************
//Determine if student pass failed by counting the attendence,
// assignment and test scores
//
//*************************************************************	
	
	  
	bool pass_fail(char grade )
	{
	
	
	if (grade >= 60 == Pass)
	{		
	bool status;
	status = true;
	}			
	else
	if (grade < 60 == Fail)
	status = false;
	return status;
}
}
//************************************************************
//Calculate grade averages 
//
//
//************************************************************

double grade(double average)
{
	average = (Test_Scores + Test_Scores2 + assignment1 + 
	assignment2 + assignment3 + assignment4 + class_attendance)/ 3;
	

	
} //end of main function.
	  



************************************************
heres the rest of the assignment requirements if it helps:

The teacher's class policy is that students are required to take two tests and complete 4 assignments. All four of those assignments together bear as much weight on the final grade as a single test (hint: add the assignments and then average with tests). You will also need to determine if the student has met minimum requirements for passing the class and their final grade. Minimum requirements involve attendance and minimum grade performance - if the student has attended less than 20 classes, it is an automatic failure. If a student's grade average is lower than 60%, the student is considered to have failed also, no matter how many classes they attended.

The teacher wants to see (on the monitor) for each student, the following:
 First name and Last Name
 Test Scores and Assignment Scores
 Final average
 Letter grade corresponding to the final average (use normal grading scale)
 A message letting him know if they pass or fail or fail due to attendance
Format the output so the information is displayed in an attractive and readable manner.

Your program should do the following:
 Initialize the environment.
 Open the data file (file can be found here). Save this file in the same folder where you store your program.
 Process one record at a time using a loop.
 Perform proper cleanup after all the records are processed.
Other Requirements:

1. The program must handle a completely variable number of students. My grades.txt file could have one student or it could have 40 or somewhere in between. I may test your program with a different number of students than you use for testing.
2. The program must have three functions other than main. Please include the following functions:
a) An output function that creates the report header.
b) A function that calculates the student’s average.
c) A function that determines whether the students pass or fail.
Only one function may be a void function or have an empty parameter list. In other words, you need functions that are passing things in and out. Try to use reference parameter(s) if you need to return more than one value from a function.

Absolutely no global functions.

**************************************************
Heres what i can compille so far.

assignement 5
Programmed by Paul Duckworth
Student name: Fred Farkle
Test Scores and Assignment Scores: 72 69 17 21 23 14
Class attendence: 22
Final Average
Grade:
Pass or fail:
Student name: Tina Moffett
Test Scores and Assignment Scores: 81 86 23 25 19 25
Class attendence: 28
Final Average
Grade:
Pass or fail:
Student name: Lois Lepler
Test Scores and Assignment Scores: 98 91 25 23 24 21
Class attendence: 25
Final Average
Grade:
Pass or fail:
Student name: Brett Lyons
Test Scores and Assignment Scores: 61 56 21 14 18 23
Class attendence: 20
Final Average
Grade:
Pass or fail:
Student name: Alex Stark
Test Scores and Assignment Scores: 81 72 19 23 21 12
Class attendence: 21
Final Average
Grade:
Pass or fail:
Student name: Sandy Reardon
Test Scores and Assignment Scores: 78 81 22 19 25 17
Class attendence: 29
Final Average
Grade:
Pass or fail:
Student name: Jordan Taglauer
Test Scores and Assignment Scores: 66 52 17 15 19 21
Class attendence: 18
Final Average
Grade:
Pass or fail:
Student name: Rhonda Bellamy
Test Scores and Assignment Scores: 91 92 23 24 21 19
Class attendence: 22
Final Average
Grade:
Pass or fail:
Student name: Connor Sellers
Test Scores and Assignment Scores: 78 83 22 25 19 21
Class attendence: 21
Final Average
Grade:
Pass or fail:
Student name: Alice Legrow
Test Scores and Assignment Scores: 99 73 21 18 16 26
Class attendence: 19
Final Average
Grade:
Pass or fail:
Student name: Paul Richards
Test Scores and Assignment Scores: 78 56 12 16 18 13
Class attendence: 22
Final Average
Grade:
Pass or fail:
Student name: Adam Soleski
Test Scores and Assignment Scores: 87 65 17 17 23 16
Class attendence: 24
Final Average
Grade:
Pass or fail:
Student name: April Lanford
Test Scores and Assignment Scores: 77 83 21 23 20 19
Class attendence: 28
Final Average
Grade:
Pass or fail:
Student name: Bruce Wagner
Test Scores and Assignment Scores: 89 90 20 20 19 24
Class attendence: 29
Final Average
Grade:
Pass or fail:
Student name: Leslie Gore
Test Scores and Assignment Scores: 56 87 14 21 17 23
Class attendence: 24
Final Average
Grade:
Pass or fail:
Student name: George Whittaker
Test Scores and Assignment Scores: 66 77 20 20 17 23
Class attendence: 25
Final Average
Grade:
Pass or fail:
do you want to rocess another student? (Y/N) Scores and Assignment Scores: 87 65 17 17 23 16

--------------------------------
Process exited after 4.344 seconds with return value 0
Press any key to continue . . .

assignment requirement wrote:
Absolutely no global functions.

That makes no sense. A standalone function is a global function. You have 3.

All four of those assignments together bear as much weight on the final grade as a single test

A-HA! I thought there was some scores weighting.

In the future post your ENTIRE assignment, along with whatever code you've written to fulfill the requirements. As I said, the code I wrote didn't reflect the assignment by storing the entire number of student data in a vector.

One thing about using a struct to hold the data is it can make passing the data around into and out of functions a LOT easier.

IF, of course, you've been taught in class about structs.

The code you posted has got multiple errors, so you shouldn't be able to run it. It should fail compilation. No ifs, ands or buts.

The code you posted is fooked, it a misplaced closing bracket.

One of your function definitions is borked by having a ; at the end.

Your function declarations do NOT match their definitions later in the code.

You are defining a local variable inside an if statement, yet you use that variable in the following if then block, the scope of that variable won't allow you to use it.

You are failing to pass ALL of the data needed as parameters, so you have lots of undefined local variables in at least one of your functions.

I repeat....

The code you posted will not compile, it has errors. NO OUTPUT!
I changed my mind.
Try building on what follows if you like.
(PS: If you'd taken the earlier advice to just read in the variables, then taken the time to read up on how functions and passing parameters works you wouldn't be down the current rabbit hole. You still need to do some easy but extra work on decisions - if's etc)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void displayHeading();
double getAverage(double, double, double, double, double, double);
char getGrade(double);
char passORfail(double);

int main()
{
    string  file_name{"my_grades.txt"}, first, last;
    string line = string(50,'+');
    
    double test1, test2, ass1, ass2, ass3, ass4, attendance;
    
    double average_score{0};
    
    ifstream inputFile(file_name);
    if(!inputFile.is_open())
    {
        cout << "ERROR: File not opened\n";
        return -99;
    }
    
    displayHeading();
    while(
          inputFile
          >> first >> last >> test1 >> test2 >> ass1 >> ass2 >> ass3 >> ass4
          >> attendance
          )
    {
        average_score = getAverage(test1, test2, ass1, ass2, ass3, ass4);
        
    
        cout
        << "              Student name: " << first << " " << last << '\n'
        << "Test and Assignment Scores: "
        << test1 << " " << test2 << " " << ass1 << " " << ass2 << " " << ass3
        << " "<< ass4 << '\n'
        << "          Class attendence: " << attendance << '\n'
        << "             Final Average: " << average_score <<  '\n'
        << "                     Grade: " << getGrade(average_score)<< '\n'
        << "        Pass(P) or Fail(F): " << passORfail(average_score) << '\n'
        << line << "\n\n";
    }
    
    inputFile.close();
    
    return 0;
}

void displayHeading()
{
    cout
    << "              *** STUDENT SCORES REPORT ***\n\n"
    << "Assignement 5 - Programmed by Paul Duckworth and cplusplus\n\n";
}

double getAverage(double a, double b, double c, double d, double e, double f )
{
    return (a + b + c + d + e + f)/3.0;// 5.0;
}

// STUB - Hint: send me an average and I'll give you a letter grade
char getGrade(double av)
{
    return 'Z';
}

// STUB - Hint: send me an average and I'll give you a P or F
char passORfail(double av)
{
    return 'P';
}


Believe me, it runs and spits out a report.
Last edited on
As a starter, perhaps this. NOTE the grade boundaries need to be checked as they aren't given:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

struct Student {
	std::string  fName;
	std::string  lName;
	unsigned test_one {};
	unsigned test_two {};
	unsigned assign_one {};
	unsigned assign_two {};
	unsigned assign_three {};
	unsigned assign_four {};
	unsigned attendence {};
};

std::istream& operator>>(std::istream& is, Student& s) {
	return is >> s.fName >> s.lName >> s.test_one >> s.test_two >>
		s.assign_one >> s.assign_two >> s.assign_three >> s.assign_four >> s.attendence;
}

double average(const Student& s) {
	return (s.assign_one + s.assign_two + s.assign_three + s.assign_four  + s.test_one + s.test_two) / 3.0;
}

char grade(double average) {
	// Check bounds as required

	return 'F' - ((average > 90) + (average > 80) + (average > 70) + (average > 60) + (average > 50));
}

void display(const Student& s) {
	constexpr double passMrk {60.0};
	constexpr unsigned passAtt {20};
	const auto av {average(s)};

	std::cout << std::left << std::setw(10) << s.fName << std::setw(10) << s.lName
		<< std::setw(6) << s.test_one << std::setw(6) << s.test_two << std::setw(6)
		<< std::setw(6) << s.assign_one << std::setw(6) << s.assign_two << std::setw(6) << s.assign_three << std::setw(6) << s.assign_four
		<< std::setw(6) << std::fixed << std::setprecision(2) << av << "  " << std::setw(4) << grade(av)
		 << (av >= passMrk && s.attendence >= passAtt ? "Pass" : "Fail");
	std::cout << '\n';
}

int main() {
	std::ifstream in("data.txt");

	if (!in)
		return (std::cerr << "** Unable to open file, exiting! **\n"), 1;

	for (Student s; in >> s; )
		display(s);
}



Fred      Farkle    72    69    17    21    23    14    72.00   C   Pass
Tina      Moffett   81    86    23    25    19    25    86.33   B   Pass
Lois      Lepler    98    91    25    23    24    21    94.00   A   Pass
Brett     Lyons     61    56    21    14    18    23    64.33   D   Pass
Alex      Stark     81    72    19    23    21    12    76.00   C   Pass
Sandy     Reardon   78    81    22    19    25    17    80.67   B   Pass
Jordan    Taglauer  66    52    17    15    19    21    63.33   D   Fail
Rhonda    Bellamy   91    92    23    24    21    19    90.00   B   Pass
Connor    Sellers   78    83    22    25    19    21    82.67   B   Pass
Alice     Legrow    99    73    21    18    16    26    84.33   B   Fail
Paul      Richards  78    56    12    16    18    13    64.33   D   Pass

if the student has attended less than 20 classes, it is an automatic failure.
Isn't that what L42 allows? Irrespective of the average, if attendance < 20 then the condition will be false hence Fail will be displayed? The only 2 students with an attendance < 20 are Jordan (18) and Alice (19) - both of which display Fail - even though Alice's average is greater than the pass mark.
Most school letter scoring systems don't use E as a grade. A, B, C, D are passing, F is a fail.
1
2
3
4
5
6
7
8
9
10
char grade(double average)
{
   // Check bounds as required

   char score =  'F' - ((average > 90) + (average > 80) + (average > 70) + (average > 60) + (average > 50));

   if (average < 60) score = 'F';

   return score;
}

With the current sample size data there are no score fails, everyone's average is above 60.

The score weighting likely needs to be better defined, should everyone in the sample pass based solely on score? The only fails are for poor attendance.
Yep. The grade boundaries aren't defined in the OP. When I last did exams why back with Noah, there were grades A - F. I forget the grade boundaries used. All the OP says about grades is that < 60 average is a fail (which are grades E and F on my scale) - but everyone has an average >= 60 (lowest is Jordan 63.3 - but he fails on attendance). Anyhow, it's easy to change the grade boundaries once they are known.
We both presumed things to get it working, without any actual guidance.

Letter grades in my experience were before Noah, my schooling was with Cain and Abel. A-D and F was it, no E.

An E grade if it existed doesn't mean "Excellent."
OK M. de Moivre
Just use O, E, A, P, D, T as the grades.
Dat's 'nother part of the assignment requirements the OP hasn't graced us with either, What grade letters are used.

Lots of info is assumed.

My letter grade system is what was used in US public schools, pre-Uni education.
Pages: 12