2

I'm trying to create something similar to string.find with vectors c++98, but I can't fix this error. I have simplified the code and left only the error. If I can get help on how to achieve this.

#include <iostream>
#define MAX 99999
using namespace std;
int free_pos (int v[]);
int find_line (int v[], int pos);
int main() {
 char text[MAX];
 int loc_key[MAX],start_line[MAX]; //
 for(int i=0; i<free_pos(loc_key);i++) {
 cout << "Line: " << i+1;
 int pos = find_line(start_line[],loc_key[i]); //Here is the error
 for(int j=start_line[pos];j<start_line[pos+1];j++) {
 cout<<text[j];
 }
 cout<<endl;
 }
 return 0;
}
int free_pos (int v[]) {
 for(int i=0;i<MAX;i++){
 if(v[i] == 0)
 return i;
 }
 return 99;
}
int find_line (int v[], int pos) {
 for(int i=0; i<free_pos(v); i++) {
 if(v[i]==pos)
 return v[i];
 if(v[i]< pos)
 return v[i-1];
 }
}
Ahmad
1,8846 gold badges30 silver badges60 bronze badges
asked Nov 23, 2019 at 17:26
8
  • 3
    What do you expet start_line[] to do? Commented Nov 23, 2019 at 17:27
  • that has the index of the char that begins each sentence Commented Nov 23, 2019 at 17:28
  • 2
    I guess you meant start_line[i] instead? Commented Nov 23, 2019 at 17:29
  • 2
    find_line(start_line[],loc_key[i]); -> find_line(start_line,loc_key[i]); Commented Nov 23, 2019 at 17:35
  • 1
    Does this answer your question? error: expected primary-expression before ']' token Commented Nov 23, 2019 at 18:33

2 Answers 2

2

Your both functions

int free_pos (int v[]);
int find_line (int v[], int pos);

take arrays as an input, but then you try to call int pos = find_line(start_line[],loc_key[i]);, which should take the name of an array only, as it is the name which is known by the program as an array. You can easier (in my opinion) get it, if you write both of these functions following way:

int free_pos (int* v);
int find_line (int* v, int pos);

They do the same job as your functions, but you can see that their arguments are: an int pointer (array) and an int. Then, calling the function needs only the int pointer, which in your case is start_line, and the integer loc_key[i]. If you still have problems understanding it, you could read about dynamic memory allocation of arrays, which is done as: int* arr; arr = new int[your_size]; - I think it is clearer while thinking that way.

answered Nov 23, 2019 at 17:56
Sign up to request clarification or add additional context in comments.

Comments

0
#include<bits/stdc++.h>
using namespace std;
int max(int arr[],int n){
maximum=INT_MAX;
for(int i=0;i<n;i++){
if(arr[i]>maximum){
maximum=arr[i]}}
return maximum;}
int main(){
int size;
cin>>size;
for(int i=0;i<size;i++){
cin>>arr[i]}
cout<<max(arr,size);
return 0;}
answered Aug 28, 2022 at 20:03

1 Comment

1. Never recommend the use of #include<bits/stdc++.h> in an answer on Stack Overflow; read this and then replace that line with the relevant standard headers. 2. Add some indentation and decent formatting to your code. 3. Add an explanation as to why/how your code fixes the OP's problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.