Ich habe einen Code, den ich versuche zu lernen, in C++ zu analysieren. Ich habe alles verstanden, was ich gemacht habe, aber ich verstehe nicht, wie man mit Leuten wie atoi(), atof(), strtod() arbeitet. Ich weiß, was es tun soll, aber ich verstehe nicht, warum der Compiler es nicht mag. Mein Fokus auf den Fehler ist "scores [line_count] = strtod (score);"Wie funktioniert die Konvertierungsfunktion?
#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);
}
"Der Compiler mag es nicht" ist keine sehr hilfreiche Beschreibung. – John3136
Es gibt mir einen Fehler. Es heißt: "Kann float nicht in const char * für Argument 1 konvertieren." – Robbie
@Robbie also warum ist es nicht hilfreich? Sie übergeben 'float' anstelle von' char * '! –