Filling an array

Hello,

i want to split an string into 3 pieces and to store them into an array.Why does the expression speicher[i]=s[i]; not work?

thanks for your help!

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
36
37
38
39
40
41
  #include<iostream>
#include<stdlib.h>
#include<sqlite3.h>
#include<string>
#include<string.h>
#include<fstream>
#include<vector>

using namespace std;

int main(){

string s;
char delimiter = ',';
vector<string> str;
string acc = "";
char speicher[100000];

ifstream namendatei; // liegt im verzeichnis
namendatei.open("namen.txt",ios_base::in); //oeffne textdatei mit namen

if (namendatei.is_open()) {
 
  while (getline(namendatei, s)) {
  
  for(int i = 0; i < s.size(); i++)
  {
    if(s[i] == delimiter)
    {
        str.push_back(acc);
        acc = "";
    }
    else
        acc += s[i];
        speicher[i]=s[i];
}
}
}

return 0;
}
1
2
3
    else
        acc += s[i];
        speicher[i]=s[i];


Because you didnt use an opening brace (as you did for the for loop and the if branch), the "speicher[i]=s[i];" is considered part of the for loop, not the else block. Try changing the code to

1
2
3
4
5
6
7
8
9
10
                if(s[i] == delimiter)
                {
                    str.push_back(acc);
                    acc = "";
                }
                else
                {
                    acc += s[i];
                    speicher[i]=s[i];
                }



I only had a cursory glance so this may be a misfire.
Topic archived. No new replies allowed.