Entering answers into the array

In the following code I'm suppose to display 2 students information.
Their names and 10 answers for a test.
Alan Baker a,b,b,d,c,a,d,b,d,d
Rodney Bill b,c,d,a,b,d,a,c,d,a

the problem I'm running into is I'm having a problem entering my answers into the char array included in the structure Student_Info.
then in the display I need to display the answers in the order above for each name
If anyone could help me by showing me what I'm missing or doing wrong, that would be appreciated


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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//structure created to hold student answers and name
struct Student_Info{
    string last_name;
    string first_name;
    char student_test_answers[10];
};
//there will be 2 students
Student_Info student[2];

//function to preset name and answers
void students_name_and_answers(){
student[1].first_name="Alan ";
student[1].last_name="Baker";
//the following line is where I am having a problem
student[1].student_test_answers = {'b','a','t'};

student[2].first_name="Rodney ";
student[2].last_name="Bill";
}

//function for display
void Display_Information()
{
    //for loop to cout students information
    for(int x = 1;x <3;x++)
    {
        cout<<student[x].first_name<<student[x].last_name<<"'s answers are ";
        //the following line is the display for the answers
        //cout << students_test_and_answers;
    }
}

//functions called into the main
int main(){
    students_name_and_answers();
    Display_Information();
}
Last edited on
It seems you don’t know C-style array indexes start from 0, so student[1] is the second element of the array.
Your loop here:
 
for(int x = 1; x < 3 ; x++)

will go out of boundaries (and its first element will be neglected).


- - -
About your question: assignment is not initialization. C-style arrays don’t have any overloaded operator= to perform what your are trying to do.
You can assign a value to each ‘cell’ of the array:
1
2
3
student[1].student_test_answers[0] = 'b';
student[1].student_test_answers[1] = 'a';
...

or you could simply use a third std::string:
1
2
3
4
5
struct Student_Info{
    std::string last_name;
    std::string first_name;
    std::string student_test_answers[10];
};



- - -
To output your C-style arrays properly, maybe you want to reserve space for the '\0' terminating character and add it manually.
A few things wrong here. First off, your array starts from 0, so you can't have "student[2]", your program will crash.

1
2
3
4
5
6
7
8
9
10
11
12
Student_Info student[2]; //2 Elements In This Array

//function to preset name and answers
void students_name_and_answers(){
student[1].first_name="Alan "; // Setting The FINAL Element In Array
student[1].last_name="Baker";
//the following line is where I am having a problem
student[1].student_test_answers = {'b','a','t'};

student[2].first_name="Rodney "; //Trying To Set Element OUTSIDE The Array.
student[2].last_name="Bill";
}


What you want is "student[0]" and "student[1]".


Next, you can't do what you're trying to at line 20. That's only possible when the variable is being declared, so only possible within the struct if you want that variable to have default values. Now, you have to use a loop and change every element solo, like this:

1
2
3
4
5
string answers = "b a t"; //Store The Answers Here
for (int i = 0; i < answers.length(); i++)
{
	student[1].student_test_answers[i] = answers[i];
}


You can store the answers in a string and then assign them. Or, if you have to get the answers from the user or a file, you can directly >> them into array.
Topic archived. No new replies allowed.