我正在阅读一个文件和格式化数组,所以我可以用其他东西来处理它们但我一开始就陷入困境。它说我不能从char *更改为char但我的令牌不是char *。
#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;
}
}
将我的数组变成字符串使我无法将float或double值赋予它们吗?
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
答案 0 :(得分:2)
char token;
token = strtok (buffer, ",");
token
的类型为char
,strtok
返回char*
。它们的类型不同,编译器(逻辑上)抱怨它。 strtok
返回指向已处理输入char*
的下一个标记的指针,所以确实将它分配给单个字符是没有意义的。
编译器确实无法找到从返回char*
转换为char
的方法,错误中没有什么奇怪的。
答案 1 :(得分:0)
strtok()
会返回char*
,但您的token
变量会被声明为char
。它需要声明为char*
。
更新:由于您使用的是C ++,我强烈建议您使用C ++,不要将C和C ++混合在一起。忽略strtok()
甚至存在,例如:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
struct Team
{
std::string Name;
double Value1;
double Value2;
};
void get_input(std::vector<Team> &teams)
{
std::string infile;
std::ifstream file;
std::cout << "Enter the input file: ";
std::cin >> infile;
file.open (infile.c_str());
if (!file.is_open())
{
std::cout << "Unable to open file";
return;
}
std::string line;
while (std::getline(file, line))
{
Team team;
std::istringstream ss(line);
std::string token;
int counter = 0;
while (std::getline(ss, token, ","))
{
switch (counter)
{
case 0:
team.Name = token;
break;
case 1:
std::stringstream(token) >> team.Value1;
break;
case 2:
std::stringstream(token) >> team.Value2;
break;
}
++counter;
}
teams.push_back(team);
}
}
int main()
{
std::cout << "Welcome to the football bracket game!" << std::endl;
std::vector<Team> teams;
get_input(teams);
for (std::vector<Team>::iterator iter = teams.begin(); iter != teams.end(); ++iter)
{
std::cout << iter->Name << ": " << iter->Value1 << " " iter->Value2 << std::endl;
}
}
答案 2 :(得分:0)
您将变量标记定义为类型char
char token;
while函数strtok返回类型char *
的obhect。这些语句
token = strtok (buffer, ",");
和
token = strtok (NULL, ",");
无效。我想你的意思是
char *token;
您还将阵列团队定义为std::string
string teams[7][2];
但是在函数get_input中,您尝试将类型为double的值赋给std :: string
类型的对象double value;
//...
teams[teamcounter][counter] = value;
如果函数有第二个参数指定数组第一维的大小,那么它会更正确
void get_input(string teams[][2], int n );
并将被称为
get_input(teams, 7);
您应该检查您输入的文件的行数。