Only "assigns" the number to the variable if I cout?

I have a variable I'm trying to set to a certain number, but it won't do it unless I cout that variable first. The details are in the main 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
#include <iostream>
#include <string>

using namespace std;

int secondFunction (string theArray[9], int num ) {
    int counter;
    for ( int i = 0; i <= 7; i++ ) {
        cout << "Enter in name " << i+1 << ": ";
        cin >> theArray[i];
        counter++;
    }
    cout << endl;
return theArray, counter;
}

int thirdFunction ( string anotherArray[9], int limit ) {
    cout << limit;
    for ( int i = 0; i < limit; i++ ) {
        cout << "Name " << i + 1 << " is " << anotherArray[i] << endl;
    }

}

int main() {

    int counter;
    string myArray[9];
    myArray, counter = secondFunction ( myArray, 9 );
    /*counter only equals 8 when I cout << counter; here
    Otherwise, in thirdFunction, it outputs a blah number where I put cout << limit;.*/
    thirdFunction ( myArray, counter );

}
Last edited on
There are several problems here. First, at line 7, int counter; the variable counter is defined but it is not initialised, hence its value is garbage. It should be given some initial value, zero makes sense from the context.
 
int counter = 0;


Next, at line 14, the return statement is odd. The function secondFunction has been defined with a return type of int which means it will be able to return a single value of type int.
 
    return theArray, counter;
should be simply:
 
    return counter;


And finally, in function main() at line 29, a rather strange assignment statement,
 
    myArray, counter = secondFunction ( myArray, 9 );

which should be simply
 
    counter = secondFunction ( myArray, 9 );


It think that should fix things reasonably well. Though function secondFunction() is still a little puzzling, as it receives the parameter num but does not use it, and instead has a hard-coded value of 7 in the for loop.
First error on line 14, change:
return theArray, counter;
to:
return counter;

Similarly line 29, change:
myArray, counter = secondFunction ( myArray, 9 );
to:
counter = secondFunction ( myArray );

The main problem is that:
int secondFunction (string theArray[9], int num )
does not modify the callers string's, but modifies the copy passed to it. So the setting of theArray in line 10 is lost on return. Using a reference will fix this.

Change:
int secondFunction (string theArray[9], int num )
to:
int secondFunction (string (&theArray)[9]) // reference to an array of 9 strings

Change the 7 in line 8.
Last edited on
The main problem is that:
int secondFunction (string theArray[9], int num )
does not modify the callers string's, but modifies the copy passed to it.

What is passed is a pointer to the start of the array. There is no copy here. The array is the data. The code as it stands already works correctly regarding the passing of the array.

See "Arrays as parameters" here,
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Topic archived. No new replies allowed.