5 Replies - 796 Views - Last Post: 18 April 2012 - 02:00 PM Rate Topic: -----

#1 Travis L  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-April 12

Sorting Program with Read-in Issues

Posted 18 April 2012 - 09:22 AM

I'm working on a program for my Programming I class. It's a sorting program within an album name, release year, and song list as the input file. The objective is to sort a portion of The Beatles' discography by both album name and song title. My issue right now is reading in the information and separating the album name from the song title/list. I know, it's probably something that's right in front of me, but I can't seem to figure it out.

#include <iostream>
#include <string>

using namespace std;

struct album
  {
    string name;
    string date;
    string song[30];
    int count;
  } Beatles;

int main()
  {
    string st, len;
    int i;

    while (getline(cin, st))
      {
        len = st.length();
        Beatles.name = st;
        Beatles.date = st;
        do
          {
            Beatles.song[i] = st.substr(4, len);
            i++;
          } while (st != "=");
      }
  }


It says the error is in line 26, which would be
Beatles.song[i] = st.substr(4, len);
but I'm not sure what I'm doing wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Program with Read-in Issues

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Sorting Program with Read-in Issues

Posted 18 April 2012 - 09:27 AM

Your variable len was defined as a string, yet you seem to be trying to use it as a number. Maybe you need to change the type of your len variable to a size_t.

What does your input file look like? Please show a small sample. There may be easier ways to process the input.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Travis L  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-April 12

Re: Sorting Program with Read-in Issues

Posted 18 April 2012 - 12:45 PM

I changed len to an int variable, and it compiled correctly (at least the portion above did), so I'll update it as I come across any other issues..

Here is the input file:

http://www.comsc.uco...tles-songs.html

It's being sorted alphabetically by album and song.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Sorting Program with Read-in Issues

Posted 18 April 2012 - 12:56 PM

An int should work but it really should be a size_t if you are going to be using it to hold the value returned from std::string.length(). The std::string.length() function returns a size_t which can possibly be larger than what an int can hold. You should always use the correct type for the value.

Your substr() method should work with this input file but you don't really need the second parameter. The default parameter should work.
Beatles.song[i] = st.substr(4); // Start at the 4th element and continue to the end of the string.

Which means that you don't really even need the len variable.


Jim

This post has been edited by jimblumberg: 18 April 2012 - 01:12 PM

Was This Post Helpful? 1
  • +
  • -

#5 Travis L  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-April 12

Re: Sorting Program with Read-in Issues

Posted 18 April 2012 - 01:48 PM

Now compiling is fine so far, but I'm running it to print the input and make sure it can read them, but all I'm getting now is a segmentation fault.

while (getline(cin, st))
  {
    Beatles.name = st;
    Beatles.date = st;
    do
      {
        Beatles.song[i] = st.substr(4);
        i++;
      } while(st != "=");

    cout << Beatles.name << endl;
    cout << Beatles.date << endl;
    cout << Beatles.song[i] << endl;
  }

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Sorting Program with Read-in Issues

Posted 18 April 2012 - 02:00 PM

What is the purpose of your do/while loop? How will this loop ever end? Where are you changing the value of st?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1