Could someone please tell me why this program does not run as I expect.
It compiles and links ok.
The program asks you to input details of 2 CDs. these details are stored as instances of a structure declared in the header file.
When the program asks for the second CD's details, a couple of printf lines are executed before they are supposed to be.
please tell me why
header:
#include <stdio.h>
#include <string.h>
//CD structure "template" for CD details
typedef struct
{
char Album_name[20]; //string name of album
char Artist[15]; //string artist
int CD_number; //int ID number of CD...alphabetical order
int CD_popularity; //int most listened too CD
}CD_details; //name of struct type
CD_details CD0 = {"no details","no details",0,0};
CD_details CD1 = {"no details","no details",0,0};
CD_details CD2 = {"no details","no details",0,0};
CD_details CD3 = {"no details","no details",0,0};
CD_details CD4 = {"no details","no details",0,0};
CD_details CD5 = {"no details","no details",0,0};
CD_details CD6 = {"no details","no details",0,0};
CD_details CD7 = {"no details","no details",0,0};
//CD_details CD[i] = {"no details","no details",0,0};
//print structure CD_details function, returning no value
void displayCDdetails0()
{
printf("%s\n",CD0.Artist);
printf("%s\n",CD0.Album_name);
printf("%d\n",CD0.CD_number);
}
void displayCDdetails1()
{
printf("%s\n",CD1.Artist);
printf("%s\n",CD1.Album_name);
printf("%d\n",CD1.CD_number);
}
main program:
/*This program aims to use a structure to record my CD names, artists, etc.
BUG--> for some reason after the first function is called it prints out
a couple of extra printf 's without stopping to input the right data*/
//Preprocessor directives
#include <stdio.h>
#include <string.h>
#include "CD_structure_header.h"//includes declaration of structure and functions
//constant definitions
#define CD_POPULARITY_MAX 10
main()
{
//Entering CD details, and storing it in the
//structure CD details
printf("\nEnter Artist Name\n");
gets(CD0.Artist);
printf("Enter Album name please\n");
gets(CD0.Album_name);
printf("\nEnter CD number\n");
scanf("%d",&CD0.CD_number);
//function to display details of structure
displayCDdetails0();
printf("\nEnter Artist Name\n");
gets(CD1.Artist);
printf("Enter Album name please\n");
gets(CD1.Album_name);
printf("\nEnter CD number\n");
scanf("%d",&CD1.CD_number);
displayCDdetails1();
}