-2
struct ventasType
{
    string iD;
    double quarterSales, totalSales;
};

void readingID(int counter) {

    ventasType sales;
    ifstream inFileID;
    counter = 0;

    inFileID.open("id.txt");

    inFileID >> sales.iD;

    while (!inFileID.eof())
    {
        inFileID >> sales.iD;
        counter++;
    }

    cout << "Total de vendedores:" << counter;
}


int main()
{
    ventasType sales;
    int count;
    readingID(count);
    cout << count;

    return 0;
}

When I try to print count it prints a memory location. I'm i doing this right? I'm trying to count hoy many sales people are in the text file. Did this code but its not running correctly. it prints a memory location. The programming language is c++.

Kentaro Okuda
  • 1,446
  • 2
  • 10
  • 15
  • 1
    `readingID` takes `count` by value, there's no way it can change the variable in `main`. *That* `count` will always remain an uninitialised variable with indeterminate value. – Jesper Juhl Nov 26 '19 at 22:01
  • 1
    Count is uninitialized garbage when you print it. As @Jesper mentioned, its passed by value, not by reference. Give this a read [pass by reference](https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.cbclx01/cplr233.htm) – Kai Nov 26 '19 at 22:02
  • what do you mean there's no way it can change the variable in main? – Leomar Rodriguez Gonzalez Nov 26 '19 at 22:03
  • Didn't I say? It is taking the argument by value (copy). It cannot modify the original. Did you mean to pass by reference or pointer? – Jesper Juhl Nov 26 '19 at 22:05
  • The question could be made easier to understand with a little more attention to formatting of the code. There is no indentation of lines within readingID, for instance. Then there is mixed indentation in main. – Rich von Lehe Nov 26 '19 at 22:06
  • 1
    @Jesper is trying to tell you that within the scope of `ReadingID(){}`, it cannot change the value of Main's `count` because they are not the same `int` – Kai Nov 26 '19 at 22:12
  • You probably should change this `void readingID(int counter) {` to `int readingID()`. In addition I don't like the naming but I will leave that up to you. – drescherjm Nov 26 '19 at 22:31
  • Unrelated: Recommended reading: [Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Nov 26 '19 at 22:35

2 Answers2

0

You have to pass variable count by pointer or reference. The easiest way to do it is to change

void readingID(int& counter) 

This way you pass reference to variable without making a copy Also I would like to suggest to initialize variable firstly before using it: int count{0};

Kentaro Okuda
  • 1,446
  • 2
  • 10
  • 15
Jonas Kibildis
  • 111
  • 1
  • 6
0

The following code cout << "Total de vendedores:" << counter; should be printing correctly. However, if you want your code cout << count; to print correctly, you need to pass the variable by reference or have your function return the value.

To pass the variable by reference, change void readingID(int counter) to void readingID(int& counter).

To change your function to return the total number salesmen, change to int readingID(), add return counter; at the end of your function, and set your counter variable in main to the value counter = readingID();