We were assigned to declare a two dimensional array to store name and grade. The program should not stop in accepting array elements until the user entered 'N';
Enter Name: qwerty
Enter grade:100
Add new record? Y
Enter Name: yow
Enter grade: 90
Add New record? N
Grade Summary Report
Name Grade Remarks
qwerty 100 passed
yow 90 passed
i was able to do it but using one dimensional array can someone please help me here
below is the code on how i did it.
Arrays can only hold one type of variable and since name is a string and grades is a numeric type you can't use an multidimensional array to hold these two types of variables.
You should consider a structure/class to hold the information instead.
Also the following is not allowed in a C++ program:
1 2
int n=99999;
string name[n];
Array sizes in C++ must be compile time constants.
And instead of arrays, you really should consider using std::vector instead.
Your method of using two (separate) 1d arrays is a valid solution. Unless you store the data all as strings for example there is no 2d array which will store numerical values (grade) and string values (name).
If you store the data all as strings it would be possible but unnecessarily complicated to store names and grades. For example you could store thenames in the top half and the grades in the bottom half of a 2d (rectangular) array. But that would be a bit silly. Or maybe you could store them alongside each other in a checkerboard pattern. Still a bit silly.
is it possible to convert the string data type of a multidimensional array to a integer and print it as a table? @ jib how can i declare an array with unknown elements and base the elements on the input of the user.
because the program must not stop on accepting names and grades.
Either use std::vector is the best way. One of the other options would be to create a very large array and limit the number of entries to the size of that array. Another way would be to dynamically allocate the array to a medium size and "reallocate" the array to a larger size when required, but note this can get messy very very quickly and would probably be better suited to implement as a class.
is it possible to convert the string data type of a multidimensional array to a integer and print it as a table?
This really depends on what information your string is actually holding. For example converting a string that holds someone's name to an integer would probably not be practical. You would be better off using a structure to hold the data for each "person".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <vector>
#include <string>
struct Person
{
std::string name;
int age;
};
...
int main()
{
std::vector<Person> people;
// Or:
Person *people = new Person[500]; // An array of 500 Person.
...
how can i declare an array with unknown elements and base the elements on the input of the user.
For the fact that your array size depends on the user input, you should think of dynamic allocation of memory. In that case, a std::vector will come in handy.
You can create a structure, class to hold the name and grade
usingnamespace std;
struct record
{
string name;
int grade;
};
typedefstruct record R;
vector<R> vec;
while (true)
{
R instance;
char choice;
cout<<"Enter Name: ";
cin>>instance.name;
cout<<"Enter Grade: ";
cin>>instance.grade;
vec.push_back(instance);
//then do the check for new record
}
if you have a 2d string array called 'store' then the details for student n could be stored as store[n][0] =name, store[n][1] = grade. Use stoi() function to convert to numerical grade. So for 50 students need a 50 x 2 array.
Well it depends a bit on what decisions you make on the array of data. If you follow the 2d array I mentioned then you loop through all students with a for loop.
1 2
for ( int i = 0; i < TOTAL_STUDENTS; i++ )
std::cout << store[i][0] << " " << store[i][1];
Each student n has a name which is stored as a string in store[n][0] and a grade which is stored as a string in store[n][1].
If you want to convert student n's student number from a string to a number then the conversion would involve the function 'stoi' or 'strtol' which you would need to look up in the reference section of this site for <string>.
( If the student number doesn't have to be a numerical value then leave it as a string. )
i am able to convert it to an integer but whenever i try to compare the value of the elements to a certain number it only reads the final element of the array i already tried using loops but it only gives me the final element/ score inputted by the user
The reason you are getting no apparent response is because passf in line 49 is always the same value whether it is a pass or fail from the last student.
So you have to place the pass/fail test inside your last for loop. :)