Thread: function problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    Unhappy 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();

    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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)...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM