如何使转换功能起作用?

How to make conversion function work?

我有一段代码,正在尝试学习如何在 C++ 中进行解析。我明白我所做的一切,但我不明白如何使用 atoi()、atof()、strtod() 之类的东西。我知道它应该做什么,但我不明白为什么编译器不喜欢它。我关注的错误是 "scores[line_count] = strtod (score);"

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int readScores(string inputFile, string name[], float scores[], int     array_size)
{
//delcare variables
ifstream infile;
int line_count = 0;
string line;
string named;
float score;
char character;
int word_index;
string names[array_size];


// open input file
infile.open(inputFile);

//Check if file opens succesfully.
if(infile.fail())
{
    cout << "File cannot open!!" << endl;
    return -1;
}

while(getline(infile, line))
{
    cout << line << endl;
    // PARSING GOES HERE

    word_index = 0;

    for(int i=0; i < (int)line.length(); i++)
    {
        character = line[i];

        if (character == ',')
        {
            names[line_count] = named;
            named = "";
            word_index++;
        }
        else
        {
            if(word_index == 0)
            {
                named += character;

            }
            else if (word_index == 1)
            {
                score += character;
                cout << character << " " << endl;
            }
        }

    }
scores[line_count] =  strtod (score);



    line_count++;

}

//close file
infile.close();

//return line count
return line_count;
cout << line_count << endl;
}

int main(void)
{
int array_size = 50;
string inputFile = "Test.txt";
string name [array_size];
float scores [array_size];


readScores(inputFile, name, scores, array_size);
}

函数 strtod() 的形式为

double strtod (const char* str, char** endptr);

但你只给它字符串。

如您所见,它有两个参数,一个是您希望转换为双精度的字符串,另一个是 "endptr"。 endptr 被描述为 here 作为

Reference to an already allocated object of type char*, whose value is set by > the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.

所以你需要声明一个char指针来保存小数点后的下一个字符,即使没有。这使您可以从单个字符串中提取多个双打,就像分词器一样。

char * endPtr;
scores[line_count] = strtod(score, &endPtr);

编辑

正如 Alex Lop 指出的那样,您甚至没有将字符串传递给 strtod,而是传递了一个浮点数。看来您想将浮点数转换为双数?

编译器当然不喜欢。请阅读 strtod.

的描述

double strtod (const char* str, char** endptr);

Convert string to double.

Parses the C-string str interpreting its content as a floating point number (according to the current locale) and returns its value as a double. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. A pointer to the rest of the string after the last valid character is stored in the object pointed by endptr.

并且在您的代码中,您只向 strtod 传递了一个类型为 float 的参数,并将 double 的返回结果存储到 float 的数组中.如果要将 float 的值从一个变量移动到另一个变量,则不需要任何 "convertion" 函数:

scores[line_count] = score; 

注意: 我并没有真正检查您的代码,因为您特别询问了 scores[line_count] = strtod (score);。但是在我看了你如何修改 score 之后,也许它应该是 string 而不是 float。如果是这样,那就是另一个要解决的问题。