Can i do this?

Aug 27, 2012 at 10:22pm
Ok so i am working on pointers and its going well but referencing stuff in the constructors can get hectic, especially when i have 7 different things that need to be referewnced. Im looking for a way to reference all this stuff without using classes, making them global or traditional referencing. What i was thinking of is this:

Ok so lets say i have 5 variables: int a, int b, int c, string d, string e.

Now instead if putting them all in my constructors of the functions i want to use i was thinking of something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void function(int *hold)

int main()
{
   int a = 12;
   int b = 13;
   int c = 56;
   int d = 67;
   int e = 1;

   int hold;

   hold = function(int &a, int &b, int &c, int &d, int &e);
}

void function(int *hold)
{
  cout << *hold << endl;
}


So basically i want to be able to reference my variables withou having to type in all of them in my function constructors, can something like this even work?
Last edited on Aug 27, 2012 at 10:23pm
Aug 27, 2012 at 10:28pm
No. You have to pass in the parameters the function expects. In this case, a pointer to an integer.

Moreover, there are a couple of other errors there. Namely that you wouldn't include data types when calling a function and that you're trying to assign a void function's return value (or lack thereof) to a variable.

Out of curiosity, why wouldn't you just use a class, struct or array?
Last edited on Aug 27, 2012 at 10:29pm
Aug 27, 2012 at 10:29pm
First of all, constructors are only for classes.

and if you want to pass a variable by reference to a function, something like this example will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int add(int&,int&);//function prototype

int main()
{
     int x,y;

     add(x,y);
     cout<<"Ans: "<<x+y;
     cin.ignore();
     cin.get();
}

int add(int& a,int& b)
{
     cout<<"Enter a number: ";
     cin>>x;
     cout<<"Enter another one to add to the first number: ";
     cin>>y;
}


When you pass a variable by reference, you are actually editting the real variable. For more info, read: http://cplusplus.com/doc/tutorial/functions2/
Last edited on Aug 27, 2012 at 10:33pm
Aug 27, 2012 at 11:32pm
Is this better

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

using namespace std;

void start(int *S, string *N);

struct str
{
    int Generic;
    string name;
};

int main()
{
    str s, n;
    s.Generic = 0;
    n.name;

    cout << "Enter your name" << endl;
    getline(cin, n.name);

    start(&s.Generic, &n.name);
}

void start(int *S, string *N)
{
    string choice;

    cout << "Ok " << *N << " type something in, the longer it is the more\npoints you get." << endl;

    do
    {
        getline(cin, choice);

        cout << "\n";

        cin.sync();
        *S += choice.length();

        cout << *S << endl;
    }while(choice != "quit");
}
Aug 27, 2012 at 11:48pm
What's the use of this : n.name; ?
Aug 28, 2012 at 12:05am
for the player to enter their name getline(cin, n.name);
Topic archived. No new replies allowed.