Switch Cases are ending prematurely
Dec 5, 2016 at 11:16pm UTC
Hi guys, I am creating a music player list for myself and I am having issues with my case statements jumping to case(4) after selecting the third option to create a new playlist. When I enter the amount of songs I want to enter, it skips the for loop entirely and jumps to case statement (4).
Any tips on what I am doing wrong would be appreciated!
The functions that are incomplete or blank will be coded later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream Playlist("Playlist.txt" );
double musicTimeVerify(double SongTime)
{
if (SongTime <= 0)
{
cout << "Incorrect Time format. Please try again!" ;
}
else
return 1;
}
double Songplaylist(double SongTime, int numSong, string musicName)
{
cout << "How many songs are you entering?: " ;
cin >> numSong;
for (int i = 0; i == numSong; i++)
{
cout << "Please enter your music name and THEN the song length in seconds." << endl;
cout << "Song name :" ;
Playlist >> musicName;
cout << "\t" ;
cout << "Song Length (in seconds please): " ;
Playlist >> SongTime;
musicTimeVerify(SongTime);
cout << "\t" ;
}
return 1;
}
void SaveData()
{
}
int main()
{
char choice = 'Y' ;
int numSong = 0;
string musicName;
double songTime = 0;
int selection = 0;
bool Continue = true ;
cout << "_______________________________________________________________________________" << endl;
cout << "Welcome to the Music Playlist program. \nPlease enter in a number for the following:" << endl;
cout << "1.View playlist" << endl;
cout << "2.Delete playlist" << endl;
cout << "3.Create a new playlist" << endl;
cout << "4.Exit the program" << endl;
cout << "_______________________________________________________________________________" << endl;
cout << "Selection: " ;
cin >> selection;
switch (selection)
{
case (1):
case (2):
if (remove("Playlist.txt" ) != 0)
perror("Error in deleting File..." );
else if (cout << "Successful Deletion of the Playlist" );
break ;
case (3):
cout << "A new playlist has been created." << endl;
cout << "Would you like to edit the playlist now? Y/N:" ;
cin >> choice;
if (choice == 'y' || choice == 'Y' )
{
cout << "You are now editing your new playlist." << endl;
Songplaylist(songTime, numSong, musicName);
}
case (4):
cout << "\nGood bye!" ;
system("pause" );
return 0;
}
}
Last edited on Dec 5, 2016 at 11:18pm UTC
Dec 5, 2016 at 11:24pm UTC
You're missing a break
for case (1):
and case (3):
Dec 6, 2016 at 12:31am UTC
If numSong
is greater than 0, then i == numSong
will never be true and the body of the for loop on line 26 will never be entered.
Dec 6, 2016 at 2:31am UTC
Thanks guys for the help!
I have worked on my code a bit more, but I am having issues with my code reading from Playlist.txt. When I chose option #1, I think it attempts to read from the file but then it deletes it as I chose #2. I think it is pretty counter intuitive for it to view then delete the file.
:P
Here is my code so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
fstream Playlist("Playlist.txt" ,ios::out|ios::in);
/* musicTimeVerify(double songTime)
{
if (songTime <= 0)
{
cout << "Incorrect Time format. Please try again!";
}
else
return 1;
}
*/
double Songplaylist(double songTime, int numSong, string musicName)
{
cout << "How many songs are you entering?: " ;
cin >> numSong;
Playlist.open("Playlist.txt" , ios::out);
for (int i = 1; i <= numSong; i++)
{
cout << "Please enter your music name and THEN the song length in seconds." << endl;
cout << "Song name :" ;
cin >> musicName;
Playlist << musicName;
cout << "Song Length (in seconds please): " ;
cin >> musicName;
Playlist << songTime;
// musicTimeVerify(songTime);
}
cout << "Now closing the playlist..." << endl;
Playlist.close();
return musicName,songTime;
}
void SaveData()
{
Playlist.open("Playlist.txt" ,ios::in);
string line;
if (Playlist.is_open())
{
while (getline(Playlist, line))
{
Playlist >> line;
cout << line << endl;
}
}
cout << "Reached the end of File" << endl;
Playlist.close();
}
int main()
{
char choice = 'Y' ;
int numSong = 0;
string musicName;
double songTime = 0;
int selection = 0;
bool Continue = true ;
cout << "_______________________________________________________________________________" << endl;
cout << "Welcome to the Music Playlist program. \nPlease enter in a number for the following:" << endl;
cout << "1.View playlist" << endl;
cout << "2.Delete playlist" << endl;
cout << "3.Create a new playlist" << endl;
cout << "4.Exit the program" << endl;
cout << "_______________________________________________________________________________" << endl;
cout << "Selection: " ;
cin >> selection;
switch (selection)
{
case (1):
SaveData();
break ;
case (2):
Playlist.close();
if (remove("Playlist.txt" ) != 0)
perror("Error in deleting File..." );
else if (cout << "Successful Deletion of the Playlist" );
cout << endl;
system("pause" );
break ;
case (3):
cout << "A new playlist has been created." << endl;
cout << "Would you like to edit the playlist now? Y/N:" ;
cin >> choice;
if (choice == 'y' || choice == 'Y' )
{
cout << "You are now editing your new playlist." << endl;
Songplaylist(songTime, numSong, musicName);
}
break ;
case (4):
cout << "\nGood bye!\n" ;
system("pause" );
break ;
}
}
Last edited on Dec 6, 2016 at 2:34am UTC
Topic archived. No new replies allowed.