1

I am having what seems to be a common issue however reading through the replies to the similar questions I can't find the solution to my issue at all as I have already done what they are suggesting such as making the variable an array. I have the following code:

#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <future>
using namespace std;

string eng2Str[4] = { "money", "politics", "RT", "#"};
int resArr[4];

int main()
{

    engine2(eng2Str[4], resArr[4]);

    system("Pause");
    system("cls");

    return 0;
}

void engine2(string &eng2Str, int &resArr)
{
    ifstream fin;
    fin.open("sampleTweets.csv");
    int fcount = 0;
    string line;

    for (int i = 0; i < 4; i++) {
        while (getline(fin, line)) {
            if (line.find(eng2Str[i]) != string::npos) {
                ++fcount;
            }
        }
        resArr[i] = fcount;
    }

    fin.close();

    return;
}

Before you mark as duplicate I have made sure of the following:

  • The array and variable I am trying to assign are both int
  • Its an array

The error is:

expression must have pointer-to-object type

The error is occurring at the "resArr[i] = fcount;" line and am not sure why as resArr is an int array and I am trying to assign it a value from another int variable. I am quite new to C++ so any help would be great as I am really stuck!

Thanks!

7
  • Did you really mean to put a loop within a loop? Commented May 31, 2017 at 23:32
  • you have to paste the exact error message Commented May 31, 2017 at 23:34
  • Post compilable code. Commented May 31, 2017 at 23:34
  • Yes I did mean to put a loop within a loop as I need it to run through the file searching for several different keywords counting how many times they are used one after another. I have updated the original code with everything I have. Commented May 31, 2017 at 23:38
  • 1
    Declaring array as dimension 4 means the valid indices are 0 through 3. So when you later write eng2Str[4] as function argument, you are accessing out of bounds. (Same with resArr[4]) Commented May 31, 2017 at 23:44

2 Answers 2

2

The problem is that you've declared your function to take a reference to a single string and int, not arrays. It should be:

void engine2(string *eng2Str, int *resArr)

or:

void engine2(string eng2Str[], int resArr[])

Then when you call it, you can give the array names as arguments:

engine2(eng2Str, resArr);

Another problem is the while loop in the function. This will read the entire file during the first iteration of the for() loop. Other iterations will not have anything to read, since it will be at the end of the file already. You could seek back to the beginning of the file, but a better way would be to rearrange the two loops so you just need to read the file once.

while (getline(fin, line)) {
    for (int i = 0; i < 4; i++) {
        if (line.find(eng2Str[i]) != string::npos) {
            resArr[i]++;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply. The change in the loop has me a bit confused. Am I correct in saying that it will still run through the entire file 4 times searching for each of the 4 strings within the eng2str array and increasing the corresponding resArr value for each time is it found?
No. It goes through the file 1 time, and on each line it searches for 4 strings.
O god that is far more efficient... you sir are amazing thank you!
I am trying to do something similar in a different function where I am searching a file for specific words but need to return each line that it is found in. I am thinking I am going to need a multi-dimensional array with the first being the keyword that was searched and the second being the result but I can't get my head around how this would work. Any help would be amazing!
@KaiJones Use std:map<std::string, int>
1

I would suggest to use std::vector instead of pure C array. In your code, there are more issues. You are passing the fourth element of both arrays to the engine2 function. From your definition of void engine2(string &eng2Str, int &resArr) you expect reference to a string (not array / vector) and an address / reference of int - you need to pass an pointer to the first element of resArr.

#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <future>

using namespace std;

vector<string> eng2Str = { "money", "politics", "RT", "#" };
int resArr[4] = {};

void engine2(const vector<string>& eng2Str, int* resArr)
{
    ifstream fin;
    fin.open("sampleTweets.csv");
    int fcount = 0;
    string line;

    for (int i = 0; i < 4; i++) 
    {
        while (getline(fin, line)) 
        {
            if (line.find(eng2Str[i]) != string::npos)
            {
                ++fcount;
            }
        }
        resArr[i] = fcount;
    }

    fin.close();

    return;
}

int main()
{

    engine2(eng2Str, resArr);

    system("Pause");
    system("cls");

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.