-1

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;
}
Chris
  • 1
  • 2
    `cin` with `>>` will extract to whitespace. `getline` for a line instead of whitespace delimited input. – crashmstr Dec 11 '14 at 18:59
  • You should probably implement the stream IO operators for your struct (and store read entries in a `std::vector` instead). See also: http://stackoverflow.com/q/4421706/1025391 – moooeeeep Dec 11 '14 at 19:16
  • Sorry, but I don't see where you set quit to true... – Quest Dec 11 '14 at 19:49

1 Answers1

0

The correct way to use string with spaces is to use getline

change this-

cout << "Enter the Title of song # " << num << ": ";
                cin >> songs[songNum].title; 

to this-

getline(std::cin,songs[songNum].title);
Ankur
  • 3,584
  • 1
  • 24
  • 32