return struct function

i can't manage to return the naga.x value in the line 55 function

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
#include <iostream>
#include <cstring>
using namespace std;

struct newship{
string name;
int x;
int y;
int power;
};

newship spawn(){
newship ship;
ship.x=0;
ship.y=0;
ship.power=10;
return ship;
}

newship upgrade(newship ship){
ship.power+=2;
return ship;
}

newship orizontal_movement(newship ship){
ship.x+=2;
return ship;
}

int movetolimit(newship naga);

int main (){
int operation;
newship naga=spawn();
cin >> naga.name;
cout << naga.name << endl;
cout << "select operation:\n1-move " << naga.name << " by 2 units.\n2-move " << naga.name << " to the end of the sceen" << endl;
while(operation!=1&&operation!=2){
    cin >> operation;
    if(operation!=1&&operation!=2){
        cout << "choose between 1 and 2" << endl;
    }
}
if(operation==1){
    naga.x+=2;
}
else{
    movetolimit(naga);
}
cout << "\n" << naga.x << endl;
}

int movetolimit(newship naga){
for(int i=0;i<10000;i++){
if(naga.x>=420){
    break;
}
naga=orizontal_movement(naga);
}
return naga.x;
}
You call movetolimit(naga); but you don't store the returned value anywhere. If you want naga.x to get the returned value you would have to do it like this:

 
naga.x = movetolimit(naga);


I can understand if you think it looks ugly. If you want to call the function simply the way you have it you could change the function to take the argument by reference which means the function will be able to modify the object that you passed to it.

1
2
3
4
5
6
7
8
void movetolimit(newship& naga){
	for(int i=0;i<10000;i++){
		if(naga.x>=420){
			break;
		}
		naga=orizontal_movement(naga);
	}
}


Note that & was added after the parameter type to make it a reference, and the return type was changed to void because it doesn't look like you are interested in the return type.
Your code copies newships quite a lot. Also, most of the functions would be more appropriate as newship methods in my opinion:
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
#include <iostream>
#include <cstring>
using namespace std;

struct newship{
    string name;
    int x;
    int y;
    int power;

    newship();
    void upgrade();
    void orizontal_movement();
    void movetolimit();
};

newship::newship() :
    x(0), y(0), power(0)
{;}

void newship::upgrade() {
    power+=2;
}

void newship::orizontal_movement(){
    x+=2;
}

int main (){
    int operation;
    newship naga;
    cin >> naga.name;
    cout << naga.name << endl;
    cout << "select operation:\n1-move " << naga.name << " by 2 units.\n2-move " << naga.name << " to the end of the sceen" << endl;
    while(operation!=1&&operation!=2){
        cin >> operation;
        if(operation!=1&&operation!=2){
            cout << "choose between 1 and 2" << endl;
        }
    }
    if(operation==1){
        naga.orizontal_movement();
    }
    else{
        naga.movetolimit();
    }
    cout << "\n" << naga.x << endl;
}

void newship::movetolimit(){
    for(int i=0;i<10000;i++){
        if(x>=420){
            break;
        }
        orizontal_movement();
    }
}


movetolimit() appears inefficient. If you want to move until x=420 then just set x=420.
Topic archived. No new replies allowed.