I am currently writing a simple program that will either allow me to add music or show all of my albums, artists, and songs. The program starts in main(); then after selecting "View Music" it goes to the viewMusic() function but runs through the function and skips the getchar() for the next selection.
I tried messing around with it and found that if I write getchar() twice it will do what I want. But this obviously isn't good coding. Is there some sort of memory issue or bad function calling that I'm not aware of?
#include <stdio.h>
int main (void)
{
char action;
printf("Welcome to the music database.\n\n");
printf("What do you want to do? (v - View, e - Enter Data)\n");
action = getchar();
switch (action)
{
case 'v':
printf("\nViewing Music\n");
viewMusic();
return 0;
break;
case 'e':
printf("\nEntering Data\n");
enterMusic();
return 0;
break;
default:
printf("\nInvalid Entry\n\n");
main();
return 0;
break;
}
}
viewMusic()
{
char action;
printf("View by: [a] Album, [r] Artist, [s] Song\n");
action = getchar();
switch (action)
{
case 'a':
printf("Displaying By Album\n");
break;
case 'r':
printf("Displaying By Artist\n");
break;
case 's':
printf("Displaying By Song\n");
break;
default:
printf("Invalid\n\n");
viewMusic();
return 0;
break;
}
}
When I run the code and go to viewMusic() it runs through the function and spits out "Invalid" and then runs the viewMusic(). Once I get to viewMusic() the 2nd time I can actually select my option.
Any clue why it runs through the viewMusic() function without me being able to make a selection the 1st time?