problem with array

Pages: 12
is it possible to declare a two dimensional array that stores a name and grade
because the data type of name is string and the data type of grade is int

so im having trouble on how i will declare it
please help me thanks
Tie name and grade into struct and create an array of said struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Student
{
    std::string name;
    int grade;
};
//...
//Using our struct
Student students[10];
students[0].name = "John";
students[0].grade = 75;

//Using standard library
std::pair<std::string, int> students[10];
students[0].first = "Mary";
students[0].second = 75;
closed account (48T7M4Gy)
An array only stores one type and making it 2-d won't help because the 2 dimensions refer to the index numbers (locations) not the data itself. However you can do what you are thinking of by creating a class or struct with your two attributes of name and grade. ie encapsulate your data.
i dont know class yet we were told to use array and functions only
i want to declare a two dimensional array to store a name and grade and it will continue to store elements until the user inputs 'N';
closed account (48T7M4Gy)
struct?

Sorry, overlapped with sample code suggestion.

The only other way I can think of is studentName[x] = "Bob" and studentGrade[x] = 76

You program makes sure the x index is alway coordinated between the two. (a sort of primary key)
Last edited on
i dont know struct yet :(
closed account (48T7M4Gy)
Save Grade and Name as a single string and let the first two character equal the grade.

eg Student[1] = "76Bob"
The name and grade is base on the input of the user so we cant declare it as an element inside a array
Last edited on
You can use two arrays:
1
2
    std::string student[n];
    int grades[n];


See my example:
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

#include <iostream>
#include <string>
#include <stdexcept>

int main()
{
    const int n = 10; // Number of students
    std::string student[n];
    int grades[n];
    int counter = 0;

    // Get names and grades
    std::cout << "Enter a name and a grade for " +
                 std::to_string( n ) + " students (q=quit): ";
    for ( int i = 0; i < n; i++, counter++ ) {

        // Get name
        std::cout << std::endl << "Name: ";
        std::string str;
        if ( !std::getline( std::cin, str ) || str == "q" ) {
            break;
        }
        student[i] = str;

        // Get grade
        std::cout << "Grade: ";
        if ( !std::getline( std::cin, str ) || str == "q" ) {
            break;
        }

        // Exit from the program if user wrote non-number character
        try {
            int grade = std::stoi( str );
            grades[i] = grade;
        } catch ( const std::logic_error &e ) {
            std::cerr << "Error: incorrect grade." << std::endl;
            return 1;
        }
    }

    // Show names and grades
    std::cout << std::endl << "You wrote:" << std::endl;
    for ( int i = 0; i < counter; i++ ) {
        std::cout << "Name: " << student[i] << std::endl;
        std::cout << "Grade: " << grades[i] << std::endl;
        std::cout << std::endl;
    }

    return 0;
}


Program output:

Enter a name and a grade for 10 students (q=quit):
Name: Ivan
Grade: 5

Name: Marry
Grade: 5

Name: q

You wrote:
Name: Ivan
Grade: 5

Name: Marry
Grade: 5
Last edited on
closed account (48T7M4Gy)
@ Observer Yep, that's what I had in mind with the dual arrays. The index could be the student number.

@davez
The name and grade is base on the input of the user so we cant declare it as an element inside a array

I don't see why not. Have a look at Observers work. :-)
kemort, thank you! This problem was very good practice for me :) I hope it will help davez
Last edited on
closed account (48T7M4Gy)
Cheers Observer.
@Observer
it must be a multidimensional array
and the value of the elements inside the array is based on how many times the user inputs the grade and name of the student;
and i also need to display all the names and grade inputted by the user

i have no problem in getting the name and grade my problem is how i will display it because everytime i run the program it gives me garbage
Last edited on
You cannot have a multidimensional array containing data of different types.
Howeverm you can cast int to string and store it near name
I do not know why would you want this abomination
Last edited on
> it must be a multidimensional array
I don't understand how it will be look

> and the value of the elements inside the array is based on how many times the user inputs the grade and name of the student

Yes, you can use the std::vector class instead of the simple array:

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

int main()
{
    std::vector<std::string> students;
    std::vector<int> grades;
    int counter = 0;

    // Get names and grades
    std::cout << "Enter a name and a grade for "
                 "students (q=quit): ";
    while ( true ) {
        // Get name
        std::cout << std::endl << "Name: ";
        std::string str;
        if ( !std::getline( std::cin, str ) || str == "q" ) {
            break;
        }
        students.push_back( str );

        // Get grade
        std::cout << "Grade: ";
        if ( !std::getline( std::cin, str ) || str == "q" ) {
            break;
        }

        // Exit from the program if user wrote non-number character
        try {
            int grade = std::stoi( str );
            grades.push_back( grade );
        } catch ( const std::logic_error &e ) {
            std::cerr << "Error: incorrect grade." << std::endl;
            return 1;
        }

        counter++;
    }

    // Show names and grades
    std::cout << std::endl << "You wrote:" << std::endl;
    for ( int i = 0; i < counter; i++ ) {
        std::cout << "Name: " << students[i] << std::endl;
        std::cout << "Grade: " << grades[i] << std::endl;
        std::cout << std::endl;
    }

    return 0;
}
ill just ask my professor maybe its just a typo error
anyways thanks for feedback :D
@Observer i dont know how to use vectors yet can i store the elements entered by the user in an array?

Please teach me haha
@Observer
@MiiNiPaa
@kemort
Thank you very much i am able to finish the program using array but its not a multimensional array hahaha
I am studying C++ via this book: http://www.amazon.com/Professional-C-Marc-Gregoire/dp/0470932449

> Please teach me haha
If you will teach me English :)
Pages: 12