Passing struct intro function

Hello.
Title says it all, i got problem Passing struct intro function to change its values.
(this is just copy from my code if its lacking something i will add it)
here is my code:
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
struct Students
{
    short int subjects;
    std::string subjectName;
    short int subjectGrade;
    std::string studentsName;
};

char ReturnInput()
{
    char tempCharNameChange[31];
    if(!(std::cin >> tempCharNameChange))
    {
        std::cin.clear();
        std::cin.ignore(100, '\n');
    }
    return *tempCharNameChange;
}

bool LookForStudentAndSetName(int StudentsAmount, char* tempChar, Students *StructStudents)
{
    bool qwe = false;
    for(int x = 0; x < StudentsAmount; x++)
        {
            if(tempChar == StructStudents[x]->studentsName)
            {
                TypeStuff(6);
                (StructStudents[x]->studentsName) = ReturnInput(); //This should set my struct student name to ReturnInput
                qwe = true;
            }
        }
    return qwe;
}

int main()
{   int StudentsAmount = 0;
    char tempChar[63];
    bool changeNames = false;
    bool didFound = false;
   Students StructStudents[StudentsAmount];//Makes struct for each student



   //At this point stuff is
   //StudentAmount = HowMuchYouPicked.
   //tempChar[63] = name of student you are looking for to change its name
   //changeNames = true(keaps loop on if you want to change names)NOT INCLUDED IN HERE
   //didFound = will print text if pupil is found / else name of pupil = eror404^^

    didFound = LookForStudentAndSetName(StudentsAmount, tempChar, StructStudents);
}


Edit: Alredy found out it does change my students name
BUT! if students name was "a" and then i want to change it to "bill" it will change only existing amount of letters there fore changing "a" = "bill" will make "b"
how do i extend the name?

Edit2: Atm i wrecked search. Everything is still up, if you want to help.

Here is a link to project cut the spaces
http: //www. mediafire.com/ download.php?yv4k6837754io20
Last edited on
ReturnInput() is returning a char so only one char will be assigned to studentsName. In this case, you're dereferencing tempCharNameChange, which will give you the first character in the array (or the first character the user enters).

You should make ReturnInput() return a std::string and change it to use a std::string instead of an array of chars.

Also, you're invoking undefined behaviour here:

Students StructStudents[StudentsAmount];

as StudentsAmount is zero at the time.
Last edited on
//StudentAmount = HowMuchYouPicked.
as i said the StudentsAmount cant be less then 1 there for its always positive number.

as for the char problem, yea i will put all that to strings.


//char ReturnInput() im talking about this here
i thought i returned char pointer.(i return char there for i had error thats why i dereferenced it my bad).

Thank you a lot.

...5 min of work and everything works perfect.
Last edited on
Your escenario:
_ Architect: Here are the blueprints, I want a skyrocket of 0m height
_ Gimenez Carlos, Contractor: Ok, the building is done.
_ (Architect alters the blueprints.)

_ Nobody understand why the people are dying in the 42th floor.
Love the humor,
i was prototyping. That's why it looks like pile of junk.
Topic archived. No new replies allowed.