我正在阅读文件和格式数组,以便我可以用其他的东西来处理它们,但是我一开始就卡住了。它说我不能从char *更改为char,但我的令牌不是char *。错误:无效从'char *'转换为'char'行26,36
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
#include <stdio.h>
using namespace std;
void get_input(string teams[][2]) {
string infile;
double value;
char buffer[100];
char token;
stringstream ss;
cout << "Enter the input file: ";
cin >> infile;
ifstream file;
file.open (infile.c_str());
if (file.is_open()) {
int teamcounter = 0;
while (file.getline (buffer, 100)) {
int counter = 0;
token = strtok (buffer, ",");
while (token) {
if (counter == 0) {
teams[teamcounter][counter] = token;
}
else if ((counter == 1) || (counter == 2)) {
ss << token;
ss >> value;
teams[teamcounter][counter] = value;
}
token = strtok (NULL, ",");
counter++;
}
teamcounter++;
}
file.close();
}
else {
cout << "Unable to open file";
}
for (int i = 0; i< 7; i++){
for (int j = 0; j<2;j++){
cout << teams[i][j] << " ";
}
cout << endl;
}
}
正在将我的数组转换为字符串使我无法将浮点数或双精度值给他们吗?
int main() {
cout << "Welcome to the football bracket game!" << endl;
string teams[7][2];
get_input(teams);
}
我输入文本格式是这样的:
Trojans, 0.80, 0.60
Bruins, 0.20, 0.30
Bears, 0.60, 0.50
Trees, 0.50, 0.40
Ducks, 0.40, 0.80
Beavers, 0.50. 0.10
Huskies, 0.80, 0.40
Cougars, 0.10, 0.90
你知道错误来自哪里吗?你能告诉我们吗? – 0x499602D2
那么,当然你不能把一个浮点数或一个double放到char中,但是你可以把一个浮点数或一个double转换成一个字符串,然后存储它。错误是因为strtok返回一个char *而你试图将它存储在char变量标记中。 –
将令牌声明为char *给我分段错误。另外,如果我将数字存储为字符串,那么如何将它们用作浮点数? – user3317141