string array funcion

Hello world, that`s me, again :D

I have this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string LOAD_ITMS(string path, string IDs[6]){
       ifstream acc;
       short int i;
       int help;
       string str;
       acc.open(path.c_str());
       acc.seekg(0,ios::beg);
       acc>>str; acc>>help; acc>>help; acc>>help; acc>>help; acc>>help;
       for(i=0;i<=5;i++) acc>>IDs[i];
       return IDs[6];
}

int main(){
          string itemid[6];
          itemid[6]=LOAD_ITMS(path,itemid);
}


and the console is giving me error and shutting down :(
I tried also this change in function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string LOAD_ITMS(string path, string IDs[6]){
       ifstream acc;
       short int i;
       int help;
       string str;
       string itm[6];  //<---
       acc.open(path.c_str());
       acc.seekg(0,ios::beg);
       acc>>str; acc>>help; acc>>help; acc>>help; acc>>help; acc>>help;
       for(i=0;i<=5;i++) acc>>IDs[i];
       for(i=0;i<=5;i++) itm[i+1]=IDs[i];  //<----
       for(i=1;i<=6;i++) cout<<itm[i]<<" ";  //<---
       return itm[6];  //<---
}


and it`s still causing errors in console
Can someone help me? :)
Thanks for answer if some...
I'm not sure what are you trying to do here... You're providing an array of string as a second argument (a copy actually) and then you're returning a single string and assigning it to a single element in that same array.

acc>>str; acc>>help; acc>>help; acc>>help; acc>>help; acc>>help;
Why are you overriding help 4 times?

In the first version you have for(i=0;i<=5;i++) acc>>IDs[i]; which means the data is input to IDs[0] to IDs[5] while you're returning IDs[6].
Im trying to read some values from a file by function and return it as string array
that help I have there cause values I want from file arent at the beginning of the value but after some other values

IDs[6] is IDs[0] to IDs[5]
so in the second code I put their values to itm[6] like this:
itm[1]=ID[0]...itm[6]=ID[5]

and its still causing errors

EDIT: What have I wrong in that second??
EDIT: Hmm seems I have there mistake Im trying to return arrray of strings to array of strings :D
Last edited on
No you're not, you're returning a single string - itm[6], the last value of itm array. Why do you even need IDs ? You can read it directly do the itm array.
hmm I dont know xD

and how to do that that function will return array of strings? :)
Make an array a function parameter and manipulate it directly inside.
Topic archived. No new replies allowed.