3 Replies - 1674 Views - Last Post: 02 December 2008 - 08:50 PM Rate Topic: -----

#1 bdavidson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-September 08

Stuct/Array/ input file Problem...

Posted 02 December 2008 - 07:38 PM

ok so below i created a program that uses a file input of song title, album, artist, time, and genre. Then it asks the user which artist they would like to learn information about. This is what my input file songs.dat contains

MoonRiver OldPeoplesSongs AndyWilliams 2:30 EasyListening
MoonRiver1 OldPeoplesSongs1 AndyWilliams1 2:31 EasyListening1
MoonRiver2 OldPeoplesSongs2 AndyWilliams2 2:32 EasyListening2
MoonRiver3 OldPeoplesSongs3 AndyWilliams3 2:33 EasyListening3
MoonRiver4 OldPeoplesSongs4 AndyWilliams4 2:34 EasyListening4
MoonRiver5 OldPeoplesSongs5 AndyWilliams5 2:35 EasyListening5
MoonRiver6 OldPeoplesSongs6 AndyWilliams6 2:36 EasyListening6
MoonRiver7 OldPeoplesSongs7 AndyWilliams7 2:37 EasyListening7
MoonRiver8 OldPeoplesSongs8 AndyWilliams 2:38 EasyListening8

So if you type in AndyWilliams1 it will display all the information pertaining to that line above.

**The problem i am having is that say if i have an artist that has written more than one song like for example the first and last one of the input: "AndyWilliams" it will only display the first one. It is like the program skips over the second time it see the same artist...

**If anyone can go through and figure this out that would be great.
**If you want to program to run this on your comp you can just create file called songs.dat and copy and paste the info above.

Thanks any help would be greatly appreciated.


 #include <iostream>
#include <string> 
#include <fstream> 

using namespace std;

//Struct used for array songs[200]
//and labels each piece of info in file
struct SongType 
{	
	string title;
	string album;
	string artist;
	string time;
	string genre; 
}; 

int main ()
{
	int i = 0; //first piece of data in the array used for counter
	int songCount; //Number of songs in the file
	string more = "Yes"; //allows first entrance into loop
	string username; //Artist's name entered by user
	
	//Declare and open files
	ifstream inData; 
	inData.open("songs.dat");
	
	SongType songs[200]; //declaration of an array
	
	//loop that reads each piece of information in an array
	while(!inData.eof()) 
	{
		inData >> songs[i].title >> songs[i].album >> songs[i].artist 
			   >> songs[i].time >> songs[i].genre;
		
		i++; //counter
	}

	songCount = i; //the number of songs in the file
	
//Loop that reiterates if the user wants to find out more information about other artists 
		while ((more == "Yes") || (more == "yes"))
		{
			//displays a message asking for the artist name and then they enter it
			cout << "Please enter the artist of your choice:";
			cin >> username;
			cout << endl;
			
			//Loop that tries to match up the artist name the user entered
			//to one in the file
			for (i=0; i<songCount; i++)
			{	//If the user entered an artist name corresponding to 
				//one in the file enters loop and displays message
				if (username==songs[i].artist)

				//Displays a message corresponding to the users artist entry
					cout << username << " wrote the song " << songs[i].title 
						 << " which is on the album " << songs[i].album 
						 << '.' << endl << "The length of the song is "
						 << songs[i].time << ", and the genre of the music is "
						 << songs[i].genre << '.' << endl << endl;					
				else i++; //used for if statement below 
			}
				if (i == songCount) //Checks if the user entered an invalid 
									//artist name and displays a messege if so
				{
					cout << "Check the spelling of the artist, or we do not "
						 << endl << "have any information on that artist." 
						 << endl << endl; //displays a message and ends 
										  //lines for neatness
				}
			cout << "Would you like to find out more about another artist?"
				 << "(Enter Yes or No): "; //Asks user question
			cin >> more;  //user enters yes or no to continue
			cout << endl; //ends line for neatness
		}

	

	inData.close(); //close file songs.dat

	return 0; 
}


Is This A Good Question/Topic? 0
  • +

Replies To: Stuct/Array/ input file Problem...

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Stuct/Array/ input file Problem...

Posted 02 December 2008 - 07:55 PM

This line is messing you up:
else i++; //used for if statement below

At the entry before AndyWilliams1, it fails the artist name comparison and goes to this line, incrementing i, so that now it would actually point to AndyWilliams1 entry...then i gets incremented immediately again at the end of the loop, so you skip AndyWilliams1 when you go back to the comparison.
Was This Post Helpful? 1
  • +
  • -

#3 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Stuct/Array/ input file Problem...

Posted 02 December 2008 - 08:05 PM

I didn't get to testing your error because I stopped when I got this output:
------
Please enter the artist of your choice:AndyWilliams2

AndyWilliams2 wrote the song MoonRiver2 which is on the album OldPeoplesSongs2.
The length of the song is 2:32, and the genre of the music is EasyListening2.

Check the spelling of the artist, or we do not
have any information on that artist.

Would you like to find out more about another artist?(Enter Yes or No):
------
Since you have already given me the details of the artist telling me you have no info is slightly bizarre.

What I suggest is that you:
First go through your if-then-else statements looking at where you have (and haven't) got braces "{...}" and tidy all of that up.
Next look at the tests you have if your if() statements and think if there are other (better) ways to test for what you are trying to test for.
Was This Post Helpful? 1
  • +
  • -

#4 bdavidson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-September 08

Re: Stuct/Array/ input file Problem...

Posted 02 December 2008 - 08:50 PM

Thanks for the help guys i figured it out now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1