Is this legal? Or does it not work

I am making a program that gives grades to students in a class.
Each student is to be created from a struct (can't use classes)
The user will enter the number of students he so desires to create
I use a dynamic array with a loop based off the number of students, to store each individually created student with their associated member variables





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
 #include <iostream>
#include <string>


struct students
{

  int id;
  int gradeOption;
  std::string name;

};

int getStudents() //get and return the number of students
{
  unsigned int x;
     std::cin >> x;

   std::string empty;
std::getline(std::cin, empty); //I use this to make sure the buffer is cleared
   return x;


}

 
int main()
{



  int n, numStudents;
  std::cin >> n;

 int* artifacts = new int[n];
 int* pointsPossible = new int[n];
 
for (int i = 0; i < n; i++)
  {

std::cin >> artifacts[i];
  }

 for (int j = 0; j < n; j++)
   {

     std::cin >> pointsPossible[j];
   }

//Everything above this line actually works(I tested it with couts)

 

 numStudents = getStudents();  //sets the number of students to be made
students* studentData = new students[numStudents]; //a dynamic array is made
                                                   //to store each student



 for (int k = 0; k < numStudents; k++)
  {
    std::cin >> studentData[k].id >> studentData[k].gradeOption
             >> studentData[k].name;
       }
 

 for (int z = 0; z < numStudents; z++)
   {

     std::cout << studentData[z].id << studentData[z].gradeOption
               << studentData[z].name;
   }
 
   





    return 0;
}

Basically, at the point in which I try to enter students and their info, the loop will only run its first time, then print out something not even close to whats expect

Lets say I enter 1 for the number of students, then enter, then info
1
123 G Chris
I will get printed out
1230000


Last edited on
Warning: Please ignore the user SakurasouBrothers, as he is known for providing incorrect and unreliable answers (as he has just demonstrated by adding the 'fourth item' to your struct for no apparent reason while not addressing the actual question at all)

----------------------------------------------------------

Now to answer your question.

The program works fine for me. I modified it a little by adding cout statements so I can see what im inputting, and it worked as expected. Only other thing I would add is to free the memory allocated with new using the delete operator.

I've included the modified code below as well as the output.

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
#include <iostream>
#include <string>


struct students
{

  int id;
  int gradeOption;
  std::string name;

};

int getStudents() //get and return the number of students
{
    unsigned int x;
    std::cin >> x;

    std::string empty;
    std::getline(std::cin, empty); //I use this to make sure the buffer is cleared
   return x;


}

 
int main()
{
  int n, numStudents;
  std::cout << "Enter n: ";
  std::cin >> n;

 int* artifacts = new int[n];
 int* pointsPossible = new int[n];
 
for (int i = 0; i < n; i++)
  {
    std::cout << "Enter artifact # " << i + 1 << ": ";
    std::cin >> artifacts[i];
  }

 for (int j = 0; j < n; j++)
   {
     std::cout << "Enter point # " << j + 1 << ": ";
     std::cin >> pointsPossible[j];
   }

//Everything above this line actually works(I tested it with couts)

 
  std::cout << "\nEnter # of students: ";
  numStudents = getStudents();  //sets the number of students to be made
  students* studentData = new students[numStudents]; //a dynamic array is made
                                                   //to store each student

    std::cout << "\n";

 for (int k = 0; k < numStudents; k++)
  {
     std::cout << "Enter info for student # " << k + 1 << ": ";
    std::cin >> studentData[k].id >> studentData[k].gradeOption
             >> studentData[k].name;
  }
 

 for (int z = 0; z < numStudents; z++)
   {

     std::cout << studentData[z].id << " " << studentData[z].gradeOption << " "
               << studentData[z].name << "\n";
   }
 
   // free all memory allocated using delete
   delete [] artifacts;
   delete [] pointsPossible;
   delete [] studentData
   
    return 0;
}



Enter n: 2
Enter artifact # 1: 2
Enter artifact # 2: 2
Enter point # 1: 3
Enter point # 2: 3

Enter # of students: 2

Enter info for student # 1: 120 85 John
Enter info for student # 2: 121 90 Smith
120 85 John
121 90 Smith
Last edited on
Nope, the first item (1), is the number of students. The three inputs after that are for the three struct fields.

The problem was in fact that the OP was entering the character 'G' when the program expects an integer. 'gradeOption' is of type int. I've demonstrated this in my previous post in the output.

Like I said, incorrect and unreliable...
Last edited on
Hello everyone and thank you for the help. The problem is that I was attempting to put a string inside of an int. My mistake.

Anyways, the reason for no prompts such as "Enter number of students: " etc is that the professor told us not to do that. The class is a large class, so he runs automated tests against each program and prompts will get in the way of that.

Again thank you for the help
Topic archived. No new replies allowed.