Hello Scorpia,
Sorry about the delay. Between translating and testing this took longer than I thought.
Prefer to use the new line, ("\n"), over the "endl". "endl" is a function that takes time and the more you have the more time you will waste. At some point you may notice the program running slower.
In the beginning I changed these parts:
1 2 3 4 5 6 7
|
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
//#include <cmath> // <--- Proper file, but not needed.
void studentGrade(Student& student); // <--- Changed.
|
. The function now makes better use of what you have.
In "main":
1 2 3 4
|
for (int number = 0; number < count; number++)
{
studentGrade(student[number]);
}
|
Here you are sending 1 element to the function to use.
This is the translated version, but it is to show more the concept of what I did.
1 2 3 4 5 6 7 8 9 10 11 12
|
for (int number2 = 0; number2 < count; number2++)
{
if (student[number2].Print == 1)
cout << '\n'
<< "Student number: " << (number2 + 1) << ". information." << '\n'
<< "First name: " << student[number2].FirstName << '\n'
<< "Surname: " << student[number2].LastName << '\n'
<< "Rating of weekly exercises: " << student[number2].Grade << '\n'
<< "Grade assignment: " << student[number2].PracticeW << '\n' << '\n'
<< "Overall grade: " << student[number2].FirstName << ": " << student[number2].TotalGrade << ", rounded "
<< setprecision(1) << student[number2].TotalGrade << '\n';
}
|
The if statement is to allow something to display unless the functions choise was 2 or 3.
I put this after the above for loop because it works better:
1 2 3 4 5
|
std::cout << "\n\n" << "Press enter to close the program. ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
|
Line 2 may not always be needed.
For the function, the parts I worked on:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void studentGrade(Student& student)
{
// Enter the data
//Student student; // <--- No longer needed.
//cout << "1. Enter the student's first name: ";
//cin >> student.FirstName;
student.FirstName = "Stan"; // <--- Used for testing. Comment or remove when finished.
cout << "1. Enter the student's first name: " << student.FirstName << '\n'; // <--- Used for testing. Comment or remove when finished.
cout << "\nThe total grade is as follows: weekly exercises " << student.Setting << "% and assignment " << 100 - student.Setting << "%." << '\n';
cout << "\nPrint data? (1. yes, 2. no, 3. interrupt): ";
|
Lines 6 - 9 is a little trick to not have to enter something each time the program runs, but it works best when
int count{ 1 };
in "main". Once you know that the input is working it comes in very handy.
Line 11 was adding some spaces and using the "\n" instead of the "endl"s.
Line 13 I left where you originally had it, but did comment the following "cin". At least until I can get the while condition working better.
This is what I ended up with for the while loop:
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
|
while (cin >> student.Print && !std::cin || (student.Print > 1 || student.Print < 4))
{
if (!std::cin)
{
std::cerr << "\n Invalid Input! Must be a number.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
else if (student.Print == 1)
{
Grade1 = (student.Setting * Grade) / 100.0;
Grade2 = ((100 - student.Setting) * student.PracticeW) / 100.0;
TotalGrade = Grade1 + Grade2;
student.TotalGrade = TotalGrade;
return;
}
else if (student.Print == 2)
{
cout << "\n Data will not be printed. The program will be aborted.\n\n";
break;
}
else if (student.Print == 3)
{
std::cout << "\n Program interrupted.\n\n";
break;
}
else
{
std::cerr << "\n Invalid Choice! try again.\n\nPrint data ? (1. yes, 2. no, 3. interrupt) : ";
}
}
|
These changes appear to work for now, but there are some other things I want to try.
I would first get it working for 1 student then try for 2 or 3.
Andy