用于读取数字的 atoi() 语法不正确

Improper syntax with atoi() for reading in number

对于学校实验室,我必须获取一个包含歌曲的文本文件,然后将台词以特定格式输出,总时间如下。我完成了我的代码,但我的 timeAuthorLine 函数下的 3 atoi(line()) 下有错误行。他们说:

std::string line

Error: must have class type

我怀疑我误用了 atoi(),但我不确定是怎么回事?感谢您的帮助:)

我的文本文件:

Playlist.txt

Mingus "Ah um"
"Better Git It in Your Soul" 7:23 Charles Mingus
"Goodbye Pork Pie Hat" 5:44 Charles Mingus
"Boogie Stop Shuffle" 5:02 Charles Mingus
"Self-Portrait in Three Colors" 3:10 Charles Mingus
"Open Letter to Duke" 5:51 Charles Mingus
"Bird Calls" 6:17 Charles Mingus
"Fables of Faubus" 8:13 Charles Mingus
"Pussy Cat Dues" 9:14 Charles Mingus
"Jelly Roll" 6:17 Charles Mingus
"Bashin" 9:53 Charles Mingus

应该发生的输出:

Mingus "Ah um"
-----------------------------------------------------
 1.  7:23 Better Git It in Your Soul by Charles Mingus
 2.  5:44 Goodbye Pork Pie Hat by Charles Mingus
 3.  5:02 Boogie Stop Shuffle by Charles Mingus
 4.  3:10 Self-Portrait in Three Colors by Charles Mingus
 5.  5:51 Open Letter to Duke by Charles Mingus
 6.  6:17 Bird Calls by Charles Mingus
 7.  8:13 Fables of Faubus by Charles Mingus
 8.  9:14 Pussy Cat Dues by Charles Mingus
 9.  6:17 Jelly Roll by Charles Mingus
10.  9:53 Bashin by Charles Mingus
-----------------------------------------------------
Total Playing Time = 1:07:04

我的代码:

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

using namespace std;

void readLine(vector<string> &playlist);
void timeAuthorLine(const vector<string> playlist, vector<string> &author, vector<string> &name, int &totaltime, vector<int> &min, vector<int> &sec1, vector<int> &sec2);
void displayData(const vector<string> playlist, const vector<string> author, const vector<string> name, int totaltime, const vector<int> min, const vector<int> sec1, const vector<int> sec2);

int main()
{
    vector<string> playlist;

    readLine(playlist);

    system("pause");

    return 0;
}
void readLine(vector<string> &playlist)
{
    vector<string> author, name;
    vector<int> min, sec1, sec2;
    int totaltime = 0, i = 0;
    ifstream inFile("Playlist.txt");
    if (inFile.fail())
    {
        cout << "File not found.";
            exit(1);
    }
    else
    {
        while (getline(inFile, playlist[i]))
        {
            getline(inFile, playlist[i]);
            i = i + 1;
        }
        inFile.close();
        timeAuthorLine(playlist, author, name, totaltime, min, sec1, sec2);
        displayData(playlist, author, name, totaltime, min, sec1, sec2);
    }
}
void timeAuthorLine(const vector<string> playlist, vector<string> &author, vector<string> &name, int &totaltime, vector<int> &min, vector<int> &sec1, vector<int> &sec2)
{
    for (int i = 0; i < playlist.size(); i++)
    {
        string line = playlist[i];
        int index = line.find(":");
        min[i] = atoi(line[index - 1].c_str());
        sec1[i] = atoi(line[index + 1].c_str());
        sec2[i] = atoi(line[index + 2].c_str());
        totaltime = totaltime + 60 * min[i] + 10 * sec1[i] + sec2[i];
        for (int j = index + 4; j < line.size(); j++)
        {
            author[i] = line[j];
        }
        for (int k = 1; k < index - 4; k++)
        {
            name[i] = line[k];
        }
    }
}
void displayData(const vector<string> playlist, const vector<string> author, const vector<string> name, int totaltime, const vector<int> min, const vector<int> sec1, const vector<int> sec2)
{
    cout << playlist[0] << endl;
    cout << "-----------------------------------------------------" << endl;
    for (int i = 0; i < playlist.size(); i++)
    {
        if ((i + 1) > 10)
            cout << " " << i + 1 << ".  ";
        else
            cout << i + 1 << ".  ";
        cout << min[i] << ":" << sec1[i] << sec2[i] << " " << name[i] << " by " << author[i] << endl;
    }
    cout << "-----------------------------------------------------" << endl;
    cout << "Total Playing Time = " << totaltime / 3600 << ":";
    if ((totaltime % 3600) / 60 < 10)
        cout << "0" << (totaltime % 3600) / 60 << ":";
    else
        cout << (totaltime % 3600) / 60 << ":";
    if ((totaltime % 3600) % 60 < 10)
        cout << "0" << (totaltime % 3600) % 60;
    else
        cout << (totaltime % 3600) % 60;
}

line[x]char,因此不是 class,并且没有 c_str().

等成员

由于您要转换的是个位数,请试试这个:

min[i] = line[index - 1] - '0';
sec1[i] = line[index + 1] - '0';
sec2[i] = line[index + 2] - '0';

由于char是一个整数类型,你可以简单地从字符中减去'0'的ASCII/Unicode值得到数字的数值。