for loop Not outputting code

Pages: 123
Aug 1, 2013 at 3:04am
I have a for loop at the end of my program and it wont output my code:

here is the problem for loop:



1
2
3
4
5
for(int i = 0, j = 0; i < vect_Length, j < vect_Lenght_int; i++, j++)
    {
        cout << vect_Employees[i] << " %";
        cout << vect_percentage[j] % amountMade << endl;
    }


here is the complete 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
#include <iostream>
#include <vector>

using namespace std;

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

    string namesOfEmployees;

    vector<string> vect_Employees;
    vector<int> vect_percentage;

    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 employee names? Enter 'done' when finished" << endl;

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

        for(int i = 0; i < 1; i++)
        {
            vect_Employees.push_back(namesOfEmployees);
        }
    }

    vect_Employees.pop_back();

    cout << '\n';

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

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

    for(int i = 0; i < vect_Length; i++)
    {
        cout << num << " " << vect_Employees[i];
        cout << " %";
        cin >> percentOfWork;
        vect_percentage.push_back(percentOfWork);
        cout << endl;
        num++;
    }

    cout << '\n';

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

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

    for(int i = 0, j = 0; i < vect_Length, j < vect_Lenght_int; i++, j++)
    {
        cout << vect_Employees[i] << " %";
        cout << vect_percentage[j] % amountMade << endl;
    }

    return 0;
}
Aug 1, 2013 at 3:13am
I recommend you look at the reference files for vectors. In short, that is not how you read values in a vector. You need an iterator, not just a standard number, to read specific values.

http://www.dreamincode.net/forums/topic/53183-vector-iterators/

That pretty much covers the whole issue you're having.
Aug 1, 2013 at 10:29am
Ok like this?

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

using namespace std;

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

    string namesOfEmployees;

    vector<string> vect_Employees;
    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(namesOfEmployees != "done")
    {
        getline(cin, namesOfEmployees);

        for(int i = 0; i < 1; i++)
        {
            vect_Employees.push_back(namesOfEmployees);
        }
    }

    vect_Employees.pop_back();

    cout << '\n';

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

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

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

    cout << '\n';

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

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

    for(string_iter = vect_Employees.begin(), int_iter = vect_percentage.begin();
        string_iter != vect_Employees.end(), int_iter != vect_percentage.end();
        ++string_iter, ++int_iter)
    {
        cout << *string_iter << " %";
        cout << *int_iter % amountMade << endl;
    }

    return 0;
}


It works now which is great, thanks for correcting me. Now im having a math problem, I have a math iq of 7 so if you can help me that would be much appreciated. In the very last for loop, this part:

1
2
3
4
{
        cout << *string_iter << " $";
        cout << *int_iter % amountMade << endl;
}



im trying to output the percentage of money each person will get, so i enter an amount in the beginning and all of the team members and then it outputs everyones pay but i cant seem to get it to do that.
Last edited on Aug 1, 2013 at 1:03pm
Aug 1, 2013 at 1:05pm
1
2
3
    for(string_iter = vect_Employees.begin(), int_iter = vect_percentage.begin();
        string_iter != vect_Employees.end(), int_iter != vect_percentage.end();
        ++string_iter, ++int_iter)

What is it you're trying to achieve with the condition on the second line of that snippet? Do you really want to use the comma operator there? I suspect you've misunderstood what the comma operator does, because it's hard to see how it makes sense there.
Aug 1, 2013 at 1:10pm
Im not sure i follow please explain.
Aug 1, 2013 at 1:11pm
You know which bit in a for statement is the loop condition, right? It's the second line of the snippet I quoted.

Why are you using the comma operator in that condition? Do you know what it actually does?
Last edited on Aug 1, 2013 at 1:12pm
Aug 1, 2013 at 1:12pm
I am using 2 different vectors in the for loop and i need to output them so thats why i am doing that. I thought the comma operator seperated stuff?
Aug 1, 2013 at 1:15pm
What do you mean by "separated stuff"?

What is it that you want that condition to test?

EDIT: Why don't you actually go and read up properly in what the comma operator actually does?
Last edited on Aug 1, 2013 at 1:16pm
Aug 1, 2013 at 1:18pm
I dont know what i mean by that, i guess i really dont know what it does. I dont know that either, someone told me the way i was reading values in a vector was wrong and so they gave me a link and i followed it exactly.

http://www.dreamincode.net/forums/topic/53183-vector-iterators/
Aug 1, 2013 at 1:20pm
Well, in that case I'd recommend learning what the operators you're using actually do, rather than just guessing.
Aug 1, 2013 at 1:38pm
Ok i read up on it and it is a seperator in some contexts but an evaluator in most contexts. i see now. well that for loop is supposed to output the contents of the vector so i guess i could do a nested for loop but isnt there a way to do an advanced for loop with that code? thats what i was trying to do but i guess that wont work, so i guess this would be the more sensible solution?

1
2
3
4
5
6
7
8
for(string_iter = vect_Employees.begin(); string_iter != vect_Employees.end(); ++string_iter)
    {
        cout << *string_iter << " $";
        for(int_iter = vect_percentage.begin(); int_iter != vect_percentage.end(); ++int_iter)
        {
            cout <<  amountMade % *int_iter << endl;
        }
    }
Last edited on Aug 1, 2013 at 1:38pm
Aug 1, 2013 at 2:05pm
If you do a nested loop, then what well happen is that the inner loop will loop over all the members of vect_percentage for every member of vect_Employees. So if there were 5 elements in vect_Employees and 7 elements in vect_percentage then the inner loop would iterate 7 times for vect_Employees[0], then another 7 times for vect_Employees[1], etc - for a total of 35 times.

Is that what you want?
Last edited on Aug 1, 2013 at 2:05pm
Aug 1, 2013 at 2:11pm
No i want it to show the names of the employees stored in vect_Employees, then calculate the percentages i entered for each person to see what they would get out of the amount of money i entered in the beginning, so lets say i enter $900, then i enter the employees names, then the percentage of work the employees did, then the program calculates what percent they would get and output it.
Aug 1, 2013 at 2:39pm
Well, the devil is in the details. From the way you've described it, it sounds like the output would look like:


Adam
Barbara
Chris
Daisy
100
235
163.6
43.99


Whereas from looking at your code, it looks like what you're trying to get is:


Adam 100
Barbara 235
Chris 163.6
Daisy 43.99


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.
Aug 1, 2013 at 3:38pm
well this is a program i am using in real life for my indie game company to compute the royalty earnings of my employees so i cant have fixed values or names.
Aug 1, 2013 at 4:25pm
Who's talking about using fixed values and names?

I'm saying that instead of having one vector that contains names, and another that contains earnings, you should define a struct type that bundles together that information for an employee, and then have a single vector of those structs.
Last edited on Aug 1, 2013 at 4:25pm
Aug 1, 2013 at 11:06pm
because i dont know how. How do i do it? I know structs are like classes with the difference that structs members are public by default.
Aug 1, 2013 at 11:15pm
You don't know how to write a struct? To be honest, you'd be better off reading a proper C++ book to learn this stuff, rather than asking piecemeal questions on a forum.

The struct is really simple:

1
2
3
4
5
struct Employee  // This is the name of the type
{
  string name;
  float percentage;
}
Aug 2, 2013 at 1:36am
And how exactly do i use it in my current program like you suggested earlier?
Aug 2, 2013 at 8:20am
Make a single vector of Employee structs, rather than the two vectors you have now, so you only have to worry about iterating over a single vector. Store the name and percentage of each employee in the struct for that employee.

If you don't know how to use structs, then I'll reiterate my previous comment:

you'd be better off reading a proper C++ book to learn this stuff, rather than asking piecemeal questions on a forum.


Edit: you say you have a games company - you mean a computer games company? As in, you have actual software developers already working for you?
Last edited on Aug 2, 2013 at 8:22am
Pages: 123