Size keyword

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...

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <string>
#define NUMOFGRADES 4

struct StudentFolder{

    std::string stuName;
    int idNum;  // replace with stuIDNum
    int testGrades[NUMOFGRADES]; // scores[NUM_TESTS]
    char letterGrade;  // grade
    double average;
};

// function returns a StudentFolder pointer to an array of them
StudentFolder* initArray(int size);  // int size --> int studentsPtr

// allows for user input into it
void getInfo(StudentFolder studentFolder[], int size);

// prints out the info
void showInfo(StudentFolder studentFolder[], int size);

// checks for if the number is negative
bool isVerified(std::string num);

int main() {

    int numStudent;
  
    std::cout << "Enter the number of students: " ;
    std::cin >> numStudent;

          while(!isVerified(std::to_string(numStudent))){

              std::cout << "Enter the number of students: " ;
              std::cin >> numStudent;
            }
    std::cin.ignore();
    StudentFolder* s = initArray(numStudent);
    getInfo(s,numStudent);
    showInfo(s,numStudent);

    delete [] s;

}

StudentFolder* initArray(int size){

    return new StudentFolder[size];
}

void getInfo(StudentFolder studentFolder[], int size)
{

    for(int i = 0; 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;
        for(int j = 0; j < NUMOFGRADES; ++j){

            std::cout << "Test # " << j+1 << ": ";
            std::cin >> studentFolder[i].testGrades[j];

            while(!isVerified(std::to_string(studentFolder[i].testGrades[j]))){

                  std::cout << "Test # " << j+1 << ": ";
                std::cin >> studentFolder[i].testGrades[j];
 
            }

        }
        std::cin.ignore();
        std::cout << std::endl << std::endl;

    }
}


void showInfo(StudentFolder studentFolder[], int size)
{
  double total;
  double average;
  for(int i= 0; i < size; ++i)
  {
    total = 0.0;
    average = 0.0;

    for(int j = 0; j < NUMOFGRADES; ++j)
    {
      total +=  (double)studentFolder[i].testGrades[j];
    }
   average =  studentFolder[i].average = total / (double)NUMOFGRADES;

    if(average >= 91.0){

      studentFolder[i].letterGrade = 'A';
 
    }else if( average >= 81.0){

        studentFolder[i].letterGrade = 'B';

    }else if( average >= 71.0){

        studentFolder[i].letterGrade = 'C';

    }
    else if(average >= 61){

        studentFolder[i].letterGrade = 'D';

    }else{

        studentFolder[i].letterGrade = 'F';

    }
  }
  for(int i = 0; i < size; ++i){

    std::cout << "Student Name: " << studentFolder[i].stuName << std::endl;
     std::cout << "Student ID: " << studentFolder[i].idNum << std::endl;
     std::cout << "Student Average: " << studentFolder[i].average << std::endl;
     std::cout << "Student Letter Grade: " << studentFolder[i].letterGrade << std::endl;

    std::cout << std::endl << std::endl;
    
  }
}

bool isVerified(std::string num){

  for(int i = 0; i < num.length(); ++i){

    if( std::isdigit(num[i]) == 0 ){

      return false;
    }
   
  }
  return true;
}
Last edited on
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.
Last edited on
Ya woops, I got parameter confusion. Didn't see that numStudent was the same as size. Thanks for looking!
You're preaching to the choir. The admin doesn't care enough, unfortunately.
Lol, fair enough. I thought it was funny someone blamed my internet.
Also this site needs to be fixed, it wasn't letting me input the [] "code" functions to properly format...

Long time bug for an opening topic post, likely won't be fixed.

Follow up posts the format buttons and preview work. Editing the initial post also gets formatting and preview to work.

The two options available for the opening post? Either edit it after submission, or manually add the tags.
Perhaps something like:

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
96
97
98
99
100
101
102
#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();

    const auto s {initArray(numStudent)};

    getInfo(s, numStudent);
    showInfo(s, numStudent);

    delete[] s;
}

StudentFolder* initArray(size_t size) {
    return new 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';
        else if (studentFolder[i].average >= 81.0)
            studentFolder[i].letterGrade = 'B';
        else if (studentFolder[i].average >= 71.0)
            studentFolder[i].letterGrade = 'C';
        else if (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)
            return false;

    return true;
}


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.
Topic archived. No new replies allowed.