Hi I was wondering if I could get some help with an issue with my code. I am trying to input data using a struct and storing it in an array. It seems to work when each input is only one word but if I try an input of multiple words I get an infinite loop.
for example for song title if I input: "Fast" it works but if I input "Fast Car" i get an infinite loop.
I am also including an option to print the array data base, but if I haven't filled all 20 places I get a random output for the unused spots. I would like it so that if I input less than 20 songs it will only print what I have input. Thank you in advance.
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>
using namespace std;
void ShowMenu();
struct AFile
{
string artist;
string title;
int length;
string type;
};
int main()
{
const int numOfFiles = 20;
AFile songs[numOfFiles];
int index = 0, num, songNum = 0;
int menuSelection;
bool quit = false;
while (!quit)
{
ShowMenu();
cin >> menuSelection;
switch (menuSelection)
{
//input data
case 1:
cout << "Enter the Song number to be input: ";
cin >> num;
if (num == (index + 1))
{
songNum = (num - 1);
cout << "Enter the Artist of song # " << num << ": ";
cin >> songs[songNum].artist;
cout << "Enter the Title of song # " << num << ": ";
cin >> songs[songNum].title;
cout << "Enter the length in seconds of song # " << num << ": ";
cin >> songs[songNum].length;
cout << "Enter either mp3 or wav of song # " << num << ": ";
cin >> songs[songNum].type;
++index;
cout << "You have " << index << " songs entered into the database.";
break;
}
else
cout << "Invalid input.";
break;
//printout data
case 2:
int i;
cout << "Displaying Database:" << endl;
cout << endl;
for (i = 0; i < index; i++)
{
cout << songs[i].artist << "-";
cout << songs[i].title << "-";
cout << songs[i].length << "s.";
cout << songs[i].type << endl;
}
break;
//edit data
case 3:
break;
//end program
case 4:
quit = true;
break;
default:
cout << "Bad entry please try again " << endl;
break;
}
}
system("pause");
return 0;
}
void ShowMenu()
{
cout << endl;
cout << "What would you like to do? " << endl;
cout << "1) input data" << endl;
cout << "2) print out all data" << endl;
cout << "3) edit data" << endl;
cout << "4) end program" << endl;
}