C++ main and sub programs

Hi,
My problem is that the data requested in the main program is not passed correctly to the sub program where the data is printed.

The whole code is one big mess, so I won’t put it here all the way. However, here is an illustration of how it is now built:

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
/* Main funktion */

{ here is struct }

int print(Student, int num)

int main()
{
Student info;
const int max = 20;
int num;

cout << "How many x you want: ";
cin >> num;

for (int nro = 0; nro < num; nro++)
{
Here is many lines
}

print(info, num);

return 0;
}

int print(Student info, int num)
{
float grade;
float totalgrade;

if (info.Struct1 = ...)
{
lines
}
else if (info.Struct1 = ...)
{
lines
}

totalgrade = rules here;

for (int nro = 0; nro < num; nro++)
{
Here we print what we asked in main section
}

return 0;
}


The problem is that the sub program does not print the data entered by the user in the order they should appear in the main program loop. If the user enters at least 2 data; data x and y, then the sub program prints x data two times.

I really hope someone understand my horrible english and understood my problem.
Hello Scorpia,

Although you have made a valiant attempt to cut your code down it does not work. It is best to post code that can be compiled and tested so anyone working on it can see how it works and what it is doing.

Going over your code please read the comments:
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
/* Main funktion */

{ here is struct }  // <--- What does it look like?

int print(Student, int num)  // <--- Nice start for a forward declaration or prototype, but fails. Needs a ;.

int main()
{
    Student info;
    const int max = 20;  // <--- What is this used for?
    int num{};  // Always a good idea to initialize your variables.

    cout << "How many x you want: ";
    cin >> num;

    for (int nro = 0; nro < num; nro++)
    {
        Here is many lines  // <--- What are they?
    }

    print(info, num);

    return 0;
}

int print(Student info, int num)
{
    float grade;
    float totalgrade;

    if (info.Struct1 = ...)  // <--- = means set, which will not work, and == means compare, still will not work.
    {
        lines
    }
    else if (info.Struct1 = ...)
    {
        lines
    }

    totalgrade = rules here;

    for (int nro = 0; nro < num; nro++)
    {
        Here we print what we asked in main section
    }

    return 0;
}

Prefer to use "double"s over "float"s.

You need to post the whole code so I can have a better idea of what it is doing.

Andy
I understand, i will explain better next time. This problem solved. :)
Topic archived. No new replies allowed.