Search code examples
stringvectoratoi

Improper syntax with atoi() for reading in number


For a school lab I have to take a text file with songs and out put the lines in a certain format with the total time below. I am done with my code but there are error lines under my 3 atoi(line()) under my timeAuthorLine function. They say:

std::string line

Error: must have class type

I am suspecting I have misused atoi(), but I am not sure how? Any help is appreciated :)

My text file:

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

The output that is supposed to happen:

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

My code:

#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;
}

Solution

  • line[x] is a char and thus is not a class and does not have members such as c_str().

    Since you're converting single digits, try this instead:

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

    Since char is an integer type, you can simply subtract the ASCII/Unicode value of '0' from the character to get the digit's numeric value.