0

This is for a homework assignment, but what I am presenting is a small test program for a chunk of my assignment.

Starting out, I am to have a list of songs in file "songs.txt". My current file looks like this.

Maneater;4;32
Whip It;2;41
Wake Me Up Before You Go-Go;3;45

The file simply contains a song title, and the duration in minutes and seconds, with the title, minutes, and seconds separated by semicolons. The full file is supposed to contain the Artists and Album as well, all separated by semicolons. Anyways, the code.

#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
using namespace std;

const int CAP = 100;
const int MAXCHAR = 101;

struct songInfo
{
    char title[CAP];
    char durMin[CAP];
    char durSec[CAP];

};

void getData(songInfo Song[], int listSize, int charSize);

int main()
{
    string fileName;
    songInfo Song[CAP];
    ifstream inFile;

    cout << "What is the file location?: ";
    cin >> fileName;
    inFile.open(fileName.c_str());
    if (inFile.fail())
    {
        cout << "Cannot open file " << fileName << endl;
        exit(1);
    }

    getData(Song, CAP, MAXCHAR);

    for (int i=0;i<CAP;i++)
    {
        cout << Song[i].title << " - "
            << Song[i].durMin << ":"
            << Song[i].durSec << endl;
    }

    cout << "Press any button to continue..." << endl;
    cin.get(); cin.get();

return 0;
}

void getData(songInfo Song[], int listSize, int charSize)
{


    for (int i = 0; i < listSize; i++)
    {
        cin.get(Song[i].title, charSize, ';');
        cin.get(Song[i].durMin, charSize, ';');
        cin.get(Song[i].durSec, charSize, '\n');
        i++;
        cin.ignore();
    }
}

The program compiles correctly without incident, but the output is not what I want it to be. What should happen:

  1. Test.cpp opens songs.txt

  2. Read the first char array into Song[i].title, delimited by ';'

  3. Read the second char into Song[i].durMin, delimited by ';'

  4. Read the third char into Song[i].durSec, delimited by newline

After compiling the code and running it, I get this as my output:

~/project2Test> ./test
What is the file location?: songs.txt

The program then hangs here and I have to ctrl+C out

First, what am I doing wrong? Second, how do I go about fixing what I screwed up?

Also, as a note for class rules, I am not allowed to use any strings except for the filename. Other than that, all words must be chars.

2 Answers2

0

A debugger is definitely a good thing to use for a problem like this.

Your hanging problem is occurring because in your get_data function you are using cin.get instructing your program to get input from the standard input file. You intended to use the file you defined, "inFile" not the standard input cin.

As an aside it is not clear to me why you are incrementing i twice per iteration of the for loop.

hulud
  • 21
  • 5
0

Use inFile.get() instead of cin. You need to pass inFile to the function first.

Put a print statement in the for loop to see what is happening.. A future issue that might crop up is that if you are on a Windows machine and have \r\n line endings. Unix uses \n, Windows uses \r\n

Ryan
  • 436
  • 5
  • 15
  • But unless you open in binary mode, Windows will convert \r\n sequences to the single `char` `'\n'`. – aschepler Apr 18 '17 at 19:05
  • Oh, that's cool. I didn't know that. Last time.. I did use binary mode and that was one of my issues. Thanks for the info. – Ryan Apr 18 '17 at 19:21