I have a C++ book called Sams teach yourself C++ in 24 hours, ill get reading that more.
Yes computer games. No we dont start from scratch we use the Unreal Development Kit. Im a level designer myself, but the guy that does the programming uses Unreal Script.
"Since there is a direct one-to-one relationship between employee and percentage, why are you confusing things by trying to manage two vectors at once? Why not have a struct that contains the name and the earnings of each employee, and then have a single vector of structures? That way, you only have to manage a single iterator."
Just use the vector of structs instead of the two vectors you're using now. You've already worked out (mostly) how to do it with two separate vectors; using one single vector will be simpler, not more complicated.
C:\Users\Chay Hawk\Desktop\test\main.cpp|35|error: 'class std::vector<Employees>' has no member named 'names'|
C:\Users\Chay Hawk\Desktop\test\main.cpp|37|error: 'names' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\test\main.cpp|55|error: no match for 'operator!=' in 'string_iter != v_emp.std::vector<_Tp, _Alloc>::end<Employees, std::allocator<Employees> >()'|
You often refer to "names" as if it were a variable you've declared...but it isn't. If you're trying to access a name from the vector, you'll need something like v_emp[i].names; if you want to read a string you'll need to declare, say, a temporary structure or string to read into,
struct Employees
{
string names; // these are
int percentage; // members
};
It is not a variable to access. What you do is create a variable of type struct (type Employees in this case). You'll make one per employee:
1 2
Employees empl1; // kinda like doing: int myint;
Employees empl2; // ...but ints don't have members
Now we can access their members - which we must define first. The string and int in the struct are the members, they're unique to each variable of type Employees. You access them through . member access. So lets assign them like this:
1 2 3 4 5
emp1.names = "Buddy";
emp2.names = "Holly";
/* could've also read them in:
cin >> emp1.names;
... */
Now when we access them:
1 2
cout << emp1.names;
cout << emp2.names;
They will show their respective names as defined earlier. As you can see, every Employees object we create has it's own set of member variables that don't conflict with another Employees variable.
If I went on, I'd be giving you a tutorial. Rather, have a read of those links. If you're serious about C++ I suggest you get away from that book and get a proper one. Learning in 24 hours is impossible. Reading, yes, reading a book in 24 hours isn't, but the knowledge will go out as quick as it comes in. Learning C++ (and being good at it) takes plenty time. Learning C++ in 24 hours will not help you use Unreal Script any quicker either (in case thats what you wanted).
Im still stuck on where it actually GOES in the code what portions to modify and what parts to delete, i just tried to do it and got a million template errors and its just turning into a bigger mess. The book doesnt actually cover the entire c++ language in 24 hours, it has 24 lessons each taking an hour to complete. Also im not trying to learn Uscript, just C++. Im just going to start over on my program using the struct and see how it turns out because trying to edit this code is a nightmare.
Because v_business isn't a vector of strings, it's a vector of Business structs. You need to push a Business onto it, not a string.
Treat a vector of structs just like a vector of any other type. You'd push_back an int onto a vector of ints, and a string onto a vector of strings, so push_back a Business onto a vector of businesses.
BTW do i need 2 different iterators or can i just do:
vector<Business>::iterator b_it;
Again, a vector of Businesses is just like a vector of any other type. You iterate over it like any other type of vector. So, yes, just like that.
C:\Users\Chay Hawk\Desktop\test\main.cpp|38|error: no match for 'operator<<' in 'std::cout << str_it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<Business*, std::vector<Business> >()'|
*str_it is a Business - i.e. a struct. You can't just "print out a struct" - that's meaningless. You need to print the elements of the struct that you want to print.
whats up with that for loop at line 33? its useless. and of course it only says the last name. thats all b.names holds. you need to print the value pointed to in the vector by the iterator
- you create a single structure, called B
- you store the money earned this month in it
- you then keep looping, taking user input. For each name the user enters, you:
- change the name stored in B to hold the new name
- put a copy of the updated B onto the vector (inside a weird second loop that only loops once - why?)
So, by the end of the loop, you have:
- a vector of structs, with three elements in it
- a struct B, with the word "end" stored in the name.
You then delete the last element in the vector with a call to pop_back() - presumably, because that's the one where the user entered "end", so it's not meant to be a real Business.
What's going wrong is your final loop where you output the names. You iterate over the vector, but you don't print out the names stored in the vector. You're just printing out the name stored in B over and over again. And, since the last name to be stored in B is always "end", "end" is what you're printing out.
You need to print out the values of names stored in the vector, not the one stored in B.