Hello once again guys! I have one more problem regarding another code of mine. My code is given below and the problem is that when I run the code I does not print the names of the students. In addition, it does not print the part of the code which states how many vacancies in the team are there. Somewhere within this section of the code there is a problem but I can't spot it. COuld you please advice?
Id assume you are creating a 5 element name array 0-4 and then attempting to access name[5] which makes it exit the loop b4 it enters the loop in show or even sets the name array.
I would really like to put your code through a debugger and see what happens, but with out the "include" files you used I can only guess.
A note:
1 2 3 4 5 6 7
class date {
private: // Not needed as the class is private by default until changed. OK if you leave it.
double imera{};
double minas{};
double etos{};
public:
date(){};
On line 59 is that a lower case "L" or the number (1)? It is best to avoid using the lower case "L" unless it is part of a larger word.
At the moment I am think that the problem may start in the for loop in line 63 or just the class function.
After the first run line 90 stuck out. This sets "studentnumber" before you even get to the function "set_names" where the if statement fails because "studentnumber" is 5 and 5 is not less than 5.
When I put the comment on line 90 the first output was:
give a name
asdf
Name is set! there are still 4vacanies
Name isnt't set
give a name
qwer
Name is set! there are still 3vacanies
Name isnt't set
give a name
zxcv
Name is set! there are still 2vacanies
Name isnt't set
give a name
yuio
Name is set! there are still 1vacanies
Name isnt't set
give a name
jkl;
Name is set! there are still 0vacanies
Name isnt't set
The title is: Nanofluids
hand over date:
22/9/2020
Grade is :10
foitites: 5
asdfqwerzxcvyuiojkl;
With the adjustments I made it now looks like this:
Give a name: asdf
Name is set! there are still 4 vacanies
Give a name: ghjk
Name is set! there are still 3 vacanies
Give a name: jkl;
Name is set! there are still 2 vacanies
Give a name: qwer
Name is set! there are still 1 vacanies
Give a name: zxcv
Name is set! there are still 0 vacanies
The title is: Nanofluids
hand over date: 22/9/2020
Grade is : 10
foitites: 5
asdf ghjk jkl; qwer zxcv
Not sure what you had in mind for line 52. It gives me the impression that it would be part of an if/else.
@markyrocks: Thank you markyrocks. The thing is that it normally accepts the five names and no more names, without any prompt or error because it does not give me an cin to enter a 6th name (which is the name[5] element). I didn't quite get what you mean?
@Andy: Andy thanks a lot. Yes I omitted the headers and I apologize. I am giving you the headers in the following lines. No on line 59 it is a (1) not a lowercase L. I am suspecting two code sections: The first is the for loop in line 63 in which I didn't manage to integrate the i variable in the set_names , or, second the set_names fucntion section (lines 31 - 37). For some reason though I can't find the problem.
In regards to what markyrocks said. The array std::string names[5] creates an array of 5 elements. The elements are numbered 0 - 4 because C++ is a zero based language. So using names[5], other than defining the variable, means that you are trying to access the array past the end of the array. This is known as undefined behavior because you have no idea what bit of memory you are trying to access. It is even worse if you write to this memory.
Line 4 I did not include in what I posted, so again it is not needed.
Line 5. Anything that you might use from this header file should be covered under the "iostream" header file.
There is no point in including what you do not need unless you have yet to code something that might need 1 of these header files.
The first is the for loop in line 63 in which I didn't manage to integrate the i variable in the set_names , or, second the set_names function section (lines 31 - 37).
I understand you idea, but if you set "studentnumber" to zero when the object is created it is not a problem. You already did this in your original code line 21.
Since the function "set_names" increment "studentnumber" this is not a problem.
Ok guys, to begin with, thank you all for your help. Now, I forgot to enter the "else" condition, which I already corrected. Then I looked back to the post to see the replies and I noticed most of you spotted the "e1.set_sn(5);" I used. I corrected this and the code runs fine. However, I have a question regarding this correction.
My goal was to start with setting a class member string names [5] which created an array of 5 strings. I went on by writing the set function for accessing my private name[5] member and I wanted to set the condition that if number of students is going to be less than 5 then the name I will try to set will be set, else it won't! Finally, I used the e1.set_sn(5); so I can set five names (from 0 to 4).
Therefore, my question is, since the string array has 5 elements (0-4) why did the compiler considered "e1.set_sn(5)" as out of bounds?
Therefore, my question is, since the string array has 5 elements (0-4) why did the compiler considered "e1.set_sn(5)" as out of bounds?
Actually the compiler did not consider this to be out of bounds. The function call and function did exactly what it is supposed to do.
The problem is with the if statement in the function "set_names". You wrote: if (studentnumber < 5). To look at it differently: if (5 < 5). This would return false.
By setting the value of "studentnumber" in main before you call "set_names" you are defeating the purpose of the function.
When the program starts the variable "studentnumber" has to have a value of (0) zero for "set_names" to work properly.
Learning how to use the debugger is a great help to see what values variables contain as the program runs.
Or try this:
1 2 3 4 5
void set_names(std::string nm)
{
std::cout << "\n The student number is: " << studentnumber << '\n'; // <--- Used for testing. Comment or remove when finished.
if (studentnumber < 5)