So I just got help with a tutor and his program uses "size" in a lot of the loops. Unfortunately I can't get a hold of him, and I'm not sure how this program actually runs. I thought "size" was set to an int variable set to some number. In his code he doesn't set size to anything. I tried looking up this keyword to learn how to use it, but all I get is ".size" or sizeof() and those are different. The program sets up an array for a structure and collects 4 test scores, and calculates the grades of students.
Also this site needs to be fixed, it wasn't letting me input the [] "code" functions to properly format...
I thought "size" was set to an int variable set to some number.
It is.
In his code he doesn't set size to anything
Let's start from main.
Line 31: User enters value into 'numStudent' variable.
Line 39: initArray(numStudent); is called
The numStudent variable is passed to the initArray function here. This value is copied into the function's "int size" parameter, and initializes size to whatever numStudent is. This then determines the size of the dynamic array created, and so on.
Also this site needs to be fixed, it wasn't letting me input the [] "code" functions to properly format...
You're preaching to the choir. The admin doesn't care enough, unfortunately.
#include <iostream>
#include <string>
constexpr size_t NUMOFGRADES {4};
struct StudentFolder {
std::string stuName;
int idNum {};
int testGrades[NUMOFGRADES] {};
char letterGrade {};
double average {};
};
// function returns a StudentFolder pointer to an array of them
StudentFolder* initArray(size_t size);
// allows for user input into it
void getInfo(StudentFolder studentFolder[], size_t size);
// prints out the info
void showInfo(const StudentFolder studentFolder[], size_t size);
// checks for if the number is negative
bool isVerified(const std::string& num);
int main() {
size_t numStudent {};
do {
std::cout << "Enter the number of students: ";
std::cin >> numStudent;
} while (!isVerified(std::to_string(numStudent)) && (std::cout << "Invalid\n"));
std::cin.ignore();
constauto s {initArray(numStudent)};
getInfo(s, numStudent);
showInfo(s, numStudent);
delete[] s;
}
StudentFolder* initArray(size_t size) {
returnnew StudentFolder[size];
}
void getInfo(StudentFolder studentFolder[], size_t size) {
for (size_t i {}; i < size; ++i) {
std::cout << "Enter a name: ";
std::getline(std::cin, studentFolder[i].stuName);
std::cout << "Enter an id number: ";
std::cin >> studentFolder[i].idNum;
double total {};
for (size_t j {}; j < NUMOFGRADES; ++j) {
do {
std::cout << "Test # " << j + 1 << " test grade: ";
std::cin >> studentFolder[i].testGrades[j];
} while (!isVerified(std::to_string(studentFolder[i].testGrades[j])) && (std::cout << "Invalid test scor4\n"));
total += studentFolder[i].testGrades[j];
}
std::cin.ignore();
studentFolder[i].average = total / NUMOFGRADES;
if (studentFolder[i].average >= 91.0)
studentFolder[i].letterGrade = 'A';
elseif (studentFolder[i].average >= 81.0)
studentFolder[i].letterGrade = 'B';
elseif (studentFolder[i].average >= 71.0)
studentFolder[i].letterGrade = 'C';
elseif (studentFolder[i].average >= 61.0)
studentFolder[i].letterGrade = 'D';
else
studentFolder[i].letterGrade = 'F';
std::cout << "\n\n";
}
}
void showInfo(const StudentFolder studentFolder[], size_t size) {
for (size_t i {}; i < size; ++i) {
std::cout << "Student Name: " << studentFolder[i].stuName << '\n';
std::cout << "Student ID: " << studentFolder[i].idNum << '\n';
std::cout << "Student Average: " << studentFolder[i].average << '\n';
std::cout << "Student Letter Grade: " << studentFolder[i].letterGrade << "\n\n\n";
}
}
bool isVerified(const std::string& num) {
for (size_t i {}; i < num.length(); ++i)
if (std::isdigit(num[i]) == 0)
returnfalse;
returntrue;
}
Note that use of isVerified() probably doesn't do what you want. You obtain numeric data via stream extraction to an int. If the input isn't an int then the >> will fail. std::to_string() will always generate a valid number - so isVerified() will always return true. if you want to check for invalid numeric input, then that is more complicated.