-
function problem
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();
}
-
The problem is that you are using two notoriously incompatible function families: scanf, and gets. They handle newlines differently, and this is why you are having this problem. Don't mix the two. And by the way, don't use gets(). If someone were to type:
"Micheal P. Jackson"
for instance, the program could crash, since gets will just keep reading past the array bounds. So use fgets(char[], maxlen, stdin)...
-
A few pointers for you:
>gets(CD0.Artist);
Don't use gets(), it has no buffer overflow protection. Use fgets() instead.
>scanf("%d", &CD0.CD_number);
The scanf() function leaves data in the buffer that it doesn't want. So when you enter a number, it will leave the newline char in the input buffer. The next read call will then receive that newline, which is why it appears to get skipped over.
One quick way round this is to use some code to flush the input stream after calling scanf():
>while (getchar() != '\n');
Alternatively, read the input as a string with fgets(), and convert it to a number some way, but that's for a later lesson, probably. ;)
>main()
The main function should be declared like this:
>int main(void)
and you should return a value at the end, like this:
>return 0;
You have created many struct instances (CD0, CD1 etc). They'd do better as an array. I won't show you how here, unless you ask, but I suggest you review arrays before making code with multiple structs like this one. :)
And finally, please read this before posting your code.
[edit]And if you feel like it, this one too. ;)