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
#include <iostream>
#include <string>
struct students
{
int id;
int gradeOption;
std::string name;
};
int getStudents() //get and return the number of students
{
unsignedint 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 = newint[n];
int* pointsPossible = newint[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
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)
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.
#include <iostream>
#include <string>
struct students
{
int id;
int gradeOption;
std::string name;
};
int getStudents() //get and return the number of students
{
unsignedint 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 = newint[n];
int* pointsPossible = newint[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
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.
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.