for loop Not outputting code

Pages: 123
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.
How would this look in code?

"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."
I've already shown you how the struct would look.

You make a vector of Employee objects just like a vector of any other type.

You already know how to use iterators to iterate over a vector.

So can you be a bit more specific about what you need to be shown code for?
I know how to make the struct im just confused how to use it within my program and where the code should go, what i should delete and stuff like that.
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.

Learn by doing.
Im extreamly confused on how to implement this although i have an idea. I created my struct

1
2
3
4
5
struct Employees
{
    string names;
    int percentage;
};


Now do i delete the two vectors and use the employee struct like this?

 
vector<Employees> emp_vect;


???

after that im stumped.
Last edited on
I get these errors

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> >()'|

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>

using namespace std;

struct Employees
{
    string names;
    int percentage;
};


int main()
{
    int amountMade;
    int numOfEmployees;
    int percentOfWork;

    vector<Employees> v_emp;
    vector<string>::iterator string_iter;

    vector<int> vect_percentage;
    vector<int>::iterator int_iter;

    cout << "Royalty Calculator" << endl;
    cout << "How much money was made this month from project?" << endl;
    cout << "$";
    cin >> amountMade;

    cout << '\n';
    cin.ignore(500, '\n');

    cout << "What are the employees names? Enter 'done' when finished" << endl;

    while(names != "done")
    {
        getline(cin, names);

        for(int i = 0; i < 1; i++)
        {
            v_emp.push_back(names);
        }
    }

    v_emp.pop_back();

    cout << '\n';

    cout << "What percent of work did each individual do?" << endl;

    int num = 1;
    int vect_Length = v_emp.size();
    int vect_Lenght_int = vect_percentage.size();

    for(string_iter = v_emp.begin(); string_iter != v_emp.end(); ++string_iter)
    {
        cout << num << " " << *string_iter;
        cout << " %";
        cin >> percentOfWork;
        vect_percentage.push_back(percentOfWork);
        cout << endl;
        num++;
    }

    cout << '\n';

    cout << "Payment to each worker" << endl;

    for(string_iter = v_emp.begin(); string_iter != v_emp.end(); ++string_iter)
    {
        cout << *string_iter << " $";
        for(int_iter = vect_percentage.begin(); int_iter != vect_percentage.end(); ++int_iter)
        {
            cout <<  *int_iter << endl;
        }
    }

    return 0;
}
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,
So i would have to go through my code and put v_emp[i].names in front of every instance of names?
Read this:
www.cplusplus.com/doc/tutorial/structures/
or
http://www.learncpp.com/cpp-tutorial/47-structs/

(forgive me if I get any terminology wrong:)

This is a struct definition:

1
2
3
4
5
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).

Hope I helped?
Last edited on
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.
Last edited on
Now why cant i use push_back?

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
#include <iostream>
#include <vector>

using namespace std;

struct Business
{
    string names;
    int percentage;
    float moneyEarned;
};


int main()
{
    vector<Business> v_business;
    vector<string>::iterator str_it;
    vector<int>::iterator int_it;

    Business B;

    cout << "How much money was made this month?" << endl;
    cin >> B.moneyEarned;

    cout << '\n';

    cout << "Now What are the employee names?" << endl;

    for(int i = 0; i < 1; i++)
    {
        getline(cin, B.names);
        v_business.push_back(B.names);
    }

    return 0;
}


C:\Users\Chay Hawk\Desktop\test\main.cpp|32|error: no matching function for call to 'std::vector<Business>::push_back(std::string&)'|

BTW do i need 2 different iterators or can i just do:

vector<Business>::iterator b_it;
Last edited on
Now why cant i use push_back?

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.
Ok i see now, it compiles, but now it wont let me compile this

1
2
3
4
for(str_it = v_business.begin(); str_it != v_business.end(); ++str_it)
    {
        cout << *str_it;
    }


I dont get it it says

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.
Ok now when i enter 3 names it just replaces all 3 with the last element of the vector

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
#include <iostream>
#include <vector>

using namespace std;

struct Business
{
    string names;
    int percentage;
    float moneyEarned;
};


int main()
{
    vector<Business> v_business;
    vector<Business>::iterator str_it;

    Business B;

    cout << "How much money was made this month?" << endl;
    cin >> B.moneyEarned;

    cout << '\n';
    cin.ignore(30, '\n');

    cout << "Now What are the employee names?" << endl;

    while(B.names != "end")
    {
        getline(cin, B.names);

        for(int i = 0; i < 1; i++)
        {
            v_business.push_back(B);
        }
    }

    v_business.pop_back();

    cout << '\n';

    for(str_it = v_business.begin(); str_it != v_business.end(); ++str_it)
    {
        cout << B.names << endl;
    }

    return 0;
}
closed account (Dy7SLyTq)
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 need to print the value pointed to in the vector by the iterator"

Im not sure what you mean by this can you be a little more specific, also i got rid of that for loop.

also what i meant about the names is that if i enter

Chay Hawk
Michael Gardner
end

it outputs like this

end
end

i also did

cout << *str_it < endl;

but that didnt work
Last edited on
The way your code is written at the moment:

- 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.
How do i output the vector though, i thought thats waht i was doing in the very last for loop?

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
#include <iostream>
#include <vector>

using namespace std;

struct Business
{
    string names;
    int percentage;
    float moneyEarned;
};


int main()
{
    vector<Business> v_business;
    vector<Business>::iterator str_it;

    Business B;

    cout << "How much money was made this month?" << endl;
    cin >> B.moneyEarned;

    cout << '\n';
    cin.ignore(30, '\n');

    cout << "Now What are the employee names?" << endl;

    while(B.names != "end")
    {
        getline(cin, B.names);

        v_business.push_back(B);
    }

    v_business.pop_back();

    cout << '\n';

    for(str_it = v_business.begin(); str_it != v_business.end(); ++str_it)
    {
        cout << *str_itr << endl;
    }

    return 0;
}
Pages: 123