[ERROR] cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment

May 25, 2013 at 6:26pm
Write your question here.
I want to read data from csv file and store in to each array, but when I tried to store data into each array I am getting this error and cannot fix the problem. Does anyone knows how to fix this error?
Thank you,
Ayumu Oda

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>  //for cout and cin
#include<conio.h>  //for getch()
#include<cmath>
#include<fstream>  //to intract with files
#include<string>
#include<cstring>
#include <ctime>	// for time(0)
#include <stdlib.h>     /* atof */
#include <vector>
#include <string.h>




using namespace std;
string parseCsvRow ( string row, int commaOffset ) ;

int main(){
	string accountName;
	
	
	int    r=0;
	int numberOfTransactions = 0;
	string date,
		   type,
		   description;
		 float  amount;
		float balace;
	cout << "what is your account name?" << endl;
	cin >> accountName;
	ifstream accountFile;
	accountName+= ".csv"; //add to the account name that user put to .csv.
	accountFile.open(accountName.c_str());

	while (accountFile.good()){
		string row;
		getline (accountFile, row);
		date[r] = parseCsvRow(row, 0);
		type[r] = parseCsvRow(row,1);
		description[r] = parseCsvRow(row, 2);
		amount[r]  = atof(parseCsvRow(row,3).c_str());
		numberOfTransactions++;
		r++;
		
		
		
	}
	accountFile.close();
	
}

string parseCsvRow ( string row, int commaOffset ) {
	int commaIndex = row.find(",",0);
	if ( commaIndex == -1 )
		return "";

	commaOffset++;
	row += ",";

	commaIndex = 0;
	int lastCommaIndex = 0;
	string data;
	for(int i = 0; i < commaOffset; i++ ) {
		lastCommaIndex = commaIndex;
		if(lastCommaIndex > 0) {
			lastCommaIndex++;
		}
		commaIndex = row.find(",",lastCommaIndex);
		if(commaIndex == -1)
			return "";
		data = row.substr(lastCommaIndex, commaIndex - lastCommaIndex);
	}
	return data;
}
Last edited on May 25, 2013 at 6:27pm
May 25, 2013 at 6:43pm
date is a std::string.
date[r] is a single character in that string.

The return type of function parseCsvRow() is std::string.

This line is attempting to assign to a single character, an entire string of characters.
date[r] = parseCsvRow(row, 0);
May 26, 2013 at 5:49pm
Thank you for your response and help.
so how can I fix this issue? I want to assign each data into array, and is there possible way that I can do that?
May 26, 2013 at 5:58pm
while (accountFile.good()){
string row;
getline (accountFile, row);
string date[r] = parseCsvRow(row, 0);
string type[r] = parseCsvRow(row,1);
string description[r] = parseCsvRow(row, 2);
float amount[r] = atof(parseCsvRow(row,3).c_str());
numberOfTransactions++;
r++;


instead I put string before date[r] and now I get this error messages.
[Error] variable-sized object 'date' may not be initialized

what this means? and how can I solve this problem?
May 26, 2013 at 9:10pm
I want to assign each data into array, and is there possible way that I can do that?


I suppose the first thing you need is to define an array. Currently I don't see any array in your program.

Since there are various values that you want to place in an array, then you would need several arrays. However, it might be simpler to define a struct to hold the data from one row, then use a single array of objects of that type.

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/structures/


It looks like you are parsing a csv file in function parseCsvRow. I'd suggest it could be a lot simpler to use a stringstream for that. (If your current code works, you don't need to do this, but it is a possible way to both simplify the code, as well as making use of C++ built-in capabilities rather than re-inventing them).

Example:
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
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>

#include <conio.h>  //for getch()

using namespace std;

struct Account {
    string date;
    string type;
    string description;
    double amount;
};

int main()
{
    Account accounts[1000];
    
    string filename = "odaayumu.csv";
    ifstream accountFile(filename.c_str());

    int r = 0;
    string row;
    const char comma = ',';

    while (getline (accountFile, row))
    {
        istringstream ss(row);
        getline(ss, accounts[r].date, comma);
        getline(ss, accounts[r].type, comma);
        getline(ss, accounts[r].description, comma);
        string temp;
        getline(ss, temp, comma);
        accounts[r].amount = atof(temp.c_str());
        r++;
    }

    int numberOfTransactions = r;

    for (int i=0; i<numberOfTransactions; i++)
    {
        cout << "date: " << accounts[i].date
             << " type: " << accounts[i].type
             << " description: " << accounts[i].description
             << " amount: " << accounts[i].amount
             << endl;
    }
}
Last edited on May 26, 2013 at 10:20pm
Topic archived. No new replies allowed.