last day (16 days later) » 

7:04 PM
-1
Q: Problem at overloading operator "<<" not reading all the attrbutes

Damian NarcisI'm doing a homework for university. I created a class and overloaded >> and << operators. I am trying to read the object with the overloaded operators. I am trying to input the attributes. I input the number of sites, then i am asked to input the site names, after i write the first one's name, t...

 
Have a look at how to make a minimal reproducible example. The idea is that I should be able to copy-paste the program, compile it and get the same result/error that you see.
 
Unfortunately, because the shown code fails to meet all requirements for a minimal reproducible example that anyone can use to reproduce your problem, themselves, it is unlikely that anyone will be able to help you. For more information, see How to Ask.
 
An operator >> shouldn't have input prompts in it. What if someone just wants to construct a Game without prompts, just data? If any of those cin calls fail or do unexpected things, your Game is either corrupted or has invalid data. Also having input prompts in the middle of code we need to see makes it harder for anyone to help, since they have to sit at their keyboard and type in the faulting data over and over again to run the test(s).
 
@PaulMcKenzie This is how the teacher showed us that we construct the function so should i try without input prompts?
 
Well, I give your teacher a demerit. We have no idea what data you used to initialize your object. Without input prompts would require a slick usage of std::istringstream and std::string, since your function takes an istream reference.
 
7:04 PM
well seems like i don't understand that site so good, ok, i will not be able to ask again for several days
@PaulMcKenzie isn't the initialization made by the constructor ?
 
@DamianNarcis -- No. The user has to run the gauntlet of an operator >> to set any of the members. See this for an example of simulating user input without sitting at the keyboard. The trick is to put the entire input into a string separated by newline characters, and then stream that string using istringstream into operator >>.
 
@PaulMcKenzie ok so no1 will be able to answer my question ? can i delete it then ?
 
All it takes is what I posted as a link. Put the data in a string and use that string as the input. Your operator >> takes an istream reference anyway, so it is quite capable of doing what I posted.
 
It looks like your teacher is doing less than stellar job.
"asked to input the site names". Where? The code doesn't mention anything that resembles site names.
 
well rip graduation
i have to idea what u guys are talking about .. @n.'pronouns'm.
 
7:04 PM
"i am asked to input the site names". Which lines of your code are responsible for that?
 
``` for (int i = 0; i < g.gameNoSitesRating; i++) { cout << "The site number " << i + 1 << ' ' << endl; in >> g.gameRatings[i]; } ```
 
This fragment is about some ratings, not about names.
 
no, the for is going from 0 to the number of ratings, then the sites get read @n.'pronouns'm.
 
If you wish to call int *gameRatings "sites", be my guest, but I don't know how to continue from this point. Good luck.
 
the ratings are made by sites, u enter the number of sites then the name of the sites that are rating it @n.'pronouns'm.
 
7:04 PM
Let's try again, from the beginning. What's a name? Can you give an example? How would you declare a variable that holds a name? Can you show me one such variable in your program?
 
string name ="Damian"
There is no variable in my program since name will be read from the keyboard when the object g1 is read
 
Sorry I give up.
 
Removing the unnecessary prompts shows no error. The prompts, given that the >> takes std::istream, makes no sense anyway. The std::istream is designed to take data from any stream, not just from keyboard. Thus the prompts are out-of-place for something like what I posted.
 

  last day (16 days later) »