having troubles with multidimensional

Pages: 12
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.

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
 #include <iostream>
#include <stdlib.h>

using namespace std;
int main()
{
int n=99999;
string name[n];
 unsigned long int grade[n];
 string passf;
    int pass=0,fail=0;
 int counter=0;
 char choice;


   while (true)
   {
       rak:
       cout<<"Enter Name: ";
       cin>>name[counter];
       start:
       cout<<"Enter Grade: ";
       cin>>grade[counter];
         if (grade[counter]>100)
    {
        cout<<"invalid grade type again"<<endl;
        goto start;
    }
       cout<<"Add new record: ";
       cin>>choice;
    counter++;


       if (choice=='N'||choice=='n')
         {      display:
        system("cls");

        cout<<"GRADE SUMMARY REPORT"<<endl;
        cout<<endl;
        cout<<"Name\tGrade\tRemarks";
        cout<<endl;
            for (int c=0;c<counter;c++)
            {
                if (grade[c]>=75)
                {passf="Passed";
                pass++;
                }
            else{
        passf="Failed";
            fail++;
            }

              cout<<name[c]<<"\t"<<grade[c]<<"\t"<<passf<<endl;
            }
        cout<<endl;
        cout<<"Total Passed:"<<pass<<endl;
   cout<<"total Failed:"<<fail<<endl;
        return 0;
        }
        else if (choice=='Y'||choice=='y')
            goto rak;

        else{
            cout<<"Thank you."<<endl;
       goto display;
        }

   }


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

closed account (48T7M4Gy)
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.
can you show me how its done kemort becuase every time i call the 2d array it only gives me the grade that i inputted and not the name.
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

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
using namespace std;

struct record
{
    string name;
    int grade;
};
typedef struct 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
}
closed account (48T7M4Gy)
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.
im still having troubles on how to use stoi? please help me
Last edited on
closed account (48T7M4Gy)
Im still having troubles on how i will print it


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];

@kemort how can i access all the elements of the score for me to convert it to an integer
closed account (48T7M4Gy)
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
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
#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
const int n=100;
 string store[n][100];
 char choice;
int c=0;
int i;
string passf;

while (c<=10)
{
    cout<<"Enter Name: ";
    cin>>store[c][0];
    cout<<"Enter Grade: ";
    cin>>store[c][1];
    cout<<"Add New record? :";
    cin>>choice;
    c++;



        if (choice=='N'||choice=='n')
        {

            for (int e=0;e<c;e++)
            {

                if ( atoi(store[e][1].c_str())>=75)
                {
                    passf="Passed";
                }
               else
               passf="Failed";
            }


            cout<<"Name\tGrade\tRemarks";

            cout<<endl;

            for (int x=0;x<c;x++)
            {
                cout<<store[x][0]<<"\t"<<store[x][1]<<"\t"<<passf<<endl;

            }
        return 0;
        }
}


}






please try this it always gives me a "failed" in remarks
Last edited on
closed account (48T7M4Gy)
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. :)
last for loop?
closed account (48T7M4Gy)
yep
i cant understand what youre trying to say what last for loop?
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
for (int x=0;x<c;x++)
            {
               if ( atoi(store[e][1].c_str())>=75)
                {
                    passf="Passed";
                }
               else
               passf="Failed";
 cout<<store[x][0]<<"\t"<<store[x][1]<<"\t"<<passf<<endl;

            }
THANKSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
Pages: 12